Update index.js

This commit is contained in:
fcarraUniSa
2026-02-17 10:50:54 +01:00
committed by GitHub
parent 15c1a0247e
commit b7944a8cc0

View File

@@ -10,7 +10,16 @@ app.use(cors());
app.use(express.json());
// --- HELPER FUNCTIONS ---
const formatDate = (date) => new Date(date).toISOString();
const safeJsonParse = (val, fallback) => {
if (!val) return fallback;
if (typeof val === 'object') return val; // Already parsed by mysql2
try {
return JSON.parse(val);
} catch (e) {
console.warn('JSON Parse error:', e);
return fallback;
}
};
// --- AUTH ENDPOINTS ---
@@ -22,10 +31,9 @@ app.post('/api/auth/login', async (req, res) => {
const agents = await query('SELECT * FROM agents WHERE email = ? AND password = ?', [email, password]);
if (agents.length > 0) {
const agent = agents[0];
// Parse JSON fields
agent.queues = typeof agent.queues === 'string' ? JSON.parse(agent.queues) : agent.queues;
agent.skills = typeof agent.skills === 'string' ? JSON.parse(agent.skills) : agent.skills;
agent.avatarConfig = typeof agent.avatar_config === 'string' ? JSON.parse(agent.avatar_config) : agent.avatar_config;
agent.queues = safeJsonParse(agent.queues, []);
agent.skills = safeJsonParse(agent.skills, []);
agent.avatarConfig = safeJsonParse(agent.avatar_config, {x: 50, y: 50, scale: 1});
return res.json({ user: agent, role: agent.role });
}
@@ -66,11 +74,11 @@ app.get('/api/settings', async (req, res) => {
const s = rows[0];
const settings = {
branding: typeof s.branding === 'string' ? JSON.parse(s.branding) : s.branding,
smtp: typeof s.smtp === 'string' ? JSON.parse(s.smtp) : s.smtp,
emailTemplates: typeof s.email_templates === 'string' ? JSON.parse(s.email_templates) : s.email_templates,
features: typeof s.features === 'string' ? JSON.parse(s.features) : s.features,
aiConfig: typeof s.ai_config === 'string' ? JSON.parse(s.ai_config) : s.ai_config
branding: safeJsonParse(s.branding, {}),
smtp: safeJsonParse(s.smtp, {}),
emailTemplates: safeJsonParse(s.email_templates, []),
features: safeJsonParse(s.features, {}),
aiConfig: safeJsonParse(s.ai_config, {})
};
res.json(settings);
} catch (e) {
@@ -106,9 +114,9 @@ app.get('/api/initial-data', async (req, res) => {
const agents = await query('SELECT * FROM agents');
const parsedAgents = agents.map(a => ({
...a,
queues: typeof a.queues === 'string' ? JSON.parse(a.queues) : a.queues,
skills: typeof a.skills === 'string' ? JSON.parse(a.skills) : a.skills,
avatarConfig: typeof a.avatar_config === 'string' ? JSON.parse(a.avatar_config) : a.avatar_config
queues: safeJsonParse(a.queues, []),
skills: safeJsonParse(a.skills, []),
avatarConfig: safeJsonParse(a.avatar_config, {x: 50, y: 50, scale: 1})
}));
// Fetch Client Users
@@ -129,11 +137,11 @@ app.get('/api/initial-data', async (req, res) => {
if (settingsRows.length > 0) {
const s = settingsRows[0];
settings = {
branding: typeof s.branding === 'string' ? JSON.parse(s.branding) : s.branding,
smtp: typeof s.smtp === 'string' ? JSON.parse(s.smtp) : s.smtp,
emailTemplates: typeof s.email_templates === 'string' ? JSON.parse(s.email_templates) : s.email_templates,
features: typeof s.features === 'string' ? JSON.parse(s.features) : s.features,
aiConfig: typeof s.ai_config === 'string' ? JSON.parse(s.ai_config) : s.ai_config
branding: safeJsonParse(s.branding, {}),
smtp: safeJsonParse(s.smtp, {}),
emailTemplates: safeJsonParse(s.email_templates, []),
features: safeJsonParse(s.features, {}),
aiConfig: safeJsonParse(s.ai_config, {})
};
}
@@ -146,7 +154,7 @@ app.get('/api/initial-data', async (req, res) => {
settings
});
} catch (e) {
console.error(e);
console.error("Initial Data Error:", e);
res.status(500).json({ error: e.message });
}
});
@@ -181,8 +189,8 @@ app.get('/api/tickets', async (req, res) => {
const tickets = rows.map(t => ({
...t,
attachments: typeof t.attachments === 'string' ? JSON.parse(t.attachments || '[]') : t.attachments,
messages: typeof t.messages === 'string' ? JSON.parse(t.messages || '[]') : (t.messages || [])
attachments: safeJsonParse(t.attachments, []),
messages: safeJsonParse(t.messages, [])
}));
res.json(tickets);