salix/modules/ticket/back/methods/ticket/merge.js

67 lines
2.2 KiB
JavaScript
Raw Normal View History

2022-11-16 08:46:35 +00:00
const dateUtil = require('vn-loopback/util/date');
module.exports = Self => {
2022-11-08 14:56:58 +00:00
Self.remoteMethodCtx('merge', {
description: 'Merge one ticket into another',
accessType: 'WRITE',
accepts: [
{
arg: 'tickets',
type: ['object'],
description: 'The array of tickets',
required: false
}
],
returns: {
type: 'string',
root: true
},
http: {
2022-11-08 14:56:58 +00:00
path: `/merge`,
verb: 'POST'
}
});
2022-11-21 10:41:17 +00:00
Self.merge = async(ctx, tickets, options) => {
2022-11-10 14:54:00 +00:00
const httpRequest = ctx.req;
const $t = httpRequest.__;
const url = await Self.app.models.Url.getUrl();
const models = Self.app.models;
const myOptions = {};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
2022-11-08 14:56:58 +00:00
2022-11-11 14:58:11 +00:00
try {
for (let ticket of tickets) {
2023-10-16 16:24:58 +00:00
const originFullPath = `${url}ticket/${ticket.originId}/summary`;
const destinationFullPath = `${url}ticket/${ticket.destinationId}/summary`;
2022-11-21 10:41:17 +00:00
const message = $t('Ticket merged', {
originDated: dateUtil.toString(new Date(ticket.originShipped)),
destinationDated: dateUtil.toString(new Date(ticket.destinationShipped)),
originId: ticket.originId,
destinationId: ticket.destinationId,
originFullPath,
destinationFullPath
2022-11-10 14:54:00 +00:00
});
if (!ticket.originId || !ticket.destinationId) continue;
2022-12-14 11:17:12 +00:00
await models.Sale.updateAll({ticketFk: ticket.originId}, {ticketFk: ticket.destinationId}, myOptions);
2023-06-25 17:08:13 +00:00
if (await models.Ticket.setDeleted(ctx, ticket.originId, myOptions))
2023-05-17 10:41:00 +00:00
await models.Chat.sendCheckingPresence(ctx, ticket.workerFk, message);
}
2022-11-21 10:41:17 +00:00
if (tx)
await tx.commit();
2022-11-11 14:58:11 +00:00
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
2022-11-08 14:56:58 +00:00
};