From 520346d4a5e5213157552276d88edbee9f175bea Mon Sep 17 00:00:00 2001 From: carlossa Date: Mon, 7 Oct 2024 13:03:42 +0200 Subject: [PATCH] fix: refs #7906 remake method --- modules/zone/back/methods/zone/deleteZone.js | 33 +++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/modules/zone/back/methods/zone/deleteZone.js b/modules/zone/back/methods/zone/deleteZone.js index a75302703..6ef663a03 100644 --- a/modules/zone/back/methods/zone/deleteZone.js +++ b/modules/zone/back/methods/zone/deleteZone.js @@ -1,3 +1,4 @@ +const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethodCtx('deleteZone', { description: 'Delete a zone', @@ -52,23 +53,25 @@ module.exports = Self => { const promises = []; const ticketList = await models.Ticket.find(filter, myOptions); - const fixingState = await models.State.findOne({where: {code: 'FIXING'}}, myOptions); - const worker = await models.Worker.findOne({ - where: {id: userId} - }, myOptions); + // const fixingState = await models.State.findOne({where: {code: 'FIXING'}}, myOptions); + // const worker = await models.Worker.findOne({ + // where: {id: userId} + // }, myOptions); + if (ticketList.length > 0) + throw new UserError('You cannot delete a zone that is being used'); - await models.Ticket.rawSql('UPDATE ticket SET zoneFk = NULL WHERE zoneFk = ?', [id], myOptions); + // await models.Ticket.rawSql('UPDATE ticket SET zoneFk = NULL WHERE zoneFk = ?', [id], myOptions); - for (ticket of ticketList) { - if (ticket.ticketState().alertLevel == 0) { - promises.push(models.Ticket.state(ctx, { - ticketFk: ticket.id, - stateFk: fixingState.id, - userFk: worker.id - }, myOptions)); - } - } - await Promise.all(promises); + // for (ticket of ticketList) { + // if (ticket.ticketState().alertLevel == 0) { + // promises.push(models.Ticket.state(ctx, { + // ticketFk: ticket.id, + // stateFk: fixingState.id, + // userFk: worker.id + // }, myOptions)); + // } + // } + // await Promise.all(promises); await models.Zone.destroyById(id, myOptions); if (tx) await tx.commit();