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-06-28 13:25:09 +00:00
|
|
|
module.exports = Self => {
|
2021-05-26 07:14:18 +00:00
|
|
|
Self.remoteMethodCtx('updateQuantity', {
|
2018-06-28 13:25:09 +00:00
|
|
|
description: 'Changes the quantity of a sale',
|
2019-05-22 10:35:24 +00:00
|
|
|
accessType: 'WRITE',
|
2018-06-28 13:25:09 +00:00
|
|
|
accepts: [{
|
|
|
|
arg: 'id',
|
|
|
|
type: 'number',
|
|
|
|
required: true,
|
|
|
|
description: 'sale ID',
|
|
|
|
http: {source: 'path'}
|
|
|
|
}, {
|
|
|
|
arg: 'quantity',
|
|
|
|
type: 'number',
|
|
|
|
required: true,
|
|
|
|
description: 'newQuantity'
|
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
type: 'string',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/:id/updateQuantity`,
|
|
|
|
verb: 'post'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
Self.updateQuantity = async(ctx, id, newQuantity, options) => {
|
2021-05-26 07:14:18 +00:00
|
|
|
const models = Self.app.models;
|
2021-09-29 06:27:18 +00:00
|
|
|
const $t = ctx.req.__; // $translate
|
|
|
|
const myOptions = {};
|
|
|
|
let tx;
|
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
|
|
|
if (!myOptions.transaction) {
|
|
|
|
tx = await Self.beginTransaction({});
|
|
|
|
myOptions.transaction = tx;
|
|
|
|
}
|
2021-05-26 07:14:18 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
try {
|
2023-01-31 08:04:45 +00:00
|
|
|
await models.Sale.canEdit(ctx, [id], myOptions);
|
2021-05-26 07:14:18 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
const filter = {
|
|
|
|
include: {
|
|
|
|
relation: 'ticket',
|
|
|
|
scope: {
|
|
|
|
include: {
|
|
|
|
relation: 'client',
|
|
|
|
scope: {
|
|
|
|
include: {
|
|
|
|
relation: 'salesPersonUser',
|
|
|
|
scope: {
|
|
|
|
fields: ['id', 'name']
|
|
|
|
}
|
2021-07-29 15:20:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-29 06:27:18 +00:00
|
|
|
};
|
2021-07-29 15:20:57 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
const sale = await models.Sale.findById(id, filter, myOptions);
|
2021-07-29 15:20:57 +00:00
|
|
|
|
2023-05-05 11:58:11 +00:00
|
|
|
const isRoleAdvanced = await models.ACL.checkAccessAcl(ctx, 'Ticket', 'isRoleAdvanced', '*');
|
2022-10-05 13:13:36 +00:00
|
|
|
if (newQuantity > sale.quantity && !isRoleAdvanced)
|
2021-09-29 06:27:18 +00:00
|
|
|
throw new UserError('The new quantity should be smaller than the old one');
|
2018-06-28 13:25:09 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
const oldQuantity = sale.quantity;
|
|
|
|
const result = await sale.updateAttributes({quantity: newQuantity}, myOptions);
|
2021-07-29 15:20:57 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
const salesPerson = sale.ticket().client().salesPersonUser();
|
|
|
|
if (salesPerson) {
|
|
|
|
const origin = ctx.req.headers.origin;
|
|
|
|
const message = $t('Changed sale quantity', {
|
|
|
|
ticketId: sale.ticket().id,
|
|
|
|
itemId: sale.itemFk,
|
|
|
|
concept: sale.concept,
|
|
|
|
oldQuantity: oldQuantity,
|
|
|
|
newQuantity: newQuantity,
|
|
|
|
ticketUrl: `${origin}/#!/ticket/${sale.ticket().id}/sale`,
|
|
|
|
itemUrl: `${origin}/#!/item/${sale.itemFk}/summary`
|
|
|
|
});
|
|
|
|
await models.Chat.sendCheckingPresence(ctx, salesPerson.id, message, myOptions);
|
|
|
|
}
|
2021-07-29 15:20:57 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
if (tx) await tx.commit();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
} catch (error) {
|
|
|
|
if (tx) await tx.rollback();
|
|
|
|
throw error;
|
|
|
|
}
|
2018-06-28 13:25:09 +00:00
|
|
|
};
|
|
|
|
};
|