Update index.js
This commit is contained in:
@@ -2,12 +2,41 @@ import express from 'express';
|
|||||||
import cors from 'cors';
|
import cors from 'cors';
|
||||||
import { checkConnection, query, initDb } from './db.js';
|
import { checkConnection, query, initDb } from './db.js';
|
||||||
import { randomUUID } from 'crypto';
|
import { randomUUID } from 'crypto';
|
||||||
|
import multer from 'multer';
|
||||||
|
import path from 'path';
|
||||||
|
import fs from 'fs';
|
||||||
|
import { fileURLToPath } from 'url';
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const PORT = process.env.PORT || 3000;
|
const PORT = process.env.PORT || 3000;
|
||||||
|
|
||||||
|
// Fix per __dirname in ES Modules
|
||||||
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
|
const __dirname = path.dirname(__filename);
|
||||||
|
|
||||||
|
// Ensure uploads directory exists
|
||||||
|
const uploadDir = path.join(__dirname, 'uploads');
|
||||||
|
if (!fs.existsSync(uploadDir)) {
|
||||||
|
fs.mkdirSync(uploadDir, { recursive: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Multer Configuration
|
||||||
|
const storage = multer.diskStorage({
|
||||||
|
destination: function (req, file, cb) {
|
||||||
|
cb(null, uploadDir)
|
||||||
|
},
|
||||||
|
filename: function (req, file, cb) {
|
||||||
|
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9)
|
||||||
|
cb(null, uniqueSuffix + '-' + file.originalname)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const upload = multer({ storage: storage });
|
||||||
|
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
// Serve uploaded files statically
|
||||||
|
app.use('/uploads', express.static(uploadDir));
|
||||||
|
|
||||||
// --- HELPER FUNCTIONS ---
|
// --- HELPER FUNCTIONS ---
|
||||||
const safeJsonParse = (val, fallback) => {
|
const safeJsonParse = (val, fallback) => {
|
||||||
@@ -159,6 +188,24 @@ app.get('/api/initial-data', async (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// --- UPLOAD ENDPOINT ---
|
||||||
|
app.post('/api/upload', upload.single('file'), (req, res) => {
|
||||||
|
if (!req.file) {
|
||||||
|
return res.status(400).json({ error: 'Nessun file caricato' });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Construct absolute URL for the file
|
||||||
|
// In prod, this would be S3 url or similar
|
||||||
|
const fileUrl = `/api/uploads/${req.file.filename}`;
|
||||||
|
|
||||||
|
res.json({
|
||||||
|
id: req.file.filename,
|
||||||
|
name: req.file.originalname,
|
||||||
|
url: fileUrl,
|
||||||
|
type: req.file.mimetype
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
// --- TICKET ENDPOINTS ---
|
// --- TICKET ENDPOINTS ---
|
||||||
|
|
||||||
app.get('/api/tickets', async (req, res) => {
|
app.get('/api/tickets', async (req, res) => {
|
||||||
@@ -176,7 +223,8 @@ app.get('/api/tickets', async (req, res) => {
|
|||||||
'id', m.id,
|
'id', m.id,
|
||||||
'role', m.role,
|
'role', m.role,
|
||||||
'content', m.content,
|
'content', m.content,
|
||||||
'timestamp', m.timestamp
|
'timestamp', m.timestamp,
|
||||||
|
'attachments', m.attachments
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
FROM ticket_messages m
|
FROM ticket_messages m
|
||||||
@@ -192,7 +240,10 @@ app.get('/api/tickets', async (req, res) => {
|
|||||||
...t,
|
...t,
|
||||||
hasBeenAnalyzed: !!t.hasBeenAnalyzed, // Cast to boolean
|
hasBeenAnalyzed: !!t.hasBeenAnalyzed, // Cast to boolean
|
||||||
attachments: safeJsonParse(t.attachments, []),
|
attachments: safeJsonParse(t.attachments, []),
|
||||||
messages: safeJsonParse(t.messages, [])
|
messages: safeJsonParse(t.messages, []).map(m => ({
|
||||||
|
...m,
|
||||||
|
attachments: safeJsonParse(m.attachments, [])
|
||||||
|
}))
|
||||||
}));
|
}));
|
||||||
|
|
||||||
res.json(tickets);
|
res.json(tickets);
|
||||||
@@ -276,13 +327,13 @@ app.post('/api/tickets/mark-analyzed', async (req, res) => {
|
|||||||
|
|
||||||
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, attachments } = req.body;
|
||||||
const messageId = `m-${Date.now()}`;
|
const messageId = `m-${Date.now()}`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await query(
|
await query(
|
||||||
'INSERT INTO ticket_messages (id, ticket_id, role, content) VALUES (?, ?, ?, ?)',
|
'INSERT INTO ticket_messages (id, ticket_id, role, content, attachments) VALUES (?, ?, ?, ?, ?)',
|
||||||
[messageId, id, role, content]
|
[messageId, id, role, content, JSON.stringify(attachments || [])]
|
||||||
);
|
);
|
||||||
|
|
||||||
if (role === 'user') {
|
if (role === 'user') {
|
||||||
@@ -293,6 +344,7 @@ app.post('/api/tickets/:id/messages', async (req, res) => {
|
|||||||
id: messageId,
|
id: messageId,
|
||||||
role,
|
role,
|
||||||
content,
|
content,
|
||||||
|
attachments: attachments || [],
|
||||||
timestamp: new Date().toISOString()
|
timestamp: new Date().toISOString()
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user