2018-07-03 13:00:16 +00:00
|
|
|
|
2018-12-27 11:54:16 +00:00
|
|
|
let UserError = require('vn-loopback/util/user-error');
|
2018-08-02 07:49:00 +00:00
|
|
|
|
2018-07-03 13:00:16 +00:00
|
|
|
module.exports = Self => {
|
2019-07-22 11:14:00 +00:00
|
|
|
Self.remoteMethodCtx('reserve', {
|
2018-07-03 13:00:16 +00:00
|
|
|
description: 'Change the state of a ticket',
|
2019-05-22 10:35:24 +00:00
|
|
|
accessType: 'WRITE',
|
2021-07-29 15:20:57 +00:00
|
|
|
accepts: [
|
|
|
|
{
|
|
|
|
arg: 'ticketId',
|
|
|
|
type: 'number',
|
|
|
|
required: true,
|
|
|
|
description: 'The ticket id'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'sales',
|
|
|
|
type: ['object'],
|
|
|
|
required: true,
|
|
|
|
description: 'The sale to reserve'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'reserved',
|
|
|
|
type: 'boolean',
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
],
|
2018-07-03 13:00:16 +00:00
|
|
|
returns: {
|
2021-07-29 15:20:57 +00:00
|
|
|
type: ['object'],
|
2018-07-03 13:00:16 +00:00
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/reserve`,
|
2020-07-01 12:28:05 +00:00
|
|
|
verb: 'POST'
|
2018-07-03 13:00:16 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-07-01 12:28:05 +00:00
|
|
|
Self.reserve = async(ctx, ticketId, sales, reserved) => {
|
2021-07-29 15:20:57 +00:00
|
|
|
const $t = ctx.req.__; // $translate
|
2020-07-01 12:28:05 +00:00
|
|
|
const models = Self.app.models;
|
2021-07-29 15:20:57 +00:00
|
|
|
|
2020-07-01 12:28:05 +00:00
|
|
|
const isTicketEditable = await models.Ticket.isEditable(ctx, ticketId);
|
|
|
|
if (!isTicketEditable)
|
2018-08-02 07:49:00 +00:00
|
|
|
throw new UserError(`The sales of this ticket can't be modified`);
|
2018-07-03 13:00:16 +00:00
|
|
|
|
2021-05-26 07:14:18 +00:00
|
|
|
const canEditSale = await models.Sale.canEdit(ctx, sales);
|
|
|
|
|
|
|
|
if (!canEditSale)
|
|
|
|
throw new UserError(`Sale(s) blocked, please contact production`);
|
|
|
|
|
2021-07-29 15:20:57 +00:00
|
|
|
let changesMade = '';
|
2020-07-01 12:28:05 +00:00
|
|
|
const promises = [];
|
2021-07-29 15:20:57 +00:00
|
|
|
|
2020-07-01 12:28:05 +00:00
|
|
|
for (let sale of sales) {
|
2021-07-29 15:20:57 +00:00
|
|
|
if (sale.reserved != reserved) {
|
|
|
|
const oldState = sale.reserved ? 'reserved' : 'regular';
|
|
|
|
const newState = reserved ? 'reserved' : 'regular';
|
|
|
|
|
|
|
|
const reservedSale = models.Sale.update({id: sale.id}, {reserved: reserved});
|
|
|
|
|
|
|
|
promises.push(reservedSale);
|
|
|
|
|
|
|
|
changesMade += `\r\n-${sale.itemFk}: ${sale.concept} (${sale.quantity}) ${$t('State')}: ${$t(oldState)} ➔ *${$t(newState)}*`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = await Promise.all(promises);
|
|
|
|
|
|
|
|
const ticket = await models.Ticket.findById(ticketId, {
|
|
|
|
include: {
|
|
|
|
relation: 'client',
|
|
|
|
scope: {
|
|
|
|
include: {
|
|
|
|
relation: 'salesPersonUser',
|
|
|
|
scope: {
|
|
|
|
fields: ['id', 'name']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const salesPerson = ticket.client().salesPersonUser();
|
|
|
|
if (salesPerson) {
|
|
|
|
const origin = ctx.req.headers.origin;
|
|
|
|
|
|
|
|
const message = $t('Changed sale reserved state', {
|
|
|
|
ticketId: ticketId,
|
|
|
|
ticketUrl: `${origin}/#!/ticket/${ticketId}/sale`,
|
|
|
|
changes: changesMade
|
|
|
|
});
|
|
|
|
await models.Chat.sendCheckingPresence(ctx, salesPerson.id, message);
|
2020-07-01 12:28:05 +00:00
|
|
|
}
|
|
|
|
|
2021-07-29 15:20:57 +00:00
|
|
|
return result;
|
2018-07-03 13:00:16 +00:00
|
|
|
};
|
|
|
|
};
|