fix: refs #7906 optimize method
This commit is contained in:
parent
617c44a178
commit
4284082292
|
@ -13,23 +13,18 @@ module.exports = Self => {
|
|||
root: true
|
||||
},
|
||||
http: {
|
||||
path: `/:id/deleteZone`,
|
||||
path: '/:id/deleteZone',
|
||||
verb: 'POST'
|
||||
}
|
||||
});
|
||||
|
||||
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);
|
||||
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);
|
||||
|
||||
let tx;
|
||||
const myOptions = {userId};
|
||||
|
||||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
const myOptions = {userId, ...options};
|
||||
|
||||
if (!myOptions.transaction) {
|
||||
tx = await Self.beginTransaction({});
|
||||
|
@ -38,38 +33,33 @@ module.exports = Self => {
|
|||
|
||||
try {
|
||||
const filter = {
|
||||
where: {
|
||||
zoneFk: id,
|
||||
shipped: {gte: today}
|
||||
},
|
||||
where: {zoneFk: id, shipped: {gte: today}},
|
||||
include: {
|
||||
relation: 'ticketState',
|
||||
scope: {
|
||||
fields: ['id', 'alertLevel', 'code']
|
||||
}
|
||||
scope: {fields: ['id', 'alertLevel', 'code']}
|
||||
}
|
||||
};
|
||||
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 [ticketList, fixingState, worker] = await Promise.all([
|
||||
Ticket.find(filter, myOptions),
|
||||
State.findOne({where: {code: 'FIXING'}}, myOptions),
|
||||
Worker.findOne({where: {id: userId}}, myOptions)
|
||||
]);
|
||||
|
||||
await models.Ticket.rawSql('UPDATE ticket SET zoneFk = NULL WHERE zoneFk = ?', [id], myOptions);
|
||||
await 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, {
|
||||
const updatePromises = ticketList.map(ticket => {
|
||||
if (ticket.ticketState().alertLevel === 0) {
|
||||
return Ticket.state(ctx, {
|
||||
ticketFk: ticket.id,
|
||||
stateFk: fixingState.id,
|
||||
userFk: worker.id
|
||||
}, myOptions));
|
||||
}, myOptions);
|
||||
}
|
||||
}
|
||||
await Promise.all(promises);
|
||||
await models.Zone.destroyById(id, myOptions);
|
||||
});
|
||||
|
||||
await Promise.all(updatePromises);
|
||||
await Zone.destroyById(id, myOptions);
|
||||
|
||||
if (tx) await tx.commit();
|
||||
|
||||
|
|
Loading…
Reference in New Issue