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();