refactor(monitors): added transactions to endpoints
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Carlos Jimenez Ruiz 2021-10-22 17:32:10 +02:00
parent 2cdc22bc4d
commit 13c06bf5be
8 changed files with 298 additions and 129 deletions

View File

@ -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 userId = ctx.req.accessToken.userId;
const conn = Self.dataSource.connector; const conn = Self.dataSource.connector;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const stmt = new ParameterizedSQL(` const stmt = new ParameterizedSQL(`
SELECT SELECT
@ -55,6 +59,6 @@ module.exports = Self => {
stmt.merge(conn.makeSuffix(filter)); stmt.merge(conn.makeSuffix(filter));
return conn.executeStmt(stmt); return conn.executeStmt(stmt, myOptions);
}; };
}; };

View File

@ -20,14 +20,28 @@ module.exports = Self => {
Self.deleteOrders = async(deletes = [], options) => { Self.deleteOrders = async(deletes = [], options) => {
const models = Self.app.models; const models = Self.app.models;
const myOptions = {};
let myOptions = {}; let tx;
if (typeof options == 'object') if (typeof options == 'object')
Object.assign(myOptions, options); Object.assign(myOptions, options);
return models.Order.destroyAll({ if (!myOptions.transaction) {
id: {inq: deletes} tx = await Self.beginTransaction({});
}, myOptions); 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;
}
}; };
}; };

View File

@ -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 userId = ctx.req.accessToken.userId;
const conn = Self.dataSource.connector; const conn = Self.dataSource.connector;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const stmt = new ParameterizedSQL(` const stmt = new ParameterizedSQL(`
SELECT SELECT
c.id AS clientFk, c.id AS clientFk,
c.name AS clientName, c.name AS clientName,
a.nickname, a.nickname,
o.id, o.id,
o.date_make, o.date_make,
o.date_send, o.date_send,
o.customer_id, o.customer_id,
COUNT(item_id) AS totalRows, COUNT(item_id) AS totalRows,
ROUND(SUM(amount * price)) * 1 AS import, ROUND(SUM(amount * price)) * 1 AS import,
u.id AS salesPersonFk, u.id AS salesPersonFk,
u.name AS salesPerson, u.name AS salesPerson,
am.name AS agencyName am.name AS agencyName
FROM hedera.order o FROM hedera.order o
JOIN hedera.order_row orw ON o.id = orw.order_id JOIN hedera.order_row orw ON o.id = orw.order_id
JOIN client c ON c.id = o.customer_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.makeGroupBy('o.id'));
stmt.merge(conn.makePagination(filter)); stmt.merge(conn.makePagination(filter));
return conn.executeStmt(stmt); return conn.executeStmt(stmt, myOptions);
}; };
}; };

View File

@ -63,7 +63,7 @@ module.exports = Self => {
}, { }, {
arg: 'myTeam', arg: 'myTeam',
type: 'boolean', 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', arg: 'problems',
type: 'boolean', 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 userId = ctx.req.accessToken.userId;
const conn = Self.dataSource.connector; const conn = Self.dataSource.connector;
const models = Self.app.models; const models = Self.app.models;
const args = ctx.args; const args = ctx.args;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
// Apply filter by team // Apply filter by team
const teamMembersId = []; const teamMembersId = [];
@ -113,7 +117,7 @@ module.exports = Self => {
include: { include: {
relation: 'collegues' relation: 'collegues'
} }
}); }, myOptions);
const collegues = worker.collegues() || []; const collegues = worker.collegues() || [];
for (let collegue of collegues) for (let collegue of collegues)
teamMembersId.push(collegue.collegueFk); teamMembersId.push(collegue.collegueFk);
@ -150,7 +154,7 @@ module.exports = Self => {
filter = mergeFilters(filter, {where}); filter = mergeFilters(filter, {where});
let stmts = []; const stmts = [];
let stmt; let stmt;
stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.filter'); 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.makeOrderBy(filter.order));
stmt.merge(conn.makeLimit(filter)); stmt.merge(conn.makeLimit(filter));
let ticketsIndex = stmts.push(stmt) - 1; const ticketsIndex = stmts.push(stmt) - 1;
stmts.push( stmts.push(
`DROP TEMPORARY TABLE `DROP TEMPORARY TABLE
@ -367,8 +371,8 @@ module.exports = Self => {
tmp.sale_getProblems, tmp.sale_getProblems,
tmp.risk`); tmp.risk`);
let sql = ParameterizedSQL.join(stmts, ';'); const sql = ParameterizedSQL.join(stmts, ';');
let result = await conn.executeStmt(sql); const result = await conn.executeStmt(sql, myOptions);
return result[ticketsIndex]; return result[ticketsIndex];
}; };

View File

@ -1,11 +1,22 @@
const app = require('vn-loopback/server/server'); const models = require('vn-loopback/server/server').models;
describe('SalesMonitor clientsFilter()', () => { describe('SalesMonitor clientsFilter()', () => {
it('should return the clients web activity', async() => { it('should return the clients web activity', async() => {
const ctx = {req: {accessToken: {userId: 18}}, args: {}}; const tx = await models.SalesMonitor.beginTransaction({});
const filter = {order: 'dated DESC'};
const result = await app.models.SalesMonitor.clientsFilter(ctx, filter);
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;
}
}); });
}); });

View File

@ -1,14 +1,14 @@
const app = require('vn-loopback/server/server'); const models = require('vn-loopback/server/server').models;
describe('SalesMonitor deleteOrders()', () => { describe('SalesMonitor deleteOrders()', () => {
it('should return the deleted orders', async() => { it('should return the deleted orders', async() => {
const tx = await app.models.Order.beginTransaction({}); const tx = await models.Order.beginTransaction({});
try { try {
const options = {transaction: tx}; const options = {transaction: tx};
const deletes = [1, 2]; 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); expect(result.count).toEqual(2);

View File

@ -1,11 +1,22 @@
const app = require('vn-loopback/server/server'); const models = require('vn-loopback/server/server').models;
describe('SalesMonitor ordersFilter()', () => { describe('SalesMonitor ordersFilter()', () => {
it('should return the orders activity', async() => { it('should return the orders activity', async() => {
const ctx = {req: {accessToken: {userId: 18}}, args: {}}; const tx = await models.SalesMonitor.beginTransaction({});
const filter = {order: 'date_make DESC'};
const result = await app.models.SalesMonitor.ordersFilter(ctx, filter);
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;
}
}); });
}); });

View File

@ -2,141 +2,262 @@ const models = require('vn-loopback/server/server').models;
describe('SalesMonitor salesFilter()', () => { describe('SalesMonitor salesFilter()', () => {
it('should now return the tickets matching the filter', async() => { it('should now return the tickets matching the filter', async() => {
const ctx = {req: {accessToken: {userId: 9}}, args: {}}; const tx = await models.SalesMonitor.beginTransaction({});
const filter = {order: 'id DESC'};
const result = await models.SalesMonitor.salesFilter(ctx, filter);
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() => { it('should now return the tickets matching the problems on true', async() => {
const yesterday = new Date(); const tx = await models.SalesMonitor.beginTransaction({});
yesterday.setHours(0, 0, 0, 0);
const today = new Date();
today.setHours(23, 59, 59, 59);
const ctx = {req: {accessToken: {userId: 9}}, args: { try {
problems: true, const options = {transaction: tx};
from: yesterday,
to: today
}};
const filter = {};
const result = await models.SalesMonitor.salesFilter(ctx, filter);
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() => { it('should now return the tickets matching the problems on false', async() => {
const yesterday = new Date(); const tx = await models.SalesMonitor.beginTransaction({});
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: { try {
problems: false, const options = {transaction: tx};
from: yesterday,
to: today
}};
const filter = {};
const result = await models.SalesMonitor.salesFilter(ctx, filter);
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() => { it('should now return the tickets matching the problems on null', async() => {
const ctx = {req: {accessToken: {userId: 9}}, args: {problems: null}}; const tx = await models.SalesMonitor.beginTransaction({});
const filter = {};
const result = await models.SalesMonitor.salesFilter(ctx, filter);
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() => { it('should now return the tickets matching the orderId 11', async() => {
const ctx = {req: {accessToken: {userId: 9}}, args: {orderFk: 11}}; const tx = await models.SalesMonitor.beginTransaction({});
const filter = {};
const result = await models.SalesMonitor.salesFilter(ctx, filter);
const firstRow = result[0];
expect(result.length).toEqual(1); try {
expect(firstRow.id).toEqual(11); 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() => { 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 tx = await models.SalesMonitor.beginTransaction({});
const filter = {};
const result = await models.SalesMonitor.salesFilter(ctx, filter);
const length = result.length; try {
const anyResult = result[Math.floor(Math.random() * Math.floor(length))]; const options = {transaction: tx};
expect(length).toEqual(7); const ctx = {req: {accessToken: {userId: 9}}, args: {pending: true}};
expect(anyResult.state).toMatch(/(Libre|Arreglar)/); 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() => { it('should now return the tickets that are not pending', async() => {
const ctx = {req: {accessToken: {userId: 9}}, args: {pending: false}}; const tx = await models.SalesMonitor.beginTransaction({});
const filter = {};
const result = await models.SalesMonitor.salesFilter(ctx, filter);
const firstRow = result[0];
const secondRow = result[1];
const thirdRow = result[2];
expect(result.length).toEqual(12); try {
expect(firstRow.state).toEqual('Entregado'); const options = {transaction: tx};
expect(secondRow.state).toEqual('Entregado');
expect(thirdRow.state).toEqual('Entregado'); 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() => { it('should now return the tickets from the worker team', async() => {
const ctx = {req: {accessToken: {userId: 18}}, args: {myTeam: true}}; const tx = await models.SalesMonitor.beginTransaction({});
const filter = {};
const result = await models.SalesMonitor.salesFilter(ctx, filter);
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() => { it('should now return the tickets that are not from the worker team', async() => {
const ctx = {req: {accessToken: {userId: 18}}, args: {myTeam: false}}; const tx = await models.SalesMonitor.beginTransaction({});
const filter = {};
const result = await models.SalesMonitor.salesFilter(ctx, filter);
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() => { it('should return the tickets sorted by problems descendant', async() => {
const yesterday = new Date(); const tx = await models.SalesMonitor.beginTransaction({});
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: 18}}, args: {}}; try {
const filter = {order: 'totalProblems DESC'}; const options = {transaction: tx};
const result = await models.SalesMonitor.salesFilter(ctx, filter);
const firstTicket = result.shift(); const yesterday = new Date();
const secondTicket = result.shift(); 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); const ctx = {req: {accessToken: {userId: 18}}, args: {}};
expect(secondTicket.totalProblems).toEqual(2); 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() => { it('should return the tickets sorted by problems ascendant', async() => {
const yesterday = new Date(); const tx = await models.SalesMonitor.beginTransaction({});
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: 18}}, args: {}}; try {
const filter = {order: 'totalProblems ASC'}; const options = {transaction: tx};
const result = await models.SalesMonitor.salesFilter(ctx, filter);
const firstTicket = result.shift(); const yesterday = new Date();
const secondTicket = result.shift(); 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); const ctx = {req: {accessToken: {userId: 18}}, args: {}};
expect(secondTicket.totalProblems).toEqual(null); 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;
}
}); });
}); });