This commit is contained in:
parent
acf47468c0
commit
afa34c6296
|
@ -0,0 +1,71 @@
|
|||
module.exports = Self => {
|
||||
Self.remoteMethod('makeMulti', {
|
||||
description: 'Add a record or update it if it already exists.',
|
||||
accessType: 'WRITE',
|
||||
accepts: [{
|
||||
arg: 'shelvingFk',
|
||||
type: 'string',
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
arg: 'items',
|
||||
type: ['number'],
|
||||
required: true,
|
||||
description: 'array of item foreign keys'
|
||||
},
|
||||
{
|
||||
arg: 'warehouseFk',
|
||||
type: 'number',
|
||||
required: true
|
||||
}],
|
||||
|
||||
http: {
|
||||
path: `/makeMulti`,
|
||||
verb: 'POST'
|
||||
}
|
||||
});
|
||||
|
||||
Self.makeMulti = async(shelvingFk, items, warehouseFk, options) => {
|
||||
const myOptions = {};
|
||||
let tx;
|
||||
|
||||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
|
||||
if (!myOptions.transaction) {
|
||||
tx = await Self.beginTransaction({});
|
||||
myOptions.transaction = tx;
|
||||
}
|
||||
|
||||
const discardItems = [];
|
||||
|
||||
try {
|
||||
for (let item of items) {
|
||||
if (!discardItems.includes(item)) {
|
||||
let quantity = items.reduce((acc, cur) => {
|
||||
return acc + (cur === item ? 1 : 0);
|
||||
}, 0);
|
||||
discardItems.push(item);
|
||||
|
||||
const [result] = await Self.rawSql('SELECT vn.itemPacking(?, ?) itemPacking',
|
||||
[item, warehouseFk], myOptions);
|
||||
let packing;
|
||||
|
||||
if (result) packing = result.itemPacking;
|
||||
if (!packing) packing = 1;
|
||||
|
||||
quantity = quantity * packing;
|
||||
|
||||
await Self.rawSql('CALL vn.itemShelving_add(?, ?, ?, NULL, NULL, ?, ?)',
|
||||
[shelvingFk, item, quantity, packing, warehouseFk], myOptions
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (tx) await tx.commit();
|
||||
} catch (e) {
|
||||
if (tx) await tx.rollback();
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
};
|
|
@ -0,0 +1,54 @@
|
|||
const {models} = require('vn-loopback/server/server');
|
||||
|
||||
describe('ItemShelving makeMulti()', () => {
|
||||
const warehouseFk = 1;
|
||||
|
||||
beforeAll(async() => {
|
||||
ctx = {
|
||||
accessToken: {userId: 9},
|
||||
req: {
|
||||
headers: {origin: 'http://localhost'},
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
it('should add two new records', async() => {
|
||||
const shelvingFk = 'ZPP';
|
||||
const items = [1, 1, 1, 2];
|
||||
const tx = await models.ItemShelving.beginTransaction({});
|
||||
const options = {transaction: tx};
|
||||
|
||||
try {
|
||||
await models.ItemShelving.makeMulti(shelvingFk, items, warehouseFk, options);
|
||||
const itemShelvings = await models.ItemShelving.find({where: {shelvingFk}}, options);
|
||||
|
||||
expect(itemShelvings.length).toEqual(2);
|
||||
await tx.rollback();
|
||||
} catch (e) {
|
||||
await tx.rollback();
|
||||
throw e;
|
||||
}
|
||||
});
|
||||
|
||||
it('should update the visible items', async() => {
|
||||
const shelvingFk = 'GVC';
|
||||
const items = [2, 2];
|
||||
const tx = await models.ItemShelving.beginTransaction({});
|
||||
const options = {transaction: tx};
|
||||
try {
|
||||
const {visible: itemsBefore} = await models.ItemShelving.findOne({
|
||||
where: {shelvingFk, itemFk: items[0]}
|
||||
}, options);
|
||||
await models.ItemShelving.makeMulti(shelvingFk, items, warehouseFk, options);
|
||||
const {visible: itemsAfter} = await models.ItemShelving.findOne({
|
||||
where: {shelvingFk, itemFk: items[0]}
|
||||
}, options);
|
||||
|
||||
expect(itemsAfter).toEqual(itemsBefore + 2);
|
||||
await tx.rollback();
|
||||
} catch (e) {
|
||||
await tx.rollback();
|
||||
throw e;
|
||||
}
|
||||
});
|
||||
});
|
|
@ -1,4 +1,5 @@
|
|||
module.exports = Self => {
|
||||
require('../methods/item-shelving/deleteItemShelvings')(Self);
|
||||
require('../methods/item-shelving/makeMulti')(Self);
|
||||
require('../methods/item-shelving/getInventory')(Self);
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue