From a9497dd2db49e597dc72c2a8364e7b881f59b77f Mon Sep 17 00:00:00 2001 From: carlossa Date: Thu, 3 Oct 2024 10:59:27 +0200 Subject: [PATCH] fix: refs #7906 optimize method fix --- modules/zone/back/methods/zone/deleteZone.js | 47 ++++++++++++-------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/modules/zone/back/methods/zone/deleteZone.js b/modules/zone/back/methods/zone/deleteZone.js index 85f0eb6f6..4b857e30a 100644 --- a/modules/zone/back/methods/zone/deleteZone.js +++ b/modules/zone/back/methods/zone/deleteZone.js @@ -6,25 +6,29 @@ module.exports = Self => { arg: 'id', type: 'number', description: 'The zone id', - http: {source: 'path'} + http: {source: 'path'}, }, returns: { type: 'object', - root: true + root: true, }, http: { - path: '/:id/deleteZone', - verb: 'POST' - } + path: `/:id/deleteZone`, + verb: 'POST', + }, }); - Self.deleteZone = async(ctx, id, options = {}) => { - const {Ticket, State, Worker, Zone} = Self.app.models; - const {userId} = ctx.req.accessToken; - const today = new Date().setHours(0, 0, 0, 0); + Self.deleteZone = async(ctx, id, options) => { + const models = Self.app.models; + const token = ctx.req.accessToken; + const userId = token.userId; + const today = Date.vnNew(); + today.setHours(0, 0, 0, 0); let tx; - const myOptions = {userId, ...options}; + const myOptions = {userId}; + + if (typeof options == 'object') Object.assign(myOptions, options); if (!myOptions.transaction) { tx = await Self.beginTransaction({}); @@ -36,30 +40,35 @@ module.exports = Self => { where: {zoneFk: id, shipped: {gte: today}}, include: { relation: 'ticketState', - scope: {fields: ['id', 'alertLevel', 'code']} - } + scope: { + fields: ['id', 'alertLevel', 'code'], + }, + }, }; const [ticketList, fixingState, worker] = await Promise.all([ - Ticket.find(filter, myOptions), - State.findOne({where: {code: 'FIXING'}}, myOptions), - Worker.findOne({where: {id: userId}}, myOptions) + models.Ticket.find(filter, myOptions), + models.State.findOne({where: {code: 'FIXING'}}, myOptions), + models.Worker.findOne({where: {id: userId}}, myOptions) ]); - await Ticket.rawSql('UPDATE ticket SET zoneFk = NULL WHERE zoneFk = ?', [id], myOptions); + await models.Ticket.rawSql( + 'UPDATE ticket SET zoneFk = NULL WHERE zoneFk = ?', + [id], + myOptions + ); const updatePromises = ticketList.map(ticket => { if (ticket.ticketState().alertLevel === 0) { - return Ticket.state(ctx, { + return models.Ticket.state(ctx, { ticketFk: ticket.id, stateFk: fixingState.id, userFk: worker.id }, myOptions); } }); - await Promise.all(updatePromises); - await Zone.destroyById(id, myOptions); + await models.Zone.destroyById(id, myOptions); if (tx) await tx.commit();