7565-testToMaster #2567
|
@ -10,8 +10,8 @@ module.exports = Self => {
|
||||||
http: {source: 'path'}
|
http: {source: 'path'}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
arg: 'itemId',
|
arg: 'barcode',
|
||||||
type: 'number',
|
type: 'any',
|
||||||
required: true
|
required: true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -29,7 +29,7 @@ module.exports = Self => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.addSale = async(ctx, id, itemId, quantity, options) => {
|
Self.addSale = async(ctx, id, barcode, quantity, options) => {
|
||||||
const $t = ctx.req.__; // $translate
|
const $t = ctx.req.__; // $translate
|
||||||
const models = Self.app.models;
|
const models = Self.app.models;
|
||||||
const myOptions = {userId: ctx.req.accessToken.userId};
|
const myOptions = {userId: ctx.req.accessToken.userId};
|
||||||
|
@ -46,7 +46,9 @@ module.exports = Self => {
|
||||||
try {
|
try {
|
||||||
await models.Ticket.isEditableOrThrow(ctx, id, myOptions);
|
await models.Ticket.isEditableOrThrow(ctx, id, myOptions);
|
||||||
|
|
||||||
|
const itemId = await models.ItemBarcode.toItem(barcode, myOptions);
|
||||||
const item = await models.Item.findById(itemId, null, myOptions);
|
const item = await models.Item.findById(itemId, null, myOptions);
|
||||||
|
|
||||||
const ticket = await models.Ticket.findById(id, {
|
const ticket = await models.Ticket.findById(id, {
|
||||||
include: {
|
include: {
|
||||||
relation: 'client',
|
relation: 'client',
|
||||||
|
|
|
@ -1,56 +0,0 @@
|
||||||
const UserError = require('vn-loopback/util/user-error');
|
|
||||||
module.exports = Self => {
|
|
||||||
Self.remoteMethodCtx('addSaleByCode', {
|
|
||||||
description: 'Add a collection',
|
|
||||||
accessType: 'WRITE',
|
|
||||||
accepts: [
|
|
||||||
{
|
|
||||||
arg: 'barcode',
|
|
||||||
type: 'string',
|
|
||||||
required: true
|
|
||||||
}, {
|
|
||||||
arg: 'quantity',
|
|
||||||
type: 'number',
|
|
||||||
required: true
|
|
||||||
}, {
|
|
||||||
arg: 'ticketFk',
|
|
||||||
type: 'number',
|
|
||||||
required: true
|
|
||||||
}, {
|
|
||||||
arg: 'warehouseFk',
|
|
||||||
type: 'number',
|
|
||||||
required: true
|
|
||||||
},
|
|
||||||
|
|
||||||
],
|
|
||||||
http: {
|
|
||||||
path: `/addSaleByCode`,
|
|
||||||
verb: 'POST'
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
Self.addSaleByCode = async(ctx, barcode, quantity, ticketFk, warehouseFk, options) => {
|
|
||||||
const myOptions = {userId: ctx.req.accessToken.userId};
|
|
||||||
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(?,?)', [barcode, warehouseFk], myOptions);
|
|
||||||
if (!item?.available) throw new UserError('We do not have availability for the selected item');
|
|
||||||
|
|
||||||
await Self.rawSql('CALL vn.collection_addItem(?, ?, ?)', [item.id, quantity, ticketFk], myOptions);
|
|
||||||
|
|
||||||
if (tx) await tx.commit();
|
|
||||||
} catch (e) {
|
|
||||||
if (tx) await tx.rollback();
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
};
|
|
|
@ -1,39 +0,0 @@
|
||||||
const {models} = require('vn-loopback/server/server');
|
|
||||||
const LoopBackContext = require('loopback-context');
|
|
||||||
|
|
||||||
describe('Ticket addSaleByCode()', () => {
|
|
||||||
const quantity = 3;
|
|
||||||
const ticketFk = 13;
|
|
||||||
const warehouseFk = 1;
|
|
||||||
beforeAll(async() => {
|
|
||||||
activeCtx = {
|
|
||||||
req: {
|
|
||||||
accessToken: {userId: 9},
|
|
||||||
headers: {origin: 'http://localhost'},
|
|
||||||
__: value => value
|
|
||||||
}
|
|
||||||
};
|
|
||||||
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
|
|
||||||
active: activeCtx
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should add a new sale', async() => {
|
|
||||||
const tx = await models.Ticket.beginTransaction({});
|
|
||||||
|
|
||||||
try {
|
|
||||||
const options = {transaction: tx};
|
|
||||||
const code = '1111111111';
|
|
||||||
|
|
||||||
const salesBefore = await models.Sale.find(null, options);
|
|
||||||
await models.Ticket.addSaleByCode(activeCtx, code, quantity, ticketFk, warehouseFk, options);
|
|
||||||
const salesAfter = await models.Sale.find(null, options);
|
|
||||||
|
|
||||||
expect(salesAfter.length).toEqual(salesBefore.length + 1);
|
|
||||||
await tx.rollback();
|
|
||||||
} catch (e) {
|
|
||||||
await tx.rollback();
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -46,6 +46,5 @@ module.exports = function(Self) {
|
||||||
require('../methods/ticket/invoiceTicketsAndPdf')(Self);
|
require('../methods/ticket/invoiceTicketsAndPdf')(Self);
|
||||||
require('../methods/ticket/docuwareDownload')(Self);
|
require('../methods/ticket/docuwareDownload')(Self);
|
||||||
require('../methods/ticket/myLastModified')(Self);
|
require('../methods/ticket/myLastModified')(Self);
|
||||||
require('../methods/ticket/addSaleByCode')(Self);
|
|
||||||
require('../methods/ticket/clone')(Self);
|
require('../methods/ticket/clone')(Self);
|
||||||
};
|
};
|
||||||
|
|
|
@ -476,7 +476,7 @@ class Controller extends Section {
|
||||||
*/
|
*/
|
||||||
addSale(sale) {
|
addSale(sale) {
|
||||||
const data = {
|
const data = {
|
||||||
itemId: sale.itemFk,
|
barcode: sale.itemFk,
|
||||||
quantity: sale.quantity
|
quantity: sale.quantity
|
||||||
};
|
};
|
||||||
const query = `tickets/${this.ticket.id}/addSale`;
|
const query = `tickets/${this.ticket.id}/addSale`;
|
||||||
|
|
|
@ -659,7 +659,7 @@ describe('Ticket', () => {
|
||||||
jest.spyOn(controller, 'resetChanges').mockReturnThis();
|
jest.spyOn(controller, 'resetChanges').mockReturnThis();
|
||||||
|
|
||||||
const newSale = {itemFk: 4, quantity: 10};
|
const newSale = {itemFk: 4, quantity: 10};
|
||||||
const expectedParams = {itemId: 4, quantity: 10};
|
const expectedParams = {barcode: 4, quantity: 10};
|
||||||
const expectedResult = {
|
const expectedResult = {
|
||||||
id: 30,
|
id: 30,
|
||||||
quantity: 10,
|
quantity: 10,
|
||||||
|
|
Loading…
Reference in New Issue