Merge pull request 'refactor(monitors): added transactions to endpoints' (#761) from 3100-monitors into dev
gitea/salix/pipeline/head There was a failure building this commit Details

Reviewed-on: #761
Reviewed-by: Joan Sanchez <joan@verdnatura.es>
This commit is contained in:
Joan Sanchez 2021-10-25 07:26:26 +00:00
commit b055618577
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 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);
};
};

View File

@ -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({
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;
}
};
};

View File

@ -26,9 +26,13 @@ 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
@ -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);
};
};

View File

@ -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];
};

View File

@ -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 tx = await models.SalesMonitor.beginTransaction({});
try {
const options = {transaction: tx};
const ctx = {req: {accessToken: {userId: 18}}, args: {}};
const filter = {order: 'dated DESC'};
const result = await app.models.SalesMonitor.clientsFilter(ctx, filter);
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()', () => {
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);

View File

@ -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 tx = await models.SalesMonitor.beginTransaction({});
try {
const options = {transaction: tx};
const ctx = {req: {accessToken: {userId: 18}}, args: {}};
const filter = {order: 'date_make DESC'};
const result = await app.models.SalesMonitor.ordersFilter(ctx, filter);
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,14 +2,30 @@ const models = require('vn-loopback/server/server').models;
describe('SalesMonitor salesFilter()', () => {
it('should now return the tickets matching the filter', async() => {
const tx = await models.SalesMonitor.beginTransaction({});
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);
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 tx = await models.SalesMonitor.beginTransaction({});
try {
const options = {transaction: tx};
const yesterday = new Date();
yesterday.setHours(0, 0, 0, 0);
const today = new Date();
@ -21,12 +37,23 @@ describe('SalesMonitor salesFilter()', () => {
to: today
}};
const filter = {};
const result = await models.SalesMonitor.salesFilter(ctx, 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 tx = await models.SalesMonitor.beginTransaction({});
try {
const options = {transaction: tx};
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
yesterday.setHours(0, 0, 0, 0);
@ -39,45 +66,89 @@ describe('SalesMonitor salesFilter()', () => {
to: today
}};
const filter = {};
const result = await models.SalesMonitor.salesFilter(ctx, 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 tx = await models.SalesMonitor.beginTransaction({});
try {
const options = {transaction: tx};
const ctx = {req: {accessToken: {userId: 9}}, args: {problems: null}};
const filter = {};
const result = await models.SalesMonitor.salesFilter(ctx, 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 tx = await models.SalesMonitor.beginTransaction({});
try {
const options = {transaction: tx};
const ctx = {req: {accessToken: {userId: 9}}, args: {orderFk: 11}};
const filter = {};
const result = await models.SalesMonitor.salesFilter(ctx, 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 tx = await models.SalesMonitor.beginTransaction({});
try {
const options = {transaction: tx};
const ctx = {req: {accessToken: {userId: 9}}, args: {pending: true}};
const filter = {};
const result = await models.SalesMonitor.salesFilter(ctx, 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 tx = await models.SalesMonitor.beginTransaction({});
try {
const options = {transaction: tx};
const ctx = {req: {accessToken: {userId: 9}}, args: {pending: false}};
const filter = {};
const result = await models.SalesMonitor.salesFilter(ctx, filter);
const result = await models.SalesMonitor.salesFilter(ctx, filter, options);
const firstRow = result[0];
const secondRow = result[1];
const thirdRow = result[2];
@ -86,25 +157,58 @@ describe('SalesMonitor salesFilter()', () => {
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 tx = await models.SalesMonitor.beginTransaction({});
try {
const options = {transaction: tx};
const ctx = {req: {accessToken: {userId: 18}}, args: {myTeam: true}};
const filter = {};
const result = await models.SalesMonitor.salesFilter(ctx, 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 tx = await models.SalesMonitor.beginTransaction({});
try {
const options = {transaction: tx};
const ctx = {req: {accessToken: {userId: 18}}, args: {myTeam: false}};
const filter = {};
const result = await models.SalesMonitor.salesFilter(ctx, 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 tx = await models.SalesMonitor.beginTransaction({});
try {
const options = {transaction: tx};
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
yesterday.setHours(0, 0, 0, 0);
@ -113,16 +217,27 @@ describe('SalesMonitor salesFilter()', () => {
const ctx = {req: {accessToken: {userId: 18}}, args: {}};
const filter = {order: 'totalProblems DESC'};
const result = await models.SalesMonitor.salesFilter(ctx, filter);
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 tx = await models.SalesMonitor.beginTransaction({});
try {
const options = {transaction: tx};
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
yesterday.setHours(0, 0, 0, 0);
@ -131,12 +246,18 @@ describe('SalesMonitor salesFilter()', () => {
const ctx = {req: {accessToken: {userId: 18}}, args: {}};
const filter = {order: 'totalProblems ASC'};
const result = await models.SalesMonitor.salesFilter(ctx, filter);
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;
}
});
});