Update index.js

This commit is contained in:
fcarraUniSa
2026-02-17 11:26:39 +01:00
committed by GitHub
parent c39984debe
commit 30d7a22eef

View File

@@ -169,6 +169,7 @@ app.get('/api/tickets', async (req, res) => {
t.assigned_agent_id as assignedAgentId, t.assigned_agent_id as assignedAgentId,
t.customer_name as customerName, t.customer_name as customerName,
t.created_at as createdAt, t.created_at as createdAt,
t.has_been_analyzed as hasBeenAnalyzed,
( (
SELECT JSON_ARRAYAGG( SELECT JSON_ARRAYAGG(
JSON_OBJECT( JSON_OBJECT(
@@ -189,6 +190,7 @@ app.get('/api/tickets', async (req, res) => {
const tickets = rows.map(t => ({ const tickets = rows.map(t => ({
...t, ...t,
hasBeenAnalyzed: !!t.hasBeenAnalyzed, // Cast to boolean
attachments: safeJsonParse(t.attachments, []), attachments: safeJsonParse(t.attachments, []),
messages: safeJsonParse(t.messages, []) messages: safeJsonParse(t.messages, [])
})); }));
@@ -206,8 +208,8 @@ app.post('/api/tickets', async (req, res) => {
try { try {
await query( await query(
'INSERT INTO tickets (id, subject, description, status, priority, customer_name, queue, attachments) VALUES (?, ?, ?, ?, ?, ?, ?, ?)', 'INSERT INTO tickets (id, subject, description, status, priority, customer_name, queue, attachments, has_been_analyzed) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)',
[id, subject, description, 'APERTO', priority, customerName, queue, JSON.stringify(attachments || [])] [id, subject, description, 'APERTO', priority, customerName, queue, JSON.stringify(attachments || []), false]
); );
const newTicket = { const newTicket = {
@@ -220,6 +222,7 @@ app.post('/api/tickets', async (req, res) => {
queue, queue,
attachments: attachments || [], attachments: attachments || [],
messages: [], messages: [],
hasBeenAnalyzed: false,
createdAt: new Date().toISOString() createdAt: new Date().toISOString()
}; };
@@ -241,6 +244,7 @@ app.patch('/api/tickets/:id', async (req, res) => {
if (updates.priority) { fields.push('priority = ?'); values.push(updates.priority); } if (updates.priority) { fields.push('priority = ?'); values.push(updates.priority); }
if (updates.assignedAgentId !== undefined) { fields.push('assigned_agent_id = ?'); values.push(updates.assignedAgentId || null); } if (updates.assignedAgentId !== undefined) { fields.push('assigned_agent_id = ?'); values.push(updates.assignedAgentId || null); }
if (updates.queue) { fields.push('queue = ?'); values.push(updates.queue); } if (updates.queue) { fields.push('queue = ?'); values.push(updates.queue); }
if (updates.hasBeenAnalyzed !== undefined) { fields.push('has_been_analyzed = ?'); values.push(updates.hasBeenAnalyzed); }
if (fields.length === 0) return res.json({ success: true }); if (fields.length === 0) return res.json({ success: true });
@@ -254,6 +258,22 @@ app.patch('/api/tickets/:id', async (req, res) => {
} }
}); });
app.post('/api/tickets/mark-analyzed', async (req, res) => {
const { ticketIds } = req.body;
if (!Array.isArray(ticketIds) || ticketIds.length === 0) {
return res.json({ success: true, count: 0 });
}
try {
// Safe parameter expansion
const placeholders = ticketIds.map(() => '?').join(',');
await query(`UPDATE tickets SET has_been_analyzed = TRUE WHERE id IN (${placeholders})`, ticketIds);
res.json({ success: true, count: ticketIds.length });
} catch (e) {
res.status(500).json({ error: e.message });
}
});
app.post('/api/tickets/:id/messages', async (req, res) => { app.post('/api/tickets/:id/messages', async (req, res) => {
const { id } = req.params; const { id } = req.params;
const { role, content } = req.body; const { role, content } = req.body;
@@ -387,12 +407,12 @@ app.post('/api/surveys', async (req, res) => {
}); });
app.post('/api/articles', async (req, res) => { app.post('/api/articles', async (req, res) => {
const { title, content, category, type, url, source } = req.body; const { title, content, category, type, url, source, visibility } = req.body;
const id = `kb-${Date.now()}`; const id = `kb-${Date.now()}`;
try { try {
await query( await query(
'INSERT INTO kb_articles (id, title, content, category, type, url, source) VALUES (?, ?, ?, ?, ?, ?, ?)', 'INSERT INTO kb_articles (id, title, content, category, type, url, source, visibility) VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
[id, title, content, category, type, url || null, source || 'manual'] [id, title, content, category, type, url || null, source || 'manual', visibility || 'public']
); );
res.json({ success: true, id, lastUpdated: new Date().toISOString() }); res.json({ success: true, id, lastUpdated: new Date().toISOString() });
} catch (e) { } catch (e) {
@@ -402,11 +422,11 @@ app.post('/api/articles', async (req, res) => {
app.patch('/api/articles/:id', async (req, res) => { app.patch('/api/articles/:id', async (req, res) => {
const { id } = req.params; const { id } = req.params;
const { title, content, category, type, url } = req.body; const { title, content, category, type, url, visibility } = req.body;
try { try {
await query( await query(
'UPDATE kb_articles SET title=?, content=?, category=?, type=?, url=? WHERE id=?', 'UPDATE kb_articles SET title=?, content=?, category=?, type=?, url=?, visibility=? WHERE id=?',
[title, content, category, type, url || null, id] [title, content, category, type, url || null, visibility || 'public', id]
); );
res.json({ success: true }); res.json({ success: true });
} catch (e) { } catch (e) {
@@ -414,6 +434,15 @@ app.patch('/api/articles/:id', async (req, res) => {
} }
}); });
app.delete('/api/articles/:id', async (req, res) => {
try {
await query('DELETE FROM kb_articles WHERE id = ?', [req.params.id]);
res.json({ success: true });
} catch (e) {
res.status(500).json({ error: e.message });
}
});
const startServer = async () => { const startServer = async () => {
await initDb(); await initDb();
app.listen(PORT, () => { app.listen(PORT, () => {