From 13c06bf5beaff28f20ab82a976fdf5314752f384 Mon Sep 17 00:00:00 2001 From: carlosjr Date: Fri, 22 Oct 2021 17:32:10 +0200 Subject: [PATCH] refactor(monitors): added transactions to endpoints --- .../methods/sales-monitor/clientsFilter.js | 8 +- .../methods/sales-monitor/deleteOrders.js | 24 +- .../methods/sales-monitor/ordersFilter.js | 32 +- .../back/methods/sales-monitor/salesFilter.js | 18 +- .../sales-monitor/specs/clientsFilter.spec.js | 21 +- .../sales-monitor/specs/deleteOrders.spec.js | 6 +- .../sales-monitor/specs/ordersFilter.spec.js | 21 +- .../sales-monitor/specs/salesFilter.spec.js | 297 ++++++++++++------ 8 files changed, 298 insertions(+), 129 deletions(-) diff --git a/modules/monitor/back/methods/sales-monitor/clientsFilter.js b/modules/monitor/back/methods/sales-monitor/clientsFilter.js index 72b1307da..8235d5092 100644 --- a/modules/monitor/back/methods/sales-monitor/clientsFilter.js +++ b/modules/monitor/back/methods/sales-monitor/clientsFilter.js @@ -26,9 +26,13 @@ module.exports = Self => { } }); - Self.clientsFilter = async(ctx, filter) => { + Self.clientsFilter = async(ctx, filter, options) => { const userId = ctx.req.accessToken.userId; const conn = Self.dataSource.connector; + const myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); const stmt = new ParameterizedSQL(` SELECT @@ -55,6 +59,6 @@ module.exports = Self => { stmt.merge(conn.makeSuffix(filter)); - return conn.executeStmt(stmt); + return conn.executeStmt(stmt, myOptions); }; }; diff --git a/modules/monitor/back/methods/sales-monitor/deleteOrders.js b/modules/monitor/back/methods/sales-monitor/deleteOrders.js index d9e1e35f9..c35682a9a 100644 --- a/modules/monitor/back/methods/sales-monitor/deleteOrders.js +++ b/modules/monitor/back/methods/sales-monitor/deleteOrders.js @@ -20,14 +20,28 @@ module.exports = Self => { Self.deleteOrders = async(deletes = [], options) => { const models = Self.app.models; - - let myOptions = {}; + const myOptions = {}; + let tx; if (typeof options == 'object') Object.assign(myOptions, options); - return models.Order.destroyAll({ - id: {inq: deletes} - }, myOptions); + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + try { + const deletedSales = await models.Order.destroyAll({ + id: {inq: deletes} + }, myOptions); + + if (tx) await tx.commit(); + + return deletedSales; + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } }; }; diff --git a/modules/monitor/back/methods/sales-monitor/ordersFilter.js b/modules/monitor/back/methods/sales-monitor/ordersFilter.js index bc9ab3bdf..6ae4042b6 100644 --- a/modules/monitor/back/methods/sales-monitor/ordersFilter.js +++ b/modules/monitor/back/methods/sales-monitor/ordersFilter.js @@ -26,24 +26,28 @@ module.exports = Self => { } }); - Self.ordersFilter = async(ctx, filter) => { + Self.ordersFilter = async(ctx, filter, options) => { const userId = ctx.req.accessToken.userId; const conn = Self.dataSource.connector; + const myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); const stmt = new ParameterizedSQL(` SELECT - c.id AS clientFk, - c.name AS clientName, - a.nickname, - o.id, - o.date_make, - o.date_send, - o.customer_id, - COUNT(item_id) AS totalRows, - ROUND(SUM(amount * price)) * 1 AS import, - u.id AS salesPersonFk, - u.name AS salesPerson, - am.name AS agencyName + c.id AS clientFk, + c.name AS clientName, + a.nickname, + o.id, + o.date_make, + o.date_send, + o.customer_id, + COUNT(item_id) AS totalRows, + ROUND(SUM(amount * price)) * 1 AS import, + u.id AS salesPersonFk, + u.name AS salesPerson, + am.name AS agencyName FROM hedera.order o JOIN hedera.order_row orw ON o.id = orw.order_id JOIN client c ON c.id = o.customer_id @@ -63,6 +67,6 @@ module.exports = Self => { stmt.merge(conn.makeGroupBy('o.id')); stmt.merge(conn.makePagination(filter)); - return conn.executeStmt(stmt); + return conn.executeStmt(stmt, myOptions); }; }; diff --git a/modules/monitor/back/methods/sales-monitor/salesFilter.js b/modules/monitor/back/methods/sales-monitor/salesFilter.js index 5627975d8..b5de38f60 100644 --- a/modules/monitor/back/methods/sales-monitor/salesFilter.js +++ b/modules/monitor/back/methods/sales-monitor/salesFilter.js @@ -63,7 +63,7 @@ module.exports = Self => { }, { arg: 'myTeam', type: 'boolean', - description: `Whether to show only tickets for the current logged user team (For now it shows only the current user tickets)` + description: `Whether to show only tickets for the current logged user team (currently user tickets)` }, { arg: 'problems', type: 'boolean', @@ -100,11 +100,15 @@ module.exports = Self => { } }); - Self.salesFilter = async(ctx, filter) => { + Self.salesFilter = async(ctx, filter, options) => { const userId = ctx.req.accessToken.userId; const conn = Self.dataSource.connector; const models = Self.app.models; const args = ctx.args; + const myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); // Apply filter by team const teamMembersId = []; @@ -113,7 +117,7 @@ module.exports = Self => { include: { relation: 'collegues' } - }); + }, myOptions); const collegues = worker.collegues() || []; for (let collegue of collegues) teamMembersId.push(collegue.collegueFk); @@ -150,7 +154,7 @@ module.exports = Self => { filter = mergeFilters(filter, {where}); - let stmts = []; + const stmts = []; let stmt; stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.filter'); @@ -358,7 +362,7 @@ module.exports = Self => { stmt.merge(conn.makeOrderBy(filter.order)); stmt.merge(conn.makeLimit(filter)); - let ticketsIndex = stmts.push(stmt) - 1; + const ticketsIndex = stmts.push(stmt) - 1; stmts.push( `DROP TEMPORARY TABLE @@ -367,8 +371,8 @@ module.exports = Self => { tmp.sale_getProblems, tmp.risk`); - let sql = ParameterizedSQL.join(stmts, ';'); - let result = await conn.executeStmt(sql); + const sql = ParameterizedSQL.join(stmts, ';'); + const result = await conn.executeStmt(sql, myOptions); return result[ticketsIndex]; }; diff --git a/modules/monitor/back/methods/sales-monitor/specs/clientsFilter.spec.js b/modules/monitor/back/methods/sales-monitor/specs/clientsFilter.spec.js index c930b5dfb..3fcc6c91a 100644 --- a/modules/monitor/back/methods/sales-monitor/specs/clientsFilter.spec.js +++ b/modules/monitor/back/methods/sales-monitor/specs/clientsFilter.spec.js @@ -1,11 +1,22 @@ -const app = require('vn-loopback/server/server'); +const models = require('vn-loopback/server/server').models; describe('SalesMonitor clientsFilter()', () => { it('should return the clients web activity', async() => { - const ctx = {req: {accessToken: {userId: 18}}, args: {}}; - const filter = {order: 'dated DESC'}; - const result = await app.models.SalesMonitor.clientsFilter(ctx, filter); + const tx = await models.SalesMonitor.beginTransaction({}); - expect(result.length).toEqual(9); + try { + const options = {transaction: tx}; + + const ctx = {req: {accessToken: {userId: 18}}, args: {}}; + const filter = {order: 'dated DESC'}; + const result = await models.SalesMonitor.clientsFilter(ctx, filter, options); + + expect(result.length).toEqual(9); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); }); diff --git a/modules/monitor/back/methods/sales-monitor/specs/deleteOrders.spec.js b/modules/monitor/back/methods/sales-monitor/specs/deleteOrders.spec.js index 70394d082..a79f27811 100644 --- a/modules/monitor/back/methods/sales-monitor/specs/deleteOrders.spec.js +++ b/modules/monitor/back/methods/sales-monitor/specs/deleteOrders.spec.js @@ -1,14 +1,14 @@ -const app = require('vn-loopback/server/server'); +const models = require('vn-loopback/server/server').models; describe('SalesMonitor deleteOrders()', () => { it('should return the deleted orders', async() => { - const tx = await app.models.Order.beginTransaction({}); + const tx = await models.Order.beginTransaction({}); try { const options = {transaction: tx}; const deletes = [1, 2]; - const result = await app.models.SalesMonitor.deleteOrders(deletes, options); + const result = await models.SalesMonitor.deleteOrders(deletes, options); expect(result.count).toEqual(2); diff --git a/modules/monitor/back/methods/sales-monitor/specs/ordersFilter.spec.js b/modules/monitor/back/methods/sales-monitor/specs/ordersFilter.spec.js index 26894f25d..16a5514dd 100644 --- a/modules/monitor/back/methods/sales-monitor/specs/ordersFilter.spec.js +++ b/modules/monitor/back/methods/sales-monitor/specs/ordersFilter.spec.js @@ -1,11 +1,22 @@ -const app = require('vn-loopback/server/server'); +const models = require('vn-loopback/server/server').models; describe('SalesMonitor ordersFilter()', () => { it('should return the orders activity', async() => { - const ctx = {req: {accessToken: {userId: 18}}, args: {}}; - const filter = {order: 'date_make DESC'}; - const result = await app.models.SalesMonitor.ordersFilter(ctx, filter); + const tx = await models.SalesMonitor.beginTransaction({}); - expect(result.length).toEqual(12); + try { + const options = {transaction: tx}; + + const ctx = {req: {accessToken: {userId: 18}}, args: {}}; + const filter = {order: 'date_make DESC'}; + const result = await models.SalesMonitor.ordersFilter(ctx, filter, options); + + expect(result.length).toEqual(12); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); }); diff --git a/modules/monitor/back/methods/sales-monitor/specs/salesFilter.spec.js b/modules/monitor/back/methods/sales-monitor/specs/salesFilter.spec.js index 28e2b5571..61bd9ecc6 100644 --- a/modules/monitor/back/methods/sales-monitor/specs/salesFilter.spec.js +++ b/modules/monitor/back/methods/sales-monitor/specs/salesFilter.spec.js @@ -2,141 +2,262 @@ const models = require('vn-loopback/server/server').models; describe('SalesMonitor salesFilter()', () => { it('should now return the tickets matching the filter', async() => { - const ctx = {req: {accessToken: {userId: 9}}, args: {}}; - const filter = {order: 'id DESC'}; - const result = await models.SalesMonitor.salesFilter(ctx, filter); + const tx = await models.SalesMonitor.beginTransaction({}); - expect(result.length).toEqual(24); + try { + const options = {transaction: tx}; + + const ctx = {req: {accessToken: {userId: 9}}, args: {}}; + const filter = {order: 'id DESC'}; + const result = await models.SalesMonitor.salesFilter(ctx, filter, options); + + expect(result.length).toEqual(24); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should now return the tickets matching the problems on true', async() => { - const yesterday = new Date(); - yesterday.setHours(0, 0, 0, 0); - const today = new Date(); - today.setHours(23, 59, 59, 59); + const tx = await models.SalesMonitor.beginTransaction({}); - const ctx = {req: {accessToken: {userId: 9}}, args: { - problems: true, - from: yesterday, - to: today - }}; - const filter = {}; - const result = await models.SalesMonitor.salesFilter(ctx, filter); + try { + const options = {transaction: tx}; - expect(result.length).toEqual(13); + const yesterday = new Date(); + yesterday.setHours(0, 0, 0, 0); + const today = new Date(); + today.setHours(23, 59, 59, 59); + + const ctx = {req: {accessToken: {userId: 9}}, args: { + problems: true, + from: yesterday, + to: today + }}; + const filter = {}; + const result = await models.SalesMonitor.salesFilter(ctx, filter, options); + + expect(result.length).toEqual(13); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should now return the tickets matching the problems on false', async() => { - const yesterday = new Date(); - yesterday.setDate(yesterday.getDate() - 1); - yesterday.setHours(0, 0, 0, 0); - const today = new Date(); - today.setHours(23, 59, 59, 59); + const tx = await models.SalesMonitor.beginTransaction({}); - const ctx = {req: {accessToken: {userId: 9}}, args: { - problems: false, - from: yesterday, - to: today - }}; - const filter = {}; - const result = await models.SalesMonitor.salesFilter(ctx, filter); + try { + const options = {transaction: tx}; - expect(result.length).toEqual(0); + const yesterday = new Date(); + yesterday.setDate(yesterday.getDate() - 1); + yesterday.setHours(0, 0, 0, 0); + const today = new Date(); + today.setHours(23, 59, 59, 59); + + const ctx = {req: {accessToken: {userId: 9}}, args: { + problems: false, + from: yesterday, + to: today + }}; + const filter = {}; + const result = await models.SalesMonitor.salesFilter(ctx, filter, options); + + expect(result.length).toEqual(0); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should now return the tickets matching the problems on null', async() => { - const ctx = {req: {accessToken: {userId: 9}}, args: {problems: null}}; - const filter = {}; - const result = await models.SalesMonitor.salesFilter(ctx, filter); + const tx = await models.SalesMonitor.beginTransaction({}); - expect(result.length).toEqual(24); + try { + const options = {transaction: tx}; + + const ctx = {req: {accessToken: {userId: 9}}, args: {problems: null}}; + const filter = {}; + const result = await models.SalesMonitor.salesFilter(ctx, filter, options); + + expect(result.length).toEqual(24); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should now return the tickets matching the orderId 11', async() => { - const ctx = {req: {accessToken: {userId: 9}}, args: {orderFk: 11}}; - const filter = {}; - const result = await models.SalesMonitor.salesFilter(ctx, filter); - const firstRow = result[0]; + const tx = await models.SalesMonitor.beginTransaction({}); - expect(result.length).toEqual(1); - expect(firstRow.id).toEqual(11); + try { + const options = {transaction: tx}; + + const ctx = {req: {accessToken: {userId: 9}}, args: {orderFk: 11}}; + const filter = {}; + const result = await models.SalesMonitor.salesFilter(ctx, filter, options); + const firstRow = result[0]; + + expect(result.length).toEqual(1); + expect(firstRow.id).toEqual(11); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should now return the tickets with grouped state "Pending" and not "Ok" nor "BOARDING"', async() => { - const ctx = {req: {accessToken: {userId: 9}}, args: {pending: true}}; - const filter = {}; - const result = await models.SalesMonitor.salesFilter(ctx, filter); + const tx = await models.SalesMonitor.beginTransaction({}); - const length = result.length; - const anyResult = result[Math.floor(Math.random() * Math.floor(length))]; + try { + const options = {transaction: tx}; - expect(length).toEqual(7); - expect(anyResult.state).toMatch(/(Libre|Arreglar)/); + const ctx = {req: {accessToken: {userId: 9}}, args: {pending: true}}; + const filter = {}; + const result = await models.SalesMonitor.salesFilter(ctx, filter, options); + + const length = result.length; + const anyResult = result[Math.floor(Math.random() * Math.floor(length))]; + + expect(length).toEqual(7); + expect(anyResult.state).toMatch(/(Libre|Arreglar)/); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should now return the tickets that are not pending', async() => { - const ctx = {req: {accessToken: {userId: 9}}, args: {pending: false}}; - const filter = {}; - const result = await models.SalesMonitor.salesFilter(ctx, filter); - const firstRow = result[0]; - const secondRow = result[1]; - const thirdRow = result[2]; + const tx = await models.SalesMonitor.beginTransaction({}); - expect(result.length).toEqual(12); - expect(firstRow.state).toEqual('Entregado'); - expect(secondRow.state).toEqual('Entregado'); - expect(thirdRow.state).toEqual('Entregado'); + try { + const options = {transaction: tx}; + + const ctx = {req: {accessToken: {userId: 9}}, args: {pending: false}}; + const filter = {}; + const result = await models.SalesMonitor.salesFilter(ctx, filter, options); + const firstRow = result[0]; + const secondRow = result[1]; + const thirdRow = result[2]; + + expect(result.length).toEqual(12); + expect(firstRow.state).toEqual('Entregado'); + expect(secondRow.state).toEqual('Entregado'); + expect(thirdRow.state).toEqual('Entregado'); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should now return the tickets from the worker team', async() => { - const ctx = {req: {accessToken: {userId: 18}}, args: {myTeam: true}}; - const filter = {}; - const result = await models.SalesMonitor.salesFilter(ctx, filter); + const tx = await models.SalesMonitor.beginTransaction({}); - expect(result.length).toEqual(20); + try { + const options = {transaction: tx}; + + const ctx = {req: {accessToken: {userId: 18}}, args: {myTeam: true}}; + const filter = {}; + const result = await models.SalesMonitor.salesFilter(ctx, filter, options); + + expect(result.length).toEqual(20); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should now return the tickets that are not from the worker team', async() => { - const ctx = {req: {accessToken: {userId: 18}}, args: {myTeam: false}}; - const filter = {}; - const result = await models.SalesMonitor.salesFilter(ctx, filter); + const tx = await models.SalesMonitor.beginTransaction({}); - expect(result.length).toEqual(4); + try { + const options = {transaction: tx}; + + const ctx = {req: {accessToken: {userId: 18}}, args: {myTeam: false}}; + const filter = {}; + const result = await models.SalesMonitor.salesFilter(ctx, filter, options); + + expect(result.length).toEqual(4); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should return the tickets sorted by problems descendant', async() => { - const yesterday = new Date(); - yesterday.setDate(yesterday.getDate() - 1); - yesterday.setHours(0, 0, 0, 0); - const today = new Date(); - today.setHours(23, 59, 59, 59); + const tx = await models.SalesMonitor.beginTransaction({}); - const ctx = {req: {accessToken: {userId: 18}}, args: {}}; - const filter = {order: 'totalProblems DESC'}; - const result = await models.SalesMonitor.salesFilter(ctx, filter); + try { + const options = {transaction: tx}; - const firstTicket = result.shift(); - const secondTicket = result.shift(); + const yesterday = new Date(); + yesterday.setDate(yesterday.getDate() - 1); + yesterday.setHours(0, 0, 0, 0); + const today = new Date(); + today.setHours(23, 59, 59, 59); - expect(firstTicket.totalProblems).toEqual(3); - expect(secondTicket.totalProblems).toEqual(2); + const ctx = {req: {accessToken: {userId: 18}}, args: {}}; + const filter = {order: 'totalProblems DESC'}; + const result = await models.SalesMonitor.salesFilter(ctx, filter, options); + + const firstTicket = result.shift(); + const secondTicket = result.shift(); + + expect(firstTicket.totalProblems).toEqual(3); + expect(secondTicket.totalProblems).toEqual(2); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should return the tickets sorted by problems ascendant', async() => { - const yesterday = new Date(); - yesterday.setDate(yesterday.getDate() - 1); - yesterday.setHours(0, 0, 0, 0); - const today = new Date(); - today.setHours(23, 59, 59, 59); + const tx = await models.SalesMonitor.beginTransaction({}); - const ctx = {req: {accessToken: {userId: 18}}, args: {}}; - const filter = {order: 'totalProblems ASC'}; - const result = await models.SalesMonitor.salesFilter(ctx, filter); + try { + const options = {transaction: tx}; - const firstTicket = result.shift(); - const secondTicket = result.shift(); + const yesterday = new Date(); + yesterday.setDate(yesterday.getDate() - 1); + yesterday.setHours(0, 0, 0, 0); + const today = new Date(); + today.setHours(23, 59, 59, 59); - expect(firstTicket.totalProblems).toEqual(null); - expect(secondTicket.totalProblems).toEqual(null); + const ctx = {req: {accessToken: {userId: 18}}, args: {}}; + const filter = {order: 'totalProblems ASC'}; + const result = await models.SalesMonitor.salesFilter(ctx, filter, options); + + const firstTicket = result.shift(); + const secondTicket = result.shift(); + + expect(firstTicket.totalProblems).toEqual(null); + expect(secondTicket.totalProblems).toEqual(null); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); });