From 2137a34b36399535c256d2d1318fa10acbe5a422 Mon Sep 17 00:00:00 2001 From: alexm Date: Tue, 1 Mar 2022 10:44:04 +0100 Subject: [PATCH 1/3] feat(supplier-account): isPayMethodChecked false when change model --- .../models/specs/supplier-account.spec.js | 63 ++++++++++++++----- .../supplier/back/models/supplier-account.js | 12 ++++ 2 files changed, 59 insertions(+), 16 deletions(-) diff --git a/modules/supplier/back/models/specs/supplier-account.spec.js b/modules/supplier/back/models/specs/supplier-account.spec.js index f56e70a89..083c681ac 100644 --- a/modules/supplier/back/models/specs/supplier-account.spec.js +++ b/modules/supplier/back/models/specs/supplier-account.spec.js @@ -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; + } + }); }); }); diff --git a/modules/supplier/back/models/supplier-account.js b/modules/supplier/back/models/supplier-account.js index c8b2f0595..ae93ef83b 100644 --- a/modules/supplier/back/models/supplier-account.js +++ b/modules/supplier/back/models/supplier-account.js @@ -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); + }); }; -- 2.40.1 From a27a48f251a17a6c7a9b05d08dc03f9b13305167 Mon Sep 17 00:00:00 2001 From: alexm Date: Tue, 1 Mar 2022 10:44:46 +0100 Subject: [PATCH 2/3] fix(supplier_account): fix setWireTransfer and add test --- modules/supplier/front/account/index.js | 10 +++++++ modules/supplier/front/account/index.spec.js | 28 ++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/modules/supplier/front/account/index.js b/modules/supplier/front/account/index.js index 6c6e77d2e..5629e65d3 100644 --- a/modules/supplier/front/account/index.js +++ b/modules/supplier/front/account/index.js @@ -45,6 +45,16 @@ class Controller extends Section { this.$.payMethodToTransfer.show(); }); } + + setWireTransfer() { + const params = { + id: this.$params.id, + payMethodFk: this.wireTransferFk + }; + const query = `Suppliers/${this.$params.id}`; + return this.$http.patch(query, params) + .then(() => this.$.watcher.notifySaved()); + } } ngModule.vnComponent('vnSupplierAccount', { diff --git a/modules/supplier/front/account/index.spec.js b/modules/supplier/front/account/index.spec.js index 5c824907f..ad29d1abc 100644 --- a/modules/supplier/front/account/index.spec.js +++ b/modules/supplier/front/account/index.spec.js @@ -5,9 +5,12 @@ import crudModel from 'core/mocks/crud-model'; describe('Supplier Component vnSupplierAccount', () => { let $scope; let controller; + let $httpBackend; + beforeEach(ngModule('supplier')); beforeEach(inject(($componentController, $rootScope, _$httpBackend_) => { + $httpBackend = _$httpBackend_; $scope = $rootScope.$new(); $scope.model = crudModel; $scope.watcher = watcher; @@ -66,5 +69,30 @@ describe('Supplier Component vnSupplierAccount', () => { }).catch(done.fail); }); }); + + describe('setWireTransfer()', () => { + it(`should make HTTP PATCH request to set wire transfer and call notifySaved`, () => { + const supplierId = 1; + const params = { + id: supplierId, + payMethodFk: 2 + }; + const response = { + data: {id: 2} + }; + const uri = 'payMethods/findOne?filter=%7B%22where%22:%7B%22code%22:%22wireTransfer%22%7D%7D'; + jest.spyOn($scope.watcher, 'notifySaved'); + + controller.$params.id = supplierId; + controller.wireTransferFk = 2; + controller.supplier = {payMethodFk: 1}; + $httpBackend.expectGET(uri).respond(response); + $httpBackend.expectPATCH(`Suppliers/${supplierId}`, params).respond(); + controller.setWireTransfer(); + $httpBackend.flush(); + + expect($scope.watcher.notifySaved).toHaveBeenCalledWith(); + }); + }); }); -- 2.40.1 From 6748426f670ed5878c86f249880f530fc0a7fb89 Mon Sep 17 00:00:00 2001 From: alexm Date: Thu, 3 Mar 2022 13:48:18 +0100 Subject: [PATCH 3/3] correct loopback form --- modules/supplier/back/models/supplier-account.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/modules/supplier/back/models/supplier-account.js b/modules/supplier/back/models/supplier-account.js index ae93ef83b..dc6c6d5fd 100644 --- a/modules/supplier/back/models/supplier-account.js +++ b/modules/supplier/back/models/supplier-account.js @@ -38,10 +38,9 @@ module.exports = Self => { 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); + const supplier = await Self.app.models.Supplier.findById(ctx.instance.supplierFk, null, options); if (supplier.isPayMethodChecked) await supplier.updateAttribute('isPayMethodChecked', false, options); -- 2.40.1