From cdbbec9d36619a3c8e4db4ff06c9ba2504f8cda2 Mon Sep 17 00:00:00 2001 From: carlossa Date: Fri, 28 Apr 2023 10:21:54 +0200 Subject: [PATCH] refs #5540 validate --- loopback/util/validateNIF.js | 16 +++++++++------- modules/client/back/models/client.js | 12 +++++++++++- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/loopback/util/validateNIF.js b/loopback/util/validateNIF.js index 22c10d612..041ca8876 100644 --- a/loopback/util/validateNIF.js +++ b/loopback/util/validateNIF.js @@ -1,12 +1,14 @@ -module.exports = async function(fi) { +const models = require('vn-loopback/server/server').models; +module.exports = async function(fi, isVies, countryFk) { const filter = { fields: ['code'], - where: {id: this.countryFk} + where: {id: countryFk} }; - const countryCode = this.fi.toUpperCase().substring(0, 2); - const country = await app.models.Country.findOne(filter); + const countryCode = fi.toUpperCase().substring(0, 2); + const country = await models.Country.findOne(filter); const code = country ? country.code : null; - if (this.isVies && countryCode == code) - err(); - done(); + if (isVies && countryCode == code) + return false; + return true; }; + diff --git a/modules/client/back/models/client.js b/modules/client/back/models/client.js index d51a67d05..972355e3e 100644 --- a/modules/client/back/models/client.js +++ b/modules/client/back/models/client.js @@ -24,7 +24,7 @@ module.exports = Self => { message: 'TIN must be unique' }); - Self.validateAsync('fi', validateNIF(fi), { + Self.validateAsync(validateNIF(this.fi, this.isVies, this.countryFk), { message: 'Invalid TIN' }); @@ -93,6 +93,16 @@ module.exports = Self => { done(); } + Self.validateAsync('fi', validationNIF, { + message: 'El NIF es incorrecto por el Vies' + }); + + async function validationNIF(err, done){ + if (!validateNIF(this.fi, this.isVies, this.countryFk)) + err(); + done(); + } + Self.validateAsync('fi', tinIsValid, { message: 'Invalid TIN' });