priceDifference test fix + refactor

This commit is contained in:
Carlos Jimenez Ruiz 2020-10-02 09:39:58 +02:00
parent eb99ce2f43
commit ff3b75267a
4 changed files with 38 additions and 41 deletions

View File

@ -96,7 +96,7 @@ async function backTestOnce(done) {
let options = { let options = {
errorOnFail: false, errorOnFail: false,
config: { config: {
random: false random: true
} }
}; };

View File

@ -48,31 +48,26 @@ module.exports = Self => {
include: {relation: 'ticket'} include: {relation: 'ticket'}
}, options); }, options);
const res = await models.Item.getVisibleAvailable(ctx.args.itemFk, request.ticket().warehouseFk, request.ticket().shipped); const itemStock = await models.Item.getVisibleAvailable(ctx.args.itemFk, request.ticket().warehouseFk, request.ticket().shipped);
const isAvailable = itemStock.available > 0;
if (res.available < 0) if (!isAvailable)
throw new UserError(`This item is not available`); throw new UserError(`This item is not available`);
if (request.saleFk) { if (request.saleFk)
sale = await models.Sale.findById(request.saleFk, null, options); throw new UserError(`This request already contains a sale`);
await sale.updateAttributes({
itemFk: ctx.args.itemFk, sale = await models.Sale.create({
quantity: ctx.args.quantity, ticketFk: request.ticketFk,
concept: item.name, itemFk: ctx.args.itemFk,
}, options); quantity: ctx.args.quantity,
} else { concept: item.name
sale = await models.Sale.create({ }, options);
ticketFk: request.ticketFk, await request.updateAttributes({
itemFk: ctx.args.itemFk, saleFk: sale.id,
quantity: ctx.args.quantity, itemFk: sale.itemFk,
concept: item.name isOk: true
}, options); }, options);
await request.updateAttributes({
saleFk: sale.id,
itemFk: sale.itemFk,
isOk: true
}, options);
}
query = `CALL vn.sale_calculateComponent(?, NULL)`; query = `CALL vn.sale_calculateComponent(?, NULL)`;
await Self.rawSql(query, [sale.id], options); await Self.rawSql(query, [sale.id], options);

View File

@ -2,7 +2,6 @@ const app = require('vn-loopback/server/server');
describe('ticket-request confirm()', () => { describe('ticket-request confirm()', () => {
let originalRequest; let originalRequest;
let originalSale;
let createdSaleId; let createdSaleId;
let ctx = { let ctx = {
req: { req: {
@ -16,8 +15,13 @@ describe('ticket-request confirm()', () => {
afterAll(async done => { afterAll(async done => {
await originalRequest.updateAttributes(originalRequest); await originalRequest.updateAttributes(originalRequest);
await originalSale.updateAttributes(originalSale);
await app.models.Sale.destroyById(createdSaleId); await app.models.Sale.destroyById(createdSaleId);
await app.models.Sale.destroyById(addedSale.id);
await app.models.Item.rawSql(`
TRUNCATE TABLE cache.last_buy
`);
done(); done();
}); });
@ -56,10 +60,9 @@ describe('ticket-request confirm()', () => {
expect(error.message).toEqual(`This item is not available`); expect(error.message).toEqual(`This item is not available`);
}); });
it(`should update the sale details if the request already contains a sale id`, async() => { it(`should throw if there's a sale id`, async() => {
const requestId = 4; const requestId = 4;
const saleId = 11; const itemId = 2;
const itemId = 1;
const quantity = 10; const quantity = 10;
ctx.args = { ctx.args = {
@ -68,17 +71,15 @@ describe('ticket-request confirm()', () => {
quantity: quantity quantity: quantity
}; };
originalRequest = await app.models.TicketRequest.findById(requestId); let error;
originalSale = await app.models.Sale.findById(saleId);
const request = await app.models.TicketRequest.findById(requestId); try {
await request.updateAttributes({saleFk: saleId}); await app.models.TicketRequest.confirm(ctx);
await app.models.TicketRequest.confirm(ctx); } catch (err) {
error = err;
}
let updatedSale = await app.models.Sale.findById(saleId); expect(error.message).toEqual(`This request already contains a sale`);
expect(updatedSale.itemFk).toEqual(itemId);
expect(updatedSale.quantity).toEqual(quantity);
}); });
it(`should create a new sale for the the request if there's no sale id`, async() => { it(`should create a new sale for the the request if there's no sale id`, async() => {
@ -86,6 +87,8 @@ describe('ticket-request confirm()', () => {
const itemId = 1; const itemId = 1;
const quantity = 10; const quantity = 10;
originalRequest = await app.models.TicketRequest.findById(requestId);
ctx.args = { ctx.args = {
itemFk: itemId, itemFk: itemId,
id: requestId, id: requestId,

View File

@ -87,15 +87,14 @@ module.exports = Self => {
const map = new Map(); const map = new Map();
// Sale price component, one per sale // Sale price component, one per sale
difComponents.forEach(difComponent => { for (difComponent of difComponents)
map.set(difComponent.saleFk, difComponent); map.set(difComponent.saleFk, difComponent);
});
function round(value) { function round(value) {
return Math.round(value * 100) / 100; return Math.round(value * 100) / 100;
} }
salesObj.items.forEach(sale => { for (sale of salesObj.items) {
const difComponent = map.get(sale.id); const difComponent = map.get(sale.id);
if (difComponent) { if (difComponent) {
@ -110,7 +109,7 @@ module.exports = Self => {
salesObj.totalUnitPrice += sale.price; salesObj.totalUnitPrice += sale.price;
salesObj.totalUnitPrice = round(salesObj.totalUnitPrice); salesObj.totalUnitPrice = round(salesObj.totalUnitPrice);
}); }
return salesObj; return salesObj;
}; };