salix/back/methods/collection/addItem.js

61 lines
1.6 KiB
JavaScript

const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('addItem', {
description: 'Add a collection',
accessType: 'WRITE',
accepts: [
{
arg: 'code',
type: 'string',
required: true
},
{
arg: 'quantity',
type: 'number',
required: true
},
{
arg: 'ticketFk',
type: 'number',
required: true
},
{
arg: 'warehouseFk',
type: 'number',
required: true
},
],
http: {
path: `/addItem`,
verb: 'POST'
},
});
Self.addItem = async(ctx, code, quantity, ticketFk, warehouseFk, options) => {
const models = Self.app.models;
const myOptions = {};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const [[item]] = await Self.rawSql('CALL vn.item_getInfo(?,?)', [code, warehouseFk]);
if (!item.available) throw new UserError('We do not have availability for the selected item');
await models.Ticket.addSale(ctx, ticketFk, item.id, quantity, myOptions);
if (tx) await tx.commit();
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};