salix/modules/ticket/back/methods/sale-tracking/mark.js

108 lines
3.1 KiB
JavaScript

const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('mark', {
description: 'Insert an itemShelvingSale and Modify a saleTracking record',
accessType: 'WRITE',
accepts: [
{
arg: 'saleFk',
type: 'number',
required: true
},
{
arg: 'originalQuantity',
type: 'number',
required: true
},
{
arg: 'code',
type: 'string',
required: true
},
{
arg: 'isChecked',
type: 'boolean',
required: true
},
{
arg: 'buyFk',
type: 'number',
required: true
},
{
arg: 'isScanned',
type: 'boolean',
},
{
arg: 'quantity',
type: 'number',
required: true
},
{
arg: 'itemShelvingFk',
type: 'number',
required: true
}
],
http: {
path: `/mark`,
verb: 'POST'
}
});
Self.mark = async(ctx, saleFk, originalQuantity, code, isChecked, buyFk, isScanned, quantity, itemShelvingFk, options) => {
const userId = ctx.req.accessToken.userId;
const models = Self.app.models;
const $t = ctx.req.__;
const myOptions = {};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
await models.ItemShelvingSale.create({
itemShelvingFk,
saleFk,
quantity,
userFk: userId
}, myOptions);
const itemShelving = await models.ItemShelving.findById(itemShelvingFk, myOptions);
await itemShelving.updateAttributes({visible: itemShelving.visible - quantity}, myOptions);
await Self.updateAll(
{saleFk},
{isChecked: true},
myOptions
);
await Self.updateTracking(ctx, saleFk, originalQuantity, code, isChecked, null, isScanned, myOptions);
try {
const buy = await models.Buy.findById(buyFk, myOptions);
if (buy.itemOriginalFk) await models.SaleBuy.create({saleFk, buyFk}, myOptions);
} catch (e) {
throw new UserError($t('The sale can not be tracked'));
}
if (tx) await tx.commit();
} catch (e) {
if (e.message == $t('The sale can not be tracked')) {
if (tx) tx.commit();
throw e;
}
if (tx) await tx.rollback();
throw new UserError($t('The line could not be marked'));
}
};
};