Fix Clear Completed button failing on queue items with Jira ticket links
The DELETE /completed endpoint failed with a FK violation when completed queue items had associated rows in jira_ticket_queue_items. Replaced the bare DELETE query with a transaction that removes junction table references before deleting the queue items themselves. Transaction sequence: BEGIN → SELECT completed IDs → DELETE junction rows → DELETE queue items → COMMIT, with ROLLBACK on error and client release in finally block.
This commit is contained in:
@@ -441,15 +441,43 @@ function createIvantiTodoQueueRouter() {
|
||||
* @error 500 Internal server error
|
||||
*/
|
||||
router.delete('/completed', requireAuth(), requireGroup('Admin', 'Standard_User'), async (req, res) => {
|
||||
const client = await pool.connect();
|
||||
try {
|
||||
const result = await pool.query(
|
||||
"DELETE FROM ivanti_todo_queue WHERE user_id = $1 AND status = 'complete'",
|
||||
await client.query('BEGIN');
|
||||
|
||||
// Select completed item IDs for this user
|
||||
const { rows: completedRows } = await client.query(
|
||||
"SELECT id FROM ivanti_todo_queue WHERE user_id = $1 AND status = 'complete'",
|
||||
[req.user.id]
|
||||
);
|
||||
res.json({ message: 'Completed items cleared.', deleted: result.rowCount });
|
||||
|
||||
if (completedRows.length === 0) {
|
||||
await client.query('COMMIT');
|
||||
return res.json({ message: 'Completed items cleared.', deleted: 0 });
|
||||
}
|
||||
|
||||
const ids = completedRows.map(r => r.id);
|
||||
|
||||
// Delete junction table references first
|
||||
await client.query(
|
||||
'DELETE FROM jira_ticket_queue_items WHERE queue_item_id = ANY($1::int[])',
|
||||
[ids]
|
||||
);
|
||||
|
||||
// Delete the completed queue items
|
||||
const deleteResult = await client.query(
|
||||
'DELETE FROM ivanti_todo_queue WHERE id = ANY($1::int[])',
|
||||
[ids]
|
||||
);
|
||||
|
||||
await client.query('COMMIT');
|
||||
res.json({ message: 'Completed items cleared.', deleted: deleteResult.rowCount });
|
||||
} catch (err) {
|
||||
await client.query('ROLLBACK');
|
||||
console.error('Error clearing completed queue items:', err);
|
||||
res.status(500).json({ error: 'Internal server error.' });
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user