From 4657bf4e433c9292d2604321d2f0dab865073cce Mon Sep 17 00:00:00 2001 From: jgallego Date: Tue, 22 Aug 2023 14:58:26 +0200 Subject: [PATCH 1/2] refs #6143 refactor: ibanValidation with countryCode param --- loopback/util/validateIban.js | 3 ++- modules/client/back/models/client.js | 8 +++----- modules/supplier/back/models/supplier-account.js | 9 +++------ 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/loopback/util/validateIban.js b/loopback/util/validateIban.js index 3ca09ef95..ed3e00426 100644 --- a/loopback/util/validateIban.js +++ b/loopback/util/validateIban.js @@ -1,6 +1,7 @@ -module.exports = function(iban) { +module.exports = function(iban, countryCode) { if (iban == null) return true; if (typeof iban != 'string') return false; + if (countryCode?.toLowerCase() != 'es') return true; iban = iban.toUpperCase(); iban = trim(iban); diff --git a/modules/client/back/models/client.js b/modules/client/back/models/client.js index 129924b78..bdf2f91b5 100644 --- a/modules/client/back/models/client.js +++ b/modules/client/back/models/client.js @@ -90,16 +90,14 @@ module.exports = Self => { }); async function ibanNeedsValidation(err, done) { + const bankEntity = await Self.app.models.BankEntity.findById(this.bankEntityFk); const filter = { fields: ['code'], - where: {id: this.countryFk} + where: {id: bankEntity.countryFk} }; const country = await Self.app.models.Country.findOne(filter); - const code = country ? country.code.toLowerCase() : null; - if (code != 'es') - return done(); - if (!validateIban(this.iban)) + if (!validateIban(this.iban, country?.code)) err(); done(); } diff --git a/modules/supplier/back/models/supplier-account.js b/modules/supplier/back/models/supplier-account.js index 51da113ec..7a9e14421 100644 --- a/modules/supplier/back/models/supplier-account.js +++ b/modules/supplier/back/models/supplier-account.js @@ -7,18 +7,15 @@ module.exports = Self => { }); async function ibanValidation(err, done) { - const supplier = await Self.app.models.Supplier.findById(this.supplierFk); + const bankEntity = await Self.app.models.BankEntity.findById(this.bankEntityFk); const filter = { fields: ['code'], - where: {id: supplier.countryFk} + where: {id: bankEntity.countryFk} }; const country = await Self.app.models.Country.findOne(filter); - const code = country ? country.code.toLowerCase() : null; - if (code != 'es') - return done(); - if (!validateIban(this.iban)) + if (!validateIban(this.iban, country?.code)) err(); done(); } -- 2.40.1 From f54a23922904e7afba8b5b69aff839769de337ff Mon Sep 17 00:00:00 2001 From: jgallego Date: Fri, 25 Aug 2023 09:46:53 +0200 Subject: [PATCH 2/2] fixes #6143 fixes: this.bankEntityFk no existe --- loopback/util/specs/validateIban.spec.js | 44 ++++++++++++++----- modules/client/back/models/client.js | 3 ++ .../supplier/back/models/supplier-account.js | 3 ++ 3 files changed, 40 insertions(+), 10 deletions(-) diff --git a/loopback/util/specs/validateIban.spec.js b/loopback/util/specs/validateIban.spec.js index a5d08d4c4..dd096c1b5 100644 --- a/loopback/util/specs/validateIban.spec.js +++ b/loopback/util/specs/validateIban.spec.js @@ -1,21 +1,45 @@ const validateIban = require('../validateIban'); describe('IBAN validation', () => { - it('should return false for non-IBAN input', () => { - let isValid = validateIban('Pepinillos'); + it('should return false for invalid Spanish IBAN format', () => { + let isValid = validateIban('ES00 9999 0000 9999 0000 9999', 'ES'); expect(isValid).toBeFalsy(); }); - it('should return false for invalid spanish IBAN input', () => { - let isValid = validateIban('ES00 9999 0000 9999 0000 9999'); - - expect(isValid).toBeFalsy(); - }); - - it('should return true for valid spanish IBAN', () => { - let isValid = validateIban('ES91 2100 0418 4502 0005 1332'); + it('should return true for valid Spanish IBAN', () => { + let isValid = validateIban('ES91 2100 0418 4502 0005 1332', 'ES'); expect(isValid).toBeTruthy(); }); + + it('should return false for invalid Spanish IBAN with incorrect length', () => { + let isValid = validateIban('ES91210004184502000513', 'ES'); + + expect(isValid).toBeFalsy(); + }); + + it('should return false for invalid Spanish IBAN with incorrect module97 result', () => { + let isValid = validateIban('ES9121000418450200051331', 'ES'); + + expect(isValid).toBeFalsy(); + }); + + it('should return true for a non-Spanish countryCode', () => { + let isValid = validateIban('DE89370400440532013000', 'AT'); + + expect(isValid).toBeTruthy(); + }); + + it('should return true for null IBAN', () => { + let isValid = validateIban(null, 'ES'); + + expect(isValid).toBeTruthy(); + }); + + it('should return false for non-string IBAN', () => { + let isValid = validateIban(12345, 'ES'); + + expect(isValid).toBeFalsy(); + }); }); diff --git a/modules/client/back/models/client.js b/modules/client/back/models/client.js index bdf2f91b5..8e720484f 100644 --- a/modules/client/back/models/client.js +++ b/modules/client/back/models/client.js @@ -90,6 +90,9 @@ module.exports = Self => { }); async function ibanNeedsValidation(err, done) { + if (!this.bankEntityFk) + return done(); + const bankEntity = await Self.app.models.BankEntity.findById(this.bankEntityFk); const filter = { fields: ['code'], diff --git a/modules/supplier/back/models/supplier-account.js b/modules/supplier/back/models/supplier-account.js index 7a9e14421..691e72580 100644 --- a/modules/supplier/back/models/supplier-account.js +++ b/modules/supplier/back/models/supplier-account.js @@ -7,6 +7,9 @@ module.exports = Self => { }); async function ibanValidation(err, done) { + if (!this.bankEntityFk) + return done(); + const bankEntity = await Self.app.models.BankEntity.findById(this.bankEntityFk); const filter = { fields: ['code'], -- 2.40.1