feat(supplier-account): isPayMethodChecked false when change model

This commit is contained in:
Alex Moreno 2022-03-01 10:44:04 +01:00
parent 73d853cb53
commit 2137a34b36
2 changed files with 59 additions and 16 deletions

View File

@ -1,16 +1,28 @@
const app = require('vn-loopback/server/server');
const models = require('vn-loopback/server/server').models;
const LoopBackContext = require('loopback-context');
describe('loopback model Supplier-account', () => {
describe('create', () => {
const supplierId = 1;
const bankEntityId = 2100;
const activeCtx = {
accessToken: {userId: 5},
http: {
req: {
headers: {origin: 'http://localhost'}
}
}
};
activeCtx.http.req.__ = value => {
return value;
};
it('should throw an error when attempting to set an invalid iban account', async() => {
let error;
const expectedError = 'The IBAN does not have the correct format';
const iban = 'incorrect format';
try {
await app.models.SupplierAccount.create(
await models.SupplierAccount.create(
{
supplierFk: supplierId,
bankEntityFk: bankEntityId,
@ -26,27 +38,16 @@ describe('loopback model Supplier-account', () => {
});
it('should create a valid supplier account', async() => {
const tx = await app.models.Claim.beginTransaction({});
const tx = await models.SupplierAccount.beginTransaction({});
try {
const options = {transaction: tx};
const iban = 'ES91 2100 0418 4502 0005 1332';
const activeCtx = {
accessToken: {userId: 5},
http: {
req: {
headers: {origin: 'http://localhost'}
}
}
};
activeCtx.http.req.__ = value => {
return value;
};
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: activeCtx
});
const createdSupplierAccount = await app.models.SupplierAccount.create({
const createdSupplierAccount = await models.SupplierAccount.create({
supplierFk: supplierId,
bankEntityFk: bankEntityId,
iban: iban
@ -60,5 +61,35 @@ describe('loopback model Supplier-account', () => {
throw e;
}
});
it('should change isPayMethodChecked to false', async() => {
const tx = await models.SupplierAccount.beginTransaction({});
try {
const options = {transaction: tx};
const iban = 'ES91 2100 0418 4502 0005 1332';
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: activeCtx
});
const supplierBefore = await models.Supplier.findById(supplierId, null, options);
await models.SupplierAccount.create({
supplierFk: supplierId,
bankEntityFk: bankEntityId,
iban: iban
},
options);
const supplierAfter = await models.Supplier.findById(supplierId, null, options);
expect(supplierBefore.isPayMethodChecked).toBeTruthy();
expect(supplierAfter.isPayMethodChecked).toBeFalsy();
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
});
});

View File

@ -34,4 +34,16 @@ module.exports = Self => {
ctx.instance.iban + ', entidad: ' + bankEntity.name + ', bic: ' + bankEntity.bic
});
});
Self.observe('after save', async ctx => {
const options = {};
// Check for transactions
if (ctx.options && ctx.options.transaction)
options.transaction = ctx.options.transaction;
const supplier = await Self.app.models.Supplier.findById(ctx.instance.supplierFk, options);
if (supplier.isPayMethodChecked)
await supplier.updateAttribute('isPayMethodChecked', false, options);
});
};