diff --git a/server/integrations/partsManagement/partsManagement.queries.js b/server/integrations/partsManagement/partsManagement.queries.js index afcc9a43c..129ca4419 100644 --- a/server/integrations/partsManagement/partsManagement.queries.js +++ b/server/integrations/partsManagement/partsManagement.queries.js @@ -216,6 +216,36 @@ const GET_JOBLINES_NOTES_BY_JOBID_UNQSEQ = ` } `; +// Clear task links to parts orders for all jobs in a shop to avoid FK violations when deleting parts orders +const CLEAR_TASKS_PARTSORDER_LINKS_BY_JOBIDS = ` + mutation ClearTasksPartsOrderLinks($jobIds: [uuid!]!) { + update_tasks( + where: { parts_order: { jobid: { _in: $jobIds } } }, + _set: { partsorderid: null } + ) { + affected_rows + } + } +`; + +// Delete parts order lines where the parent order belongs to any of the provided job IDs +const DELETE_PARTS_ORDER_LINES_BY_JOB_IDS = ` + mutation DeletePartsOrderLinesByJobIds($jobIds: [uuid!]!) { + delete_parts_order_lines(where: { parts_order: { jobid: { _in: $jobIds } } }) { + affected_rows + } + } +`; + +// Delete parts orders for the given job IDs +const DELETE_PARTS_ORDERS_BY_JOB_IDS = ` + mutation DeletePartsOrdersByJobIds($jobIds: [uuid!]!) { + delete_parts_orders(where: { jobid: { _in: $jobIds } }) { + affected_rows + } + } +`; + module.exports = { GET_BODYSHOP_STATUS, GET_VEHICLE_BY_SHOP_VIN, @@ -241,5 +271,9 @@ module.exports = { DELETE_JOBS_BY_IDS, DELETE_AUDIT_TRAIL_BY_SHOP, GET_JOBLINES_NOTES_BY_JOBID_UNQSEQ, - GET_JOB_BY_ID + GET_JOB_BY_ID, + // newly added exports + CLEAR_TASKS_PARTSORDER_LINKS_BY_JOBIDS, + DELETE_PARTS_ORDER_LINES_BY_JOB_IDS, + DELETE_PARTS_ORDERS_BY_JOB_IDS };