updateClaimAction refactor + tests

This commit is contained in:
Carlos Jimenez Ruiz 2021-03-29 15:18:14 +02:00
parent 3054cbcadb
commit 8d8bdacd86
2 changed files with 60 additions and 28 deletions

View File

@ -1,9 +1,8 @@
const app = require('vn-loopback/server/server');
describe('Update Claim', () => {
let newDate = new Date();
let newInstance;
let original = {
const newDate = new Date();
const original = {
ticketFk: 3,
clientFk: 101,
ticketCreated: newDate,
@ -14,30 +13,43 @@ describe('Update Claim', () => {
observation: 'observation'
};
beforeAll(async done => {
newInstance = await app.models.Claim.create(original);
done();
});
afterAll(async done => {
await app.models.Claim.destroyById(newInstance.id);
done();
});
it('should update the claim isChargedToMana attribute', async() => {
const ctx = {args: {isChargedToMana: false}};
const result = await app.models.Claim.updateClaimAction(ctx, newInstance.id);
const tx = await app.models.Claim.beginTransaction({});
expect(result.id).toEqual(newInstance.id);
expect(result.isChargedToMana).toBeFalsy();
try {
const options = {transaction: tx};
const ctx = {args: {isChargedToMana: false}};
const newInstance = await app.models.Claim.create(original, options);
const result = await app.models.Claim.updateClaimAction(ctx, newInstance.id, options);
expect(result.id).toEqual(newInstance.id);
expect(result.isChargedToMana).toBeFalsy();
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should update the claim responsibility attribute', async() => {
const ctx = {args: {responsibility: 2}};
const result = await app.models.Claim.updateClaimAction(ctx, newInstance.id);
const tx = await app.models.Claim.beginTransaction({});
expect(result.id).toEqual(newInstance.id);
expect(result.responsibility).toEqual(2);
try {
const options = {transaction: tx};
const ctx = {args: {responsibility: 2}};
const newInstance = await app.models.Claim.create(original, options);
const result = await app.models.Claim.updateClaimAction(ctx, newInstance.id, options);
expect(result.id).toEqual(newInstance.id);
expect(result.responsibility).toEqual(2);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
});

View File

@ -28,12 +28,32 @@ module.exports = Self => {
}
});
Self.updateClaimAction = async(ctx, id) => {
const models = Self.app.models;
const claim = await models.Claim.findById(id);
const args = ctx.args;
delete args.ctx;
Self.updateClaimAction = async(ctx, id, options) => {
let tx;
let myOptions = {};
return await claim.updateAttributes(args);
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const models = Self.app.models;
const claim = await models.Claim.findById(id, null, myOptions);
const args = ctx.args;
delete args.ctx;
const updatedClaim = await claim.updateAttributes(args, myOptions);
if (tx) await tx.commit();
return updatedClaim;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};