86 lines
2.6 KiB
JavaScript
86 lines
2.6 KiB
JavaScript
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,
|
|
description: 'The request observation',
|
|
}, {
|
|
arg: 'quantity',
|
|
type: 'Integer',
|
|
required: true,
|
|
description: 'The request observation',
|
|
}],
|
|
returns: {
|
|
type: 'Object',
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/:id/confirm`,
|
|
verb: 'post'
|
|
}
|
|
});
|
|
|
|
Self.confirm = async ctx => {
|
|
let transaction = await Self.beginTransaction({});
|
|
let options = {transaction: transaction};
|
|
|
|
try {
|
|
let item = await Self.app.models.Item.findById(ctx.args.itemFk);
|
|
if (!item)
|
|
throw new UserError(`That item doesn't exists`);
|
|
|
|
let request = await Self.app.models.TicketRequest.findById(ctx.args.id, {
|
|
include: {relation: 'ticket'}
|
|
});
|
|
|
|
let query = `CALL vn.getItemVisibleAvailable(?,?,?,?)`;
|
|
|
|
let params = [
|
|
ctx.args.itemFk,
|
|
request.ticket().warehouseFk,
|
|
request.ticket().shipped,
|
|
false
|
|
];
|
|
|
|
let [res] = await Self.rawSql(query, params);
|
|
let available = res[0].available;
|
|
|
|
if (!available)
|
|
throw new UserError(`That item is not available on that day`);
|
|
|
|
|
|
if (request.saleFk) {
|
|
let sale = await Self.app.models.Sale.findById(request.saleFk);
|
|
sale.updateAttributes({itemFk: ctx.args.itemFk, quantity: ctx.args.quantity, description: item.description}, options);
|
|
} else {
|
|
params = {
|
|
ticketFk: request.ticketFk,
|
|
itemFk: ctx.args.itemFk,
|
|
quantity: ctx.args.quantity
|
|
};
|
|
sale = await Self.app.models.Sale.create(params, options);
|
|
request.updateAttributes({saleFk: sale.id, itemFk: sale.itemFk}, options);
|
|
}
|
|
|
|
query = `CALL vn.ticketCalculateSale(?)`;
|
|
params = [sale.id];
|
|
await Self.rawSql(query, params, options);
|
|
|
|
await transaction.commit();
|
|
} catch (error) {
|
|
await transaction.rollback();
|
|
throw error;
|
|
}
|
|
};
|
|
};
|