diff --git a/server/server.js b/server/server.js index 67bdb12..9d9de2a 100644 --- a/server/server.js +++ b/server/server.js @@ -657,11 +657,29 @@ app.get('/api/tickets', authenticateToken, async (req, res) => { }); app.post('/api/tickets', authenticateToken, async (req, res) => { - const { condoId, title, description, category, priority, attachments } = req.body; + let { condoId, title, description, category, priority, attachments } = req.body; const id = uuidv4(); const connection = await pool.getConnection(); try { + // Robustness: Resolve condoId if missing + if (!condoId) { + // 1. Try from User's Family + if (req.user.familyId) { + const [f] = await connection.query('SELECT condo_id FROM families WHERE id = ?', [req.user.familyId]); + if (f.length > 0) condoId = f[0].condo_id; + } + // 2. Fallback to first condo + if (!condoId) { + const [c] = await connection.query('SELECT id FROM condos LIMIT 1'); + if (c.length > 0) condoId = c[0].id; + } + } + + if (!condoId) { + return res.status(400).json({ message: "Impossibile determinare il condominio. Selezionane uno o contatta l'amministratore." }); + } + await connection.beginTransaction(); await connection.query( 'INSERT INTO tickets (id, condo_id, user_id, title, description, category, priority) VALUES (?, ?, ?, ?, ?, ?, ?)',