80 lines
2.3 KiB
JavaScript
80 lines
2.3 KiB
JavaScript
let UserError = require('vn-loopback/util/user-error');
|
|
|
|
module.exports = Self => {
|
|
Self.remoteMethodCtx('deleteSales', {
|
|
description: 'Deletes the selected sales',
|
|
accessType: 'WRITE',
|
|
accepts: [{
|
|
arg: 'sales',
|
|
type: ['object'],
|
|
required: true,
|
|
description: 'The sales to remove'
|
|
},
|
|
{
|
|
arg: 'ticketId',
|
|
type: 'number',
|
|
required: true,
|
|
description: 'The ticket id'
|
|
}],
|
|
returns: {
|
|
type: ['object'],
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/deleteSales`,
|
|
verb: 'POST'
|
|
}
|
|
});
|
|
|
|
Self.deleteSales = async(ctx, sales, ticketId) => {
|
|
const $t = ctx.req.__; // $translate
|
|
const models = Self.app.models;
|
|
|
|
const canEditSales = await models.Sale.canEdit(ctx, sales);
|
|
|
|
const ticket = await models.Ticket.findById(ticketId, {
|
|
include: {
|
|
relation: 'client',
|
|
scope: {
|
|
include: {
|
|
relation: 'salesPersonUser',
|
|
scope: {
|
|
fields: ['id', 'name']
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
const isTicketEditable = await models.Ticket.isEditable(ctx, ticketId);
|
|
if (!isTicketEditable)
|
|
throw new UserError(`The sales of this ticket can't be modified`);
|
|
|
|
if (!canEditSales)
|
|
throw new UserError(`Sale(s) blocked, please contact production`);
|
|
|
|
const promises = [];
|
|
let deletions = '';
|
|
for (let sale of sales) {
|
|
const deletedSale = models.Sale.destroyById(sale.id);
|
|
deletions += `\r\n-${sale.itemFk}: ${sale.concept} (${sale.quantity})`;
|
|
|
|
promises.push(deletedSale);
|
|
}
|
|
|
|
const salesPerson = ticket.client().salesPersonUser();
|
|
if (salesPerson) {
|
|
const origin = ctx.req.headers.origin;
|
|
|
|
const message = $t('Deleted sales from ticket', {
|
|
ticketId: ticketId,
|
|
ticketUrl: `${origin}/#!/ticket/${ticketId}/sale`,
|
|
deletions: deletions
|
|
});
|
|
await models.Chat.sendCheckingPresence(ctx, salesPerson.id, message);
|
|
}
|
|
|
|
return Promise.all(promises);
|
|
};
|
|
};
|