diff --git a/back/models/vn-user.js b/back/models/vn-user.js index de5bf7b63..5ab0c755e 100644 --- a/back/models/vn-user.js +++ b/back/models/vn-user.js @@ -2,6 +2,7 @@ const vnModel = require('vn-loopback/common/models/vn-model'); const {Email} = require('vn-print'); const ForbiddenError = require('vn-loopback/util/forbiddenError'); const LoopBackContext = require('loopback-context'); +const validateTin = require('vn-loopback/util/validateTin'); module.exports = function(Self) { vnModel(Self); @@ -19,6 +20,27 @@ module.exports = function(Self) { // Validations + 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(); + } + Self.validatesFormatOf('email', { message: 'Invalid email', allowNull: true, diff --git a/modules/worker/back/models/worker.js b/modules/worker/back/models/worker.js index 8fb4c8a02..985d83e9f 100644 --- a/modules/worker/back/models/worker.js +++ b/modules/worker/back/models/worker.js @@ -23,25 +23,4 @@ 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 b112fc06f..e6d65221f 100644 --- a/modules/worker/front/create/index.js +++ b/modules/worker/front/create/index.js @@ -1,6 +1,5 @@ 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, $) {