67 lines
2.2 KiB
JavaScript
67 lines
2.2 KiB
JavaScript
const dateUtil = require('vn-loopback/util/date');
|
|
|
|
module.exports = Self => {
|
|
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: {
|
|
path: `/merge`,
|
|
verb: 'POST'
|
|
}
|
|
});
|
|
|
|
Self.merge = async(ctx, tickets, options) => {
|
|
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;
|
|
}
|
|
|
|
try {
|
|
for (let ticket of tickets) {
|
|
const originFullPath = `${url}ticket/${ticket.originId}/summary`;
|
|
const destinationFullPath = `${url}ticket/${ticket.destinationId}/summary`;
|
|
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
|
|
});
|
|
if (!ticket.originId || !ticket.destinationId) continue;
|
|
|
|
await models.Sale.updateAll({ticketFk: ticket.originId}, {ticketFk: ticket.destinationId}, myOptions);
|
|
if (await models.Ticket.setDeleted(ctx, ticket.originId, myOptions))
|
|
await models.Chat.sendCheckingPresence(ctx, ticket.workerFk, message);
|
|
}
|
|
if (tx)
|
|
await tx.commit();
|
|
} catch (e) {
|
|
if (tx) await tx.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|