salix/modules/ticket/back/methods/ticket-request/confirm.js

112 lines
3.5 KiB
JavaScript
Raw Normal View History

2019-04-05 13:20:12 +00:00
let UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('confirm', {
description: '',
accessType: 'WRITE',
accepts: [{
arg: 'id',
type: 'Integer',
required: true,
description: 'The request ID',
}, {
arg: 'itemFk',
type: 'Integer',
required: true,
2019-07-25 07:55:09 +00:00
description: 'The requested item ID',
2019-04-05 13:20:12 +00:00
}, {
arg: 'quantity',
type: 'Integer',
required: true,
2019-07-25 07:55:09 +00:00
description: 'The requested item quantity',
2019-04-05 13:20:12 +00:00
}],
returns: {
type: 'Object',
root: true
},
http: {
path: `/:id/confirm`,
verb: 'post'
}
});
Self.confirm = async ctx => {
2020-05-27 06:07:14 +00:00
const userId = ctx.req.accessToken.userId;
2019-04-11 07:38:53 +00:00
const models = Self.app.models;
2020-01-23 12:31:07 +00:00
const tx = await Self.beginTransaction({});
const $t = ctx.req.__; // $translate
2019-07-25 07:55:09 +00:00
let sale;
2019-04-05 13:20:12 +00:00
try {
let options = {transaction: tx};
2019-10-22 11:44:36 +00:00
let item = await models.Item.findById(ctx.args.itemFk, null, options);
2019-04-05 13:20:12 +00:00
if (!item)
throw new UserError(`That item doesn't exists`);
2019-04-11 07:38:53 +00:00
let request = await models.TicketRequest.findById(ctx.args.id, {
2019-04-05 13:20:12 +00:00
include: {relation: 'ticket'}
2019-10-22 11:44:36 +00:00
}, options);
2019-04-05 13:20:12 +00:00
2020-10-02 07:39:58 +00:00
const itemStock = await models.Item.getVisibleAvailable(ctx.args.itemFk, request.ticket().warehouseFk, request.ticket().shipped);
const isAvailable = itemStock.available > 0;
2019-04-05 13:20:12 +00:00
2020-10-02 07:39:58 +00:00
if (!isAvailable)
throw new UserError(`This item is not available`);
2019-04-05 13:20:12 +00:00
2020-10-02 07:39:58 +00:00
if (request.saleFk)
throw new UserError(`This request already contains a sale`);
sale = await models.Sale.create({
ticketFk: request.ticketFk,
itemFk: ctx.args.itemFk,
quantity: ctx.args.quantity,
concept: item.name
}, options);
await request.updateAttributes({
saleFk: sale.id,
itemFk: sale.itemFk,
isOk: true
}, options);
2019-04-05 13:20:12 +00:00
query = `CALL vn.sale_calculateComponent(?, NULL)`;
2019-07-25 07:55:09 +00:00
await Self.rawSql(query, [sale.id], options);
2019-04-05 13:20:12 +00:00
2020-01-23 12:31:07 +00:00
const origin = ctx.req.headers.origin;
const requesterId = request.requesterFk;
const message = $t('MESSAGE_BOUGHT_UNITS', {
quantity: sale.quantity,
concept: sale.concept,
itemId: sale.itemFk,
ticketId: sale.ticketFk,
url: `${origin}/#!/ticket/${sale.ticketFk}/summary`
});
await models.Chat.sendCheckingPresence(ctx, requesterId, message);
2019-04-11 07:38:53 +00:00
2020-05-27 06:07:14 +00:00
// loguejar
let logRecord = {
originFk: sale.ticketFk,
userFk: userId,
action: 'update',
changedModel: 'ticketRequest',
newInstance: {
destinationFk: sale.ticketFk,
2020-06-02 09:15:07 +00:00
quantity: sale.quantity,
concept: sale.concept,
itemId: sale.itemFk,
ticketId: sale.ticketFk,
2020-05-27 06:07:14 +00:00
}
};
await Self.app.models.TicketLog.create(logRecord);
await tx.commit();
2019-10-22 11:44:36 +00:00
return sale;
2019-04-05 13:20:12 +00:00
} catch (error) {
await tx.rollback();
2019-04-05 13:20:12 +00:00
throw error;
}
};
};