claim module transactions for filter, summary and isEditable

This commit is contained in:
Carlos Jimenez Ruiz 2021-07-05 12:00:49 +02:00
parent ae091a3b11
commit 10c7aa70d6
6 changed files with 155 additions and 52 deletions

View File

@ -79,10 +79,14 @@ module.exports = Self => {
} }
}); });
Self.filter = async(ctx, filter) => { Self.filter = async(ctx, filter, options) => {
let conn = Self.dataSource.connector; 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) { switch (param) {
case 'search': case 'search':
return /^\d+$/.test(value) return /^\d+$/.test(value)
@ -111,10 +115,9 @@ module.exports = Self => {
filter = mergeFilters(ctx.args.filter, {where}); filter = mergeFilters(ctx.args.filter, {where});
let stmts = []; const stmts = [];
let stmt;
stmt = new ParameterizedSQL( const stmt = new ParameterizedSQL(
`SELECT cl.id, c.name, cl.clientFk, cl.workerFk, u.name AS userName, cs.description, cl.created `SELECT cl.id, c.name, cl.clientFk, cl.workerFk, u.name AS userName, cs.description, cl.created
FROM claim cl FROM claim cl
LEFT JOIN client c ON c.id = cl.clientFk LEFT JOIN client c ON c.id = cl.clientFk
@ -124,10 +127,11 @@ module.exports = Self => {
); );
stmt.merge(conn.makeSuffix(filter)); 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]; return itemsIndex === 0 ? result : result[itemsIndex];
}; };
}; };

View File

@ -19,12 +19,16 @@ module.exports = Self => {
} }
}); });
Self.getSummary = async id => { Self.getSummary = async(id, options) => {
let promises = []; const myOptions = {};
let summary = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const promises = [];
const summary = {};
// Claim // Claim
let filter = { let filter = {
where: {id: id}, where: {id: id},
include: [ 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 // Claim detail
filter = { filter = {
where: {claimFk: id}, where: {claimFk: id},
include: [ include: [
{relation: 'sale', {
relation: 'sale',
scope: { scope: {
fields: ['concept', 'ticketFk', 'price', 'quantity', 'discount', 'itemFk'], fields: ['concept', 'ticketFk', 'price', 'quantity', 'discount', 'itemFk'],
include: { 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 // Claim developments
filter = { 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 // Claim action
filter = { filter = {
@ -126,9 +131,9 @@ module.exports = Self => {
{relation: 'claimBeggining'} {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.claim] = res[0];
summary.salesClaimed = res[1]; summary.salesClaimed = res[1];

View File

@ -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 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, { const claim = await Self.app.models.Claim.findById(id, {
fields: ['claimStateFk'], fields: ['claimStateFk'],
include: [{ include: [{
relation: 'claimState' relation: 'claimState'
}] }]
}); }, myOptions);
const isClaimResolved = claim && claim.claimState().code == 'resolved'; const isClaimResolved = claim && claim.claimState().code == 'resolved';

View File

@ -2,26 +2,59 @@ const app = require('vn-loopback/server/server');
describe('claim filter()', () => { describe('claim filter()', () => {
it('should return 1 result filtering by id', async() => { 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); try {
expect(result[0].id).toEqual(1); 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() => { 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); try {
expect(result[0].id).toEqual(4); 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() => { 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); try {
expect(result[0].id).toEqual(1); const options = {transaction: tx};
expect(result[1].id).toEqual(2);
expect(result[2].id).toEqual(3); const result = await app.models.Claim.filter({args: {filter: {}, workerFk: 18}}, null, options);
expect(result[3].id).toEqual(4);
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;
}
}); });
}); });

View File

@ -2,12 +2,23 @@ const app = require('vn-loopback/server/server');
describe('claim getSummary()', () => { describe('claim getSummary()', () => {
it('should return summary with claim, salesClaimed, developments and actions defined ', async() => { it('should return summary with claim, salesClaimed, developments and actions defined ', async() => {
let result = await app.models.Claim.getSummary(1); const tx = await app.models.Claim.beginTransaction({});
let keys = Object.keys(result);
expect(keys).toContain('claim'); try {
expect(keys).toContain('salesClaimed'); const options = {transaction: tx};
expect(keys).toContain('developments');
expect(keys).toContain('actions'); 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;
}
}); });
}); });

View File

@ -4,30 +4,74 @@ describe('claim isEditable()', () => {
const salesPerdonId = 18; const salesPerdonId = 18;
const claimManagerId = 72; const claimManagerId = 72;
it('should return false if the given claim does not exist', async() => { it('should return false if the given claim does not exist', async() => {
let ctx = {req: {accessToken: {userId: claimManagerId}}}; const tx = await app.models.Claim.beginTransaction({});
let result = await app.models.Claim.isEditable(ctx, 99999);
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() => { it('should not be able to edit a resolved claim for a salesPerson', async() => {
let ctx = {req: {accessToken: {userId: salesPerdonId}}}; const tx = await app.models.Claim.beginTransaction({});
let result = await app.models.Claim.isEditable(ctx, 4);
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() => { it('should be able to edit a resolved claim for a claimManager', async() => {
let ctx = {req: {accessToken: {userId: claimManagerId}}}; const tx = await app.models.Claim.beginTransaction({});
let result = await app.models.Claim.isEditable(ctx, 4);
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() => { it('should be able to edit a claim for a claimManager', async() => {
let ctx = {req: {accessToken: {userId: salesPerdonId}}}; const tx = await app.models.Claim.beginTransaction({});
let result = await app.models.Claim.isEditable(ctx, 1);
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;
}
}); });
}); });