Update server.js
This commit is contained in:
@@ -657,11 +657,29 @@ app.get('/api/tickets', authenticateToken, async (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
app.post('/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 id = uuidv4();
|
||||||
const connection = await pool.getConnection();
|
const connection = await pool.getConnection();
|
||||||
|
|
||||||
try {
|
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.beginTransaction();
|
||||||
await connection.query(
|
await connection.query(
|
||||||
'INSERT INTO tickets (id, condo_id, user_id, title, description, category, priority) VALUES (?, ?, ?, ?, ?, ?, ?)',
|
'INSERT INTO tickets (id, condo_id, user_id, title, description, category, priority) VALUES (?, ?, ?, ?, ?, ?, ?)',
|
||||||
|
|||||||
Reference in New Issue
Block a user