From 10c7aa70d6fe11767ed91b547fc277e16b72f0ab Mon Sep 17 00:00:00 2001 From: carlosjr Date: Mon, 5 Jul 2021 12:00:49 +0200 Subject: [PATCH] claim module transactions for filter, summary and isEditable --- modules/claim/back/methods/claim/filter.js | 22 +++--- .../claim/back/methods/claim/getSummary.js | 25 ++++--- .../claim/back/methods/claim/isEditable.js | 12 +++- .../back/methods/claim/specs/filter.spec.js | 57 ++++++++++++---- .../methods/claim/specs/getSummary.spec.js | 23 +++++-- .../methods/claim/specs/isEditable.spec.js | 68 +++++++++++++++---- 6 files changed, 155 insertions(+), 52 deletions(-) diff --git a/modules/claim/back/methods/claim/filter.js b/modules/claim/back/methods/claim/filter.js index 50d20410b..16f23433b 100644 --- a/modules/claim/back/methods/claim/filter.js +++ b/modules/claim/back/methods/claim/filter.js @@ -79,10 +79,14 @@ module.exports = Self => { } }); - Self.filter = async(ctx, filter) => { - let conn = Self.dataSource.connector; + Self.filter = async(ctx, filter, options) => { + const conn = Self.dataSource.connector; + const myOptions = {}; - let where = buildFilter(ctx.args, (param, value) => { + if (typeof options == 'object') + Object.assign(myOptions, options); + + const where = buildFilter(ctx.args, (param, value) => { switch (param) { case 'search': return /^\d+$/.test(value) @@ -111,10 +115,9 @@ module.exports = Self => { filter = mergeFilters(ctx.args.filter, {where}); - let stmts = []; - let stmt; + const stmts = []; - stmt = new ParameterizedSQL( + const stmt = new ParameterizedSQL( `SELECT cl.id, c.name, cl.clientFk, cl.workerFk, u.name AS userName, cs.description, cl.created FROM claim cl LEFT JOIN client c ON c.id = cl.clientFk @@ -124,10 +127,11 @@ module.exports = Self => { ); stmt.merge(conn.makeSuffix(filter)); - let itemsIndex = stmts.push(stmt) - 1; + const itemsIndex = stmts.push(stmt) - 1; + + const sql = ParameterizedSQL.join(stmts, ';'); + const result = await conn.executeStmt(sql, myOptions); - let sql = ParameterizedSQL.join(stmts, ';'); - let result = await conn.executeStmt(sql); return itemsIndex === 0 ? result : result[itemsIndex]; }; }; diff --git a/modules/claim/back/methods/claim/getSummary.js b/modules/claim/back/methods/claim/getSummary.js index 9b04d29a9..d6722d11f 100644 --- a/modules/claim/back/methods/claim/getSummary.js +++ b/modules/claim/back/methods/claim/getSummary.js @@ -19,12 +19,16 @@ module.exports = Self => { } }); - Self.getSummary = async id => { - let promises = []; - let summary = {}; + Self.getSummary = async(id, options) => { + const myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + const promises = []; + const summary = {}; // Claim - let filter = { where: {id: id}, include: [ @@ -61,13 +65,14 @@ module.exports = Self => { ] }; - promises.push(Self.app.models.Claim.find(filter)); + promises.push(Self.app.models.Claim.find(filter, myOptions)); // Claim detail filter = { where: {claimFk: id}, include: [ - {relation: 'sale', + { + relation: 'sale', scope: { fields: ['concept', 'ticketFk', 'price', 'quantity', 'discount', 'itemFk'], include: { @@ -77,7 +82,7 @@ module.exports = Self => { } ] }; - promises.push(Self.app.models.ClaimBeginning.find(filter)); + promises.push(Self.app.models.ClaimBeginning.find(filter, myOptions)); // Claim developments filter = { @@ -109,7 +114,7 @@ module.exports = Self => { } ] }; - promises.push(Self.app.models.ClaimDevelopment.find(filter)); + promises.push(Self.app.models.ClaimDevelopment.find(filter, myOptions)); // Claim action filter = { @@ -126,9 +131,9 @@ module.exports = Self => { {relation: 'claimBeggining'} ] }; - promises.push(Self.app.models.ClaimEnd.find(filter)); + promises.push(Self.app.models.ClaimEnd.find(filter, myOptions)); - let res = await Promise.all(promises); + const res = await Promise.all(promises, myOptions); [summary.claim] = res[0]; summary.salesClaimed = res[1]; diff --git a/modules/claim/back/methods/claim/isEditable.js b/modules/claim/back/methods/claim/isEditable.js index ce68153b5..cd14d70c7 100644 --- a/modules/claim/back/methods/claim/isEditable.js +++ b/modules/claim/back/methods/claim/isEditable.js @@ -19,15 +19,21 @@ module.exports = Self => { } }); - Self.isEditable = async(ctx, id) => { + Self.isEditable = async(ctx, id, options) => { const userId = ctx.req.accessToken.userId; - const isClaimManager = await Self.app.models.Account.hasRole(userId, 'claimManager'); + const myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + const isClaimManager = await Self.app.models.Account.hasRole(userId, 'claimManager', myOptions); + const claim = await Self.app.models.Claim.findById(id, { fields: ['claimStateFk'], include: [{ relation: 'claimState' }] - }); + }, myOptions); const isClaimResolved = claim && claim.claimState().code == 'resolved'; diff --git a/modules/claim/back/methods/claim/specs/filter.spec.js b/modules/claim/back/methods/claim/specs/filter.spec.js index 187fffaa4..b26afe8c4 100644 --- a/modules/claim/back/methods/claim/specs/filter.spec.js +++ b/modules/claim/back/methods/claim/specs/filter.spec.js @@ -2,26 +2,59 @@ const app = require('vn-loopback/server/server'); describe('claim filter()', () => { it('should return 1 result filtering by id', async() => { - let result = await app.models.Claim.filter({args: {filter: {}, search: 1}}); + const tx = await app.models.Claim.beginTransaction({}); - expect(result.length).toEqual(1); - expect(result[0].id).toEqual(1); + try { + const options = {transaction: tx}; + + const result = await app.models.Claim.filter({args: {filter: {}, search: 1}}, null, options); + + expect(result.length).toEqual(1); + expect(result[0].id).toEqual(1); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should return 1 result filtering by string', async() => { - let result = await app.models.Claim.filter({args: {filter: {}, search: 'Tony Stark'}}); + const tx = await app.models.Claim.beginTransaction({}); - expect(result.length).toEqual(1); - expect(result[0].id).toEqual(4); + try { + const options = {transaction: tx}; + + const result = await app.models.Claim.filter({args: {filter: {}, search: 'Tony Stark'}}, null, options); + + expect(result.length).toEqual(1); + expect(result[0].id).toEqual(4); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should return 4 results filtering by worker id', async() => { - let result = await app.models.Claim.filter({args: {filter: {}, workerFk: 18}}); + const tx = await app.models.Claim.beginTransaction({}); - expect(result.length).toEqual(4); - expect(result[0].id).toEqual(1); - expect(result[1].id).toEqual(2); - expect(result[2].id).toEqual(3); - expect(result[3].id).toEqual(4); + try { + const options = {transaction: tx}; + + const result = await app.models.Claim.filter({args: {filter: {}, workerFk: 18}}, null, options); + + expect(result.length).toEqual(4); + expect(result[0].id).toEqual(1); + expect(result[1].id).toEqual(2); + expect(result[2].id).toEqual(3); + expect(result[3].id).toEqual(4); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); }); diff --git a/modules/claim/back/methods/claim/specs/getSummary.spec.js b/modules/claim/back/methods/claim/specs/getSummary.spec.js index 445903377..541f42cfb 100644 --- a/modules/claim/back/methods/claim/specs/getSummary.spec.js +++ b/modules/claim/back/methods/claim/specs/getSummary.spec.js @@ -2,12 +2,23 @@ const app = require('vn-loopback/server/server'); describe('claim getSummary()', () => { it('should return summary with claim, salesClaimed, developments and actions defined ', async() => { - let result = await app.models.Claim.getSummary(1); - let keys = Object.keys(result); + const tx = await app.models.Claim.beginTransaction({}); - expect(keys).toContain('claim'); - expect(keys).toContain('salesClaimed'); - expect(keys).toContain('developments'); - expect(keys).toContain('actions'); + try { + const options = {transaction: tx}; + + const result = await app.models.Claim.getSummary(1, options); + const keys = Object.keys(result); + + expect(keys).toContain('claim'); + expect(keys).toContain('salesClaimed'); + expect(keys).toContain('developments'); + expect(keys).toContain('actions'); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); }); diff --git a/modules/claim/back/methods/claim/specs/isEditable.spec.js b/modules/claim/back/methods/claim/specs/isEditable.spec.js index 19436e16f..3afea7843 100644 --- a/modules/claim/back/methods/claim/specs/isEditable.spec.js +++ b/modules/claim/back/methods/claim/specs/isEditable.spec.js @@ -4,30 +4,74 @@ describe('claim isEditable()', () => { const salesPerdonId = 18; const claimManagerId = 72; it('should return false if the given claim does not exist', async() => { - let ctx = {req: {accessToken: {userId: claimManagerId}}}; - let result = await app.models.Claim.isEditable(ctx, 99999); + const tx = await app.models.Claim.beginTransaction({}); - expect(result).toEqual(false); + try { + const options = {transaction: tx}; + + const ctx = {req: {accessToken: {userId: claimManagerId}}}; + const result = await app.models.Claim.isEditable(ctx, 99999, options); + + expect(result).toEqual(false); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should not be able to edit a resolved claim for a salesPerson', async() => { - let ctx = {req: {accessToken: {userId: salesPerdonId}}}; - let result = await app.models.Claim.isEditable(ctx, 4); + const tx = await app.models.Claim.beginTransaction({}); - expect(result).toEqual(false); + try { + const options = {transaction: tx}; + + const ctx = {req: {accessToken: {userId: salesPerdonId}}}; + const result = await app.models.Claim.isEditable(ctx, 4, options); + + expect(result).toEqual(false); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should be able to edit a resolved claim for a claimManager', async() => { - let ctx = {req: {accessToken: {userId: claimManagerId}}}; - let result = await app.models.Claim.isEditable(ctx, 4); + const tx = await app.models.Claim.beginTransaction({}); - expect(result).toEqual(true); + try { + const options = {transaction: tx}; + + const ctx = {req: {accessToken: {userId: claimManagerId}}}; + const result = await app.models.Claim.isEditable(ctx, 4, options); + + expect(result).toEqual(true); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should be able to edit a claim for a claimManager', async() => { - let ctx = {req: {accessToken: {userId: salesPerdonId}}}; - let result = await app.models.Claim.isEditable(ctx, 1); + const tx = await app.models.Claim.beginTransaction({}); - expect(result).toEqual(true); + try { + const options = {transaction: tx}; + + const ctx = {req: {accessToken: {userId: salesPerdonId}}}; + const result = await app.models.Claim.isEditable(ctx, 1, options); + + expect(result).toEqual(true); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); });