From ff3b75267aa32fd9b2870d24849f9d6910c61e5d Mon Sep 17 00:00:00 2001 From: Carlos Jimenez Ruiz Date: Fri, 2 Oct 2020 09:39:58 +0200 Subject: [PATCH] priceDifference test fix + refactor --- gulpfile.js | 2 +- .../back/methods/ticket-request/confirm.js | 39 ++++++++----------- .../ticket-request/specs/confirm.spec.js | 31 ++++++++------- .../back/methods/ticket/priceDifference.js | 7 ++-- 4 files changed, 38 insertions(+), 41 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index 40590d7c2..9e4fe20c6 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -96,7 +96,7 @@ async function backTestOnce(done) { let options = { errorOnFail: false, config: { - random: false + random: true } }; diff --git a/modules/ticket/back/methods/ticket-request/confirm.js b/modules/ticket/back/methods/ticket-request/confirm.js index 0bc387fe8..67781c6c2 100644 --- a/modules/ticket/back/methods/ticket-request/confirm.js +++ b/modules/ticket/back/methods/ticket-request/confirm.js @@ -48,31 +48,26 @@ module.exports = Self => { include: {relation: 'ticket'} }, 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`); - if (request.saleFk) { - sale = await models.Sale.findById(request.saleFk, null, options); - await sale.updateAttributes({ - itemFk: ctx.args.itemFk, - quantity: ctx.args.quantity, - concept: item.name, - }, options); - } else { - sale = await models.Sale.create({ - ticketFk: request.ticketFk, - itemFk: ctx.args.itemFk, - quantity: ctx.args.quantity, - concept: item.name - }, options); - await request.updateAttributes({ - saleFk: sale.id, - itemFk: sale.itemFk, - isOk: true - }, options); - } + if (request.saleFk) + throw new UserError(`This request already contains a sale`); + + sale = await models.Sale.create({ + ticketFk: request.ticketFk, + itemFk: ctx.args.itemFk, + quantity: ctx.args.quantity, + concept: item.name + }, options); + await request.updateAttributes({ + saleFk: sale.id, + itemFk: sale.itemFk, + isOk: true + }, options); query = `CALL vn.sale_calculateComponent(?, NULL)`; await Self.rawSql(query, [sale.id], options); diff --git a/modules/ticket/back/methods/ticket-request/specs/confirm.spec.js b/modules/ticket/back/methods/ticket-request/specs/confirm.spec.js index 134ccca40..5d902ae5d 100644 --- a/modules/ticket/back/methods/ticket-request/specs/confirm.spec.js +++ b/modules/ticket/back/methods/ticket-request/specs/confirm.spec.js @@ -2,7 +2,6 @@ const app = require('vn-loopback/server/server'); describe('ticket-request confirm()', () => { let originalRequest; - let originalSale; let createdSaleId; let ctx = { req: { @@ -16,8 +15,13 @@ describe('ticket-request confirm()', () => { afterAll(async done => { await originalRequest.updateAttributes(originalRequest); - await originalSale.updateAttributes(originalSale); await app.models.Sale.destroyById(createdSaleId); + await app.models.Sale.destroyById(addedSale.id); + + await app.models.Item.rawSql(` + TRUNCATE TABLE cache.last_buy + `); + done(); }); @@ -56,10 +60,9 @@ describe('ticket-request confirm()', () => { 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 saleId = 11; - const itemId = 1; + const itemId = 2; const quantity = 10; ctx.args = { @@ -68,17 +71,15 @@ describe('ticket-request confirm()', () => { quantity: quantity }; - originalRequest = await app.models.TicketRequest.findById(requestId); - originalSale = await app.models.Sale.findById(saleId); + let error; - const request = await app.models.TicketRequest.findById(requestId); - await request.updateAttributes({saleFk: saleId}); - await app.models.TicketRequest.confirm(ctx); + try { + await app.models.TicketRequest.confirm(ctx); + } catch (err) { + error = err; + } - let updatedSale = await app.models.Sale.findById(saleId); - - expect(updatedSale.itemFk).toEqual(itemId); - expect(updatedSale.quantity).toEqual(quantity); + expect(error.message).toEqual(`This request already contains a sale`); }); 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 quantity = 10; + originalRequest = await app.models.TicketRequest.findById(requestId); + ctx.args = { itemFk: itemId, id: requestId, diff --git a/modules/ticket/back/methods/ticket/priceDifference.js b/modules/ticket/back/methods/ticket/priceDifference.js index a303412fe..2631481c6 100644 --- a/modules/ticket/back/methods/ticket/priceDifference.js +++ b/modules/ticket/back/methods/ticket/priceDifference.js @@ -87,15 +87,14 @@ module.exports = Self => { const map = new Map(); // Sale price component, one per sale - difComponents.forEach(difComponent => { + for (difComponent of difComponents) map.set(difComponent.saleFk, difComponent); - }); function round(value) { return Math.round(value * 100) / 100; } - salesObj.items.forEach(sale => { + for (sale of salesObj.items) { const difComponent = map.get(sale.id); if (difComponent) { @@ -110,7 +109,7 @@ module.exports = Self => { salesObj.totalUnitPrice += sale.price; salesObj.totalUnitPrice = round(salesObj.totalUnitPrice); - }); + } return salesObj; };