fix: refs #7906 optimize method fix

This commit is contained in:
Carlos Satorres 2024-10-03 10:59:27 +02:00
parent 4284082292
commit a9497dd2db
1 changed files with 28 additions and 19 deletions

View File

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