From b57106af483b482776f62aec82798d78d3c985f6 Mon Sep 17 00:00:00 2001 From: carlossa Date: Tue, 14 Nov 2023 11:13:33 +0100 Subject: [PATCH] refs #6291 validateTin --- modules/worker/back/models/worker.js | 21 +++++++++++++++++++++ modules/worker/front/create/index.js | 1 + 2 files changed, 22 insertions(+) diff --git a/modules/worker/back/models/worker.js b/modules/worker/back/models/worker.js index 985d83e9f..8fb4c8a02 100644 --- a/modules/worker/back/models/worker.js +++ b/modules/worker/back/models/worker.js @@ -23,4 +23,25 @@ module.exports = Self => { Self.validatesUniquenessOf('locker', { message: 'This locker has already been assigned' }); + + Self.validateAsync('fi', tinIsValid, { + message: 'Invalid TIN' + }); + + async function tinIsValid(err, done) { + if (!this.isTaxDataChecked) + return done(); + + const filter = { + fields: ['code'], + where: {id: this.countryFk} + }; + const country = await Self.app.models.Country.findOne(filter); + const code = country ? country.code.toLowerCase() : null; + const countryCode = this.fi?.toLowerCase().substring(0, 2); + + if (!this.fi || !validateTin(this.fi, code) || (this.isVies && countryCode == code)) + err(); + done(); + } }; diff --git a/modules/worker/front/create/index.js b/modules/worker/front/create/index.js index e6d65221f..b112fc06f 100644 --- a/modules/worker/front/create/index.js +++ b/modules/worker/front/create/index.js @@ -1,5 +1,6 @@ import ngModule from '../module'; import Section from 'salix/components/section'; +const validateTin = require('vn-loopback/util/validateTin'); export default class Controller extends Section { constructor($element, $) {