claim module transactions for filter, summary and isEditable
This commit is contained in:
parent
ae091a3b11
commit
10c7aa70d6
|
@ -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];
|
||||
};
|
||||
};
|
||||
|
|
|
@ -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];
|
||||
|
|
|
@ -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';
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue