This commit is contained in:
parent
b57106af48
commit
7c6798c564
|
@ -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,
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
};
|
||||
|
|
|
@ -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, $) {
|
||||
|
|
Loading…
Reference in New Issue