68 lines
2.4 KiB
JavaScript
68 lines
2.4 KiB
JavaScript
|
const LoopBackContext = require('loopback-context');
|
||
|
|
||
|
module.exports = Self => {
|
||
|
Self.remoteMethodCtx('moveTicketsFuture', {
|
||
|
description: 'Move specified tickets to the future',
|
||
|
accessType: 'WRITE',
|
||
|
accepts: [
|
||
|
{
|
||
|
arg: 'tickets',
|
||
|
type: ['object'],
|
||
|
description: 'The array of tickets',
|
||
|
required: false
|
||
|
}
|
||
|
],
|
||
|
returns: {
|
||
|
type: 'string',
|
||
|
root: true
|
||
|
},
|
||
|
http: {
|
||
|
path: `/moveTicketsFuture`,
|
||
|
verb: 'POST'
|
||
|
}
|
||
|
});
|
||
|
|
||
|
Self.moveTicketsFuture = async (ctx, tickets, options) => {
|
||
|
const loopBackContext = LoopBackContext.getCurrentContext();
|
||
|
const httpCtx = {req: loopBackContext.active};
|
||
|
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;
|
||
|
}
|
||
|
|
||
|
for (let ticket of tickets) {
|
||
|
console.log(ticket);
|
||
|
try {
|
||
|
if(!ticket.id || !ticket.ticketFuture) continue;
|
||
|
await models.Sale.updateAll({ticketFk: ticket.id}, {ticketFk: ticket.ticketFuture}, myOptions);
|
||
|
await models.Ticket.setDeleted(ctx, ticket.id, myOptions);
|
||
|
if (tx)
|
||
|
{
|
||
|
const httpRequest = httpCtx.req.http.req;
|
||
|
const $t = httpRequest.__;
|
||
|
const origin = httpRequest.headers.origin;
|
||
|
const fullPath = `${origin}/#!/ticket/${ticket.ticketFuture}/summary`;
|
||
|
const message = $t('MOVE_TICKET_CONFIRMATION', {
|
||
|
originDated: new Date(ticket.originETD).toLocaleDateString('es-ES'),
|
||
|
futureDated: new Date(ticket.destETD).toLocaleDateString('es-ES'),
|
||
|
id: ticket.id,
|
||
|
tfId: ticket.ticketFuture,
|
||
|
fullPath
|
||
|
});
|
||
|
await tx.commit();
|
||
|
await models.Chat.sendCheckingPresence(httpCtx, ticket.workerFk, message);
|
||
|
}
|
||
|
} catch (e) {
|
||
|
if (tx) await tx.rollback();
|
||
|
throw e;
|
||
|
}
|
||
|
};
|
||
|
};
|
||
|
};
|