diff --git a/services/loopback/common/models/client.js b/services/loopback/common/models/client.js index f55d230210..c5e2b8404b 100644 --- a/services/loopback/common/models/client.js +++ b/services/loopback/common/models/client.js @@ -50,10 +50,22 @@ module.exports = function(Self) { allowBlank: true }); - let validateDni = require('../validations/validateDni'); - Self.validateBinded('fi', validateDni, { + Self.validateAsync('fi', fiIsValid, { message: 'DNI Incorrecto' }); + let validateDni = require('../validations/validateDni'); + async function fiIsValid(err, done) { + let filter = { + fields: ['code'], + where: {id: this.countryFk} + }; + let country = await Self.app.models.Country.findOne(filter); + let code = country ? country.code.toLowerCase() : null; + + if (!validateDni(this.fi, code)) + err(); + done(); + } Self.validate('payMethod', hasSalesMan, { message: 'No se puede cambiar la forma de pago si no hay comercial asignado' diff --git a/services/loopback/common/validations/specs/validateDni.spec.js b/services/loopback/common/validations/specs/validateDni.spec.js index 2e902d6a22..432e64006d 100644 --- a/services/loopback/common/validations/specs/validateDni.spec.js +++ b/services/loopback/common/validations/specs/validateDni.spec.js @@ -14,7 +14,7 @@ describe('DNI validation', () => { expect(isValid).toBeTruthy(); }); - it('should return true for spanish DNI with exceeded digits', () => { + it('should return false for spanish DNI with exceeded digits', () => { let isValid = validateDni('208497563239A'); expect(isValid).toBeFalsy(); @@ -41,19 +41,19 @@ describe('DNI validation', () => { describe('French', () => { it('should return true for valid french DNI', () => { - let isValid = validateDni('FR1B123456789'); + let isValid = validateDni('1B123456789', 'fr'); expect(isValid).toBeTruthy(); }); - it('should return true for french DNI with exceeded digits', () => { - let isValid = validateDni('FR1B12345678910'); + it('should return false for french DNI with exceeded digits', () => { + let isValid = validateDni('1B12345678910', 'fr'); expect(isValid).toBeFalsy(); }); - it('should return true for french DNI with bad syntax', () => { - let isValid = validateDni('FR1B12345678A'); + it('should return false for french DNI with bad syntax', () => { + let isValid = validateDni('1B12345678A', 'fr'); expect(isValid).toBeFalsy(); }); @@ -61,19 +61,19 @@ describe('DNI validation', () => { describe('Italian', () => { it('should return true for valid italian DNI', () => { - let isValid = validateDni('IT12345678911'); + let isValid = validateDni('12345678911', 'it'); expect(isValid).toBeTruthy(); }); - it('should return true for italian DNI with exceeded digits', () => { - let isValid = validateDni('IT123456789112'); + it('should return false for italian DNI with exceeded digits', () => { + let isValid = validateDni('123456789112', 'it'); expect(isValid).toBeFalsy(); }); - it('should return true for italian DNI with bad syntax', () => { - let isValid = validateDni('IT1234567891A'); + it('should return false for italian DNI with bad syntax', () => { + let isValid = validateDni('1234567891A', 'it'); expect(isValid).toBeFalsy(); }); @@ -81,19 +81,19 @@ describe('DNI validation', () => { describe('Portuguese', () => { it('should return true for valid portuguese DNI', () => { - let isValid = validateDni('PT123456789'); + let isValid = validateDni('123456789', 'pt'); expect(isValid).toBeTruthy(); }); - it('should return true for portuguese DNI with exceeded digits', () => { - let isValid = validateDni('PT12345678910'); + it('should return false for portuguese DNI with exceeded digits', () => { + let isValid = validateDni('12345678910', 'pt'); expect(isValid).toBeFalsy(); }); - it('should return true for portuguese DNI with bad syntax', () => { - let isValid = validateDni('PT12345678A'); + it('should return false for portuguese DNI with bad syntax', () => { + let isValid = validateDni('12345678A', 'pt'); expect(isValid).toBeFalsy(); }); diff --git a/services/loopback/common/validations/validateDni.js b/services/loopback/common/validations/validateDni.js index 150603b7f9..82c9ee5ef1 100644 --- a/services/loopback/common/validations/validateDni.js +++ b/services/loopback/common/validations/validateDni.js @@ -1,14 +1,10 @@ -module.exports = function(fiWithCountry) { - if (fiWithCountry == null) return true; - if (typeof fiWithCountry != 'string') return false; +module.exports = function(fi, country) { + if (fi == null) return true; + if (typeof fi != 'string') return false; - fiWithCountry = fiWithCountry.toUpperCase(); + fi = fi.toUpperCase(); + country = country ? country.toLowerCase() : 'es'; - if (!/^[A-Z]{2}/.test(fiWithCountry)) - fiWithCountry = `ES${fiWithCountry}`; - - let country = fiWithCountry.substring(0, 2).toLowerCase(); - let fi = fiWithCountry.substring(2); let len = fi.length; let validators = { @@ -38,7 +34,7 @@ module.exports = function(fiWithCountry) { let sum = (pairSum + oddSum).toString(); let units = parseInt(sum.charAt(sum.length - 1)); - let control = units != 0 ? 10 - units : 0; + let control = units == 0 ? 0 : 10 - units; let index = 'JABCDEFGHI'.indexOf(lastDigit); computedDigit = index == -1 ? control.toString() : index; } else {