From 87974445531166c19110ea884d6f3031a6d73c9b Mon Sep 17 00:00:00 2001 From: carlosjr Date: Mon, 29 Mar 2021 15:42:29 +0200 Subject: [PATCH] importTicketSales refactor + tests --- .../methods/claim-end/importTicketSales.js | 47 ++++++++++++++----- .../claim-end/specs/importTicketSales.spec.js | 37 ++++++++------- 2 files changed, 53 insertions(+), 31 deletions(-) diff --git a/modules/claim/back/methods/claim-end/importTicketSales.js b/modules/claim/back/methods/claim-end/importTicketSales.js index c7ca93197..106313f14 100644 --- a/modules/claim/back/methods/claim-end/importTicketSales.js +++ b/modules/claim/back/methods/claim-end/importTicketSales.js @@ -17,24 +17,45 @@ module.exports = Self => { } }); - Self.importTicketSales = async(ctx, params) => { + Self.importTicketSales = async(ctx, params, options) => { let models = Self.app.models; let userId = ctx.req.accessToken.userId; - let worker = await models.Worker.findOne({where: {userFk: userId}}); - let ticketSales = await models.Sale.find({ - where: {ticketFk: params.ticketFk} - }); + let tx; + let myOptions = {}; - let claimEnds = []; - ticketSales.forEach(sale => { - claimEnds.push({ - saleFk: sale.id, - claimFk: params.claimFk, - workerFk: worker.id + if (typeof options == 'object') + Object.assign(myOptions, options); + + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + try { + const worker = await models.Worker.findOne({where: {userFk: userId}}, myOptions); + + let ticketSales = await models.Sale.find({ + where: {ticketFk: params.ticketFk} + }, myOptions); + + let claimEnds = []; + ticketSales.forEach(sale => { + claimEnds.push({ + saleFk: sale.id, + claimFk: params.claimFk, + workerFk: worker.id + }); }); - }); - return await Self.create(claimEnds); + const createdClaimEnds = await Self.create(claimEnds, myOptions); + + if (tx) await tx.commit(); + + return createdClaimEnds; + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } }; }; diff --git a/modules/claim/back/methods/claim-end/specs/importTicketSales.spec.js b/modules/claim/back/methods/claim-end/specs/importTicketSales.spec.js index 3c2a3e0a6..93f11e21f 100644 --- a/modules/claim/back/methods/claim-end/specs/importTicketSales.spec.js +++ b/modules/claim/back/methods/claim-end/specs/importTicketSales.spec.js @@ -1,25 +1,26 @@ const app = require('vn-loopback/server/server'); describe('Claim importTicketSales()', () => { - let claimEnds; - - afterAll(async done => { - claimEnds.forEach(async line => { - await line.destroy(); - }); - - done(); - }); - it('should import sales to a claim actions from an specific ticket', async() => { - let ctx = {req: {accessToken: {userId: 5}}}; - claimEnds = await app.models.ClaimEnd.importTicketSales(ctx, { - claimFk: 1, - ticketFk: 1 - }); + const ctx = {req: {accessToken: {userId: 5}}}; - expect(claimEnds.length).toEqual(4); - expect(claimEnds[0].saleFk).toEqual(1); - expect(claimEnds[2].saleFk).toEqual(3); + const tx = await app.models.Entry.beginTransaction({}); + try { + const options = {transaction: tx}; + + const claimEnds = await app.models.ClaimEnd.importTicketSales(ctx, { + claimFk: 1, + ticketFk: 1 + }, options); + + expect(claimEnds.length).toEqual(4); + expect(claimEnds[0].saleFk).toEqual(1); + expect(claimEnds[2].saleFk).toEqual(3); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); });