Update index.js
This commit is contained in:
@@ -10,7 +10,16 @@ app.use(cors());
|
|||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
// --- HELPER FUNCTIONS ---
|
// --- 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 ---
|
// --- 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]);
|
const agents = await query('SELECT * FROM agents WHERE email = ? AND password = ?', [email, password]);
|
||||||
if (agents.length > 0) {
|
if (agents.length > 0) {
|
||||||
const agent = agents[0];
|
const agent = agents[0];
|
||||||
// Parse JSON fields
|
agent.queues = safeJsonParse(agent.queues, []);
|
||||||
agent.queues = typeof agent.queues === 'string' ? JSON.parse(agent.queues) : agent.queues;
|
agent.skills = safeJsonParse(agent.skills, []);
|
||||||
agent.skills = typeof agent.skills === 'string' ? JSON.parse(agent.skills) : agent.skills;
|
agent.avatarConfig = safeJsonParse(agent.avatar_config, {x: 50, y: 50, scale: 1});
|
||||||
agent.avatarConfig = typeof agent.avatar_config === 'string' ? JSON.parse(agent.avatar_config) : agent.avatar_config;
|
|
||||||
return res.json({ user: agent, role: agent.role });
|
return res.json({ user: agent, role: agent.role });
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,11 +74,11 @@ app.get('/api/settings', async (req, res) => {
|
|||||||
|
|
||||||
const s = rows[0];
|
const s = rows[0];
|
||||||
const settings = {
|
const settings = {
|
||||||
branding: typeof s.branding === 'string' ? JSON.parse(s.branding) : s.branding,
|
branding: safeJsonParse(s.branding, {}),
|
||||||
smtp: typeof s.smtp === 'string' ? JSON.parse(s.smtp) : s.smtp,
|
smtp: safeJsonParse(s.smtp, {}),
|
||||||
emailTemplates: typeof s.email_templates === 'string' ? JSON.parse(s.email_templates) : s.email_templates,
|
emailTemplates: safeJsonParse(s.email_templates, []),
|
||||||
features: typeof s.features === 'string' ? JSON.parse(s.features) : s.features,
|
features: safeJsonParse(s.features, {}),
|
||||||
aiConfig: typeof s.ai_config === 'string' ? JSON.parse(s.ai_config) : s.ai_config
|
aiConfig: safeJsonParse(s.ai_config, {})
|
||||||
};
|
};
|
||||||
res.json(settings);
|
res.json(settings);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -106,9 +114,9 @@ app.get('/api/initial-data', async (req, res) => {
|
|||||||
const agents = await query('SELECT * FROM agents');
|
const agents = await query('SELECT * FROM agents');
|
||||||
const parsedAgents = agents.map(a => ({
|
const parsedAgents = agents.map(a => ({
|
||||||
...a,
|
...a,
|
||||||
queues: typeof a.queues === 'string' ? JSON.parse(a.queues) : a.queues,
|
queues: safeJsonParse(a.queues, []),
|
||||||
skills: typeof a.skills === 'string' ? JSON.parse(a.skills) : a.skills,
|
skills: safeJsonParse(a.skills, []),
|
||||||
avatarConfig: typeof a.avatar_config === 'string' ? JSON.parse(a.avatar_config) : a.avatar_config
|
avatarConfig: safeJsonParse(a.avatar_config, {x: 50, y: 50, scale: 1})
|
||||||
}));
|
}));
|
||||||
|
|
||||||
// Fetch Client Users
|
// Fetch Client Users
|
||||||
@@ -129,11 +137,11 @@ app.get('/api/initial-data', async (req, res) => {
|
|||||||
if (settingsRows.length > 0) {
|
if (settingsRows.length > 0) {
|
||||||
const s = settingsRows[0];
|
const s = settingsRows[0];
|
||||||
settings = {
|
settings = {
|
||||||
branding: typeof s.branding === 'string' ? JSON.parse(s.branding) : s.branding,
|
branding: safeJsonParse(s.branding, {}),
|
||||||
smtp: typeof s.smtp === 'string' ? JSON.parse(s.smtp) : s.smtp,
|
smtp: safeJsonParse(s.smtp, {}),
|
||||||
emailTemplates: typeof s.email_templates === 'string' ? JSON.parse(s.email_templates) : s.email_templates,
|
emailTemplates: safeJsonParse(s.email_templates, []),
|
||||||
features: typeof s.features === 'string' ? JSON.parse(s.features) : s.features,
|
features: safeJsonParse(s.features, {}),
|
||||||
aiConfig: typeof s.ai_config === 'string' ? JSON.parse(s.ai_config) : s.ai_config
|
aiConfig: safeJsonParse(s.ai_config, {})
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,7 +154,7 @@ app.get('/api/initial-data', async (req, res) => {
|
|||||||
settings
|
settings
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error("Initial Data Error:", e);
|
||||||
res.status(500).json({ error: e.message });
|
res.status(500).json({ error: e.message });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -181,8 +189,8 @@ app.get('/api/tickets', async (req, res) => {
|
|||||||
|
|
||||||
const tickets = rows.map(t => ({
|
const tickets = rows.map(t => ({
|
||||||
...t,
|
...t,
|
||||||
attachments: typeof t.attachments === 'string' ? JSON.parse(t.attachments || '[]') : t.attachments,
|
attachments: safeJsonParse(t.attachments, []),
|
||||||
messages: typeof t.messages === 'string' ? JSON.parse(t.messages || '[]') : (t.messages || [])
|
messages: safeJsonParse(t.messages, [])
|
||||||
}));
|
}));
|
||||||
|
|
||||||
res.json(tickets);
|
res.json(tickets);
|
||||||
|
|||||||
Reference in New Issue
Block a user