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.customer_name as customerName,
t.created_at as createdAt,
t.has_been_analyzed as hasBeenAnalyzed,
(
SELECT JSON_ARRAYAGG(
JSON_OBJECT(
@@ -189,6 +190,7 @@ app.get('/api/tickets', async (req, res) => {
const tickets = rows.map(t => ({
...t,
hasBeenAnalyzed: !!t.hasBeenAnalyzed, // Cast to boolean
attachments: safeJsonParse(t.attachments, []),
messages: safeJsonParse(t.messages, [])
}));
@@ -206,8 +208,8 @@ app.post('/api/tickets', async (req, res) => {
try {
await query(
'INSERT INTO tickets (id, subject, description, status, priority, customer_name, queue, attachments) VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
[id, subject, description, 'APERTO', priority, customerName, queue, JSON.stringify(attachments || [])]
'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 || []), false]
);
const newTicket = {
@@ -220,6 +222,7 @@ app.post('/api/tickets', async (req, res) => {
queue,
attachments: attachments || [],
messages: [],
hasBeenAnalyzed: false,
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.assignedAgentId !== undefined) { fields.push('assigned_agent_id = ?'); values.push(updates.assignedAgentId || null); }
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 });
@@ -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) => {
const { id } = req.params;
const { role, content } = req.body;
@@ -387,12 +407,12 @@ app.post('/api/surveys', 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()}`;
try {
await query(
'INSERT INTO kb_articles (id, title, content, category, type, url, source) VALUES (?, ?, ?, ?, ?, ?, ?)',
[id, title, content, category, type, url || null, source || 'manual']
'INSERT INTO kb_articles (id, title, content, category, type, url, source, visibility) VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
[id, title, content, category, type, url || null, source || 'manual', visibility || 'public']
);
res.json({ success: true, id, lastUpdated: new Date().toISOString() });
} catch (e) {
@@ -402,11 +422,11 @@ app.post('/api/articles', async (req, res) => {
app.patch('/api/articles/:id', async (req, res) => {
const { id } = req.params;
const { title, content, category, type, url } = req.body;
const { title, content, category, type, url, visibility } = req.body;
try {
await query(
'UPDATE kb_articles SET title=?, content=?, category=?, type=?, url=? WHERE id=?',
[title, content, category, type, url || null, id]
'UPDATE kb_articles SET title=?, content=?, category=?, type=?, url=?, visibility=? WHERE id=?',
[title, content, category, type, url || null, visibility || 'public', id]
);
res.json({ success: true });
} 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 () => {
await initDb();
app.listen(PORT, () => {