Added model validations

This commit is contained in:
Joan Sanchez 2020-11-02 14:30:33 +01:00
parent 61e63cdd48
commit 590de622f5
14 changed files with 90 additions and 19 deletions

View File

@ -0,0 +1,3 @@
INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) VALUES ('Supplier', 'updateFiscalData', 'WRITE', 'ALLOW', 'ROLE', 'administrative');
INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) VALUES ('Supplier', '*', 'READ', 'ALLOW', 'ROLE', 'employee');
INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) VALUES ('SupplierLog', '*', 'READ', 'ALLOW', 'ROLE', 'employee');

View File

@ -1211,8 +1211,8 @@ INSERT INTO `vn`.`annualAverageInvoiced`(`clientFk`, `invoiced`)
INSERT INTO `vn`.`supplier`(`id`, `name`, `nickname`,`account`,`countryFk`,`nif`,`isFarmer`,`commission`, `created`, `isActive`, `street`, `city`, `provinceFk`, `postCode`, `payMethodFk`, `payDemFk`, `payDay`, `taxTypeSageFk`, `withholdingSageFk`, `transactionTypeSageFk`)
VALUES
(1, 'Plants SL', 'Plants nick', 4100000001, 1, '06089160W', 0, 0, CURDATE(), 1, 'supplier address 1', 'PONTEVEDRA', 1, 15214, 1, 1, 15, 4, 1, 1),
(2, 'Farmer King', 'The farmer', 4000020002, 1, 'B22222222', 1, 0, CURDATE(), 1, 'supplier address 2', 'SILLA', 2, 43022, 1, 2, 10, 93, 2, 8),
(442, 'Verdnatura Levante SL', 'Verdnatura', 5115000442, 1, 'C33333333', 0, 0, CURDATE(), 1, 'supplier address 3', 'SILLA', 1, 43022, 1, 2, 15, 6, 9, 3);
(2, 'Farmer King', 'The farmer', 4000020002, 1, '87945234L', 1, 0, CURDATE(), 1, 'supplier address 2', 'SILLA', 2, 43022, 1, 2, 10, 93, 2, 8),
(442, 'Verdnatura Levante SL', 'Verdnatura', 5115000442, 1, '06815934E', 0, 0, CURDATE(), 1, 'supplier address 3', 'SILLA', 1, 43022, 1, 2, 15, 6, 9, 3);
INSERT INTO `cache`.`cache_calc`(`id`, `cache_id`, `cacheName`, `params`, `last_refresh`, `expires`, `created`, `connection_id`)
VALUES

View File

@ -156,5 +156,7 @@
"shipped": "F. envío",
"landed": "F. entrega",
"addressFk": "Consignatario",
"companyFk": "Empresa"
"companyFk": "Empresa",
"The social name cannot be empty": "La razón social no puede quedar en blanco",
"The nif cannot be empty": "El NIF no puede quedar en blanco"
}

View File

@ -1,7 +1,9 @@
let request = require('request-promise-native');
let UserError = require('vn-loopback/util/user-error');
let getFinalState = require('vn-loopback/util/hook').getFinalState;
let isMultiple = require('vn-loopback/util/hook').isMultiple;
const request = require('request-promise-native');
const UserError = require('vn-loopback/util/user-error');
const getFinalState = require('vn-loopback/util/hook').getFinalState;
const isMultiple = require('vn-loopback/util/hook').isMultiple;
const validateTin = require('vn-loopback/util/validateTin');
const validateIban = require('vn-loopback/util/validateIban');
const LoopBackContext = require('loopback-context');
module.exports = Self => {
@ -63,7 +65,7 @@ module.exports = Self => {
Self.validateAsync('iban', ibanNeedsValidation, {
message: 'The IBAN does not have the correct format'
});
let validateIban = require('../validations/validateIban');
async function ibanNeedsValidation(err, done) {
let filter = {
fields: ['code'],
@ -83,7 +85,6 @@ module.exports = Self => {
message: 'Invalid TIN'
});
let validateTin = require('../validations/validateTin');
async function tinIsValid(err, done) {
if (!this.isTaxDataChecked)
return done();

View File

@ -7,7 +7,7 @@ Has to invoice: Factura
Notify by email: Notificar vía e-mail
Country: País
Street: Domicilio fiscal
City: Municipio
City: Ciudad
Postcode: Código postal
Province: Provincia
Address: Consignatario

View File

@ -57,7 +57,7 @@ describe('Supplier updateFiscalData', () => {
ctx.args = {
name: 'Weapon Dealer',
nif: 'B11111111',
nif: 'A68446004',
account: '4000000005',
sageTaxTypeFk: 5,
sageWithholdingFk: 2,
@ -65,13 +65,13 @@ describe('Supplier updateFiscalData', () => {
postCode: '46460',
city: 'VALENCIA',
provinceFk: 2,
countryFk: 2,
countryFk: 1,
};
const result = await app.models.Supplier.updateFiscalData(ctx, supplierId);
expect(result.name).toEqual('Weapon Dealer');
expect(result.nif).toEqual('B11111111');
expect(result.nif).toEqual('A68446004');
expect(result.account).toEqual('4000000005');
expect(result.sageTaxTypeFk).toEqual(5);
expect(result.sageWithholdingFk).toEqual(2);
@ -79,7 +79,7 @@ describe('Supplier updateFiscalData', () => {
expect(result.postCode).toEqual('46460');
expect(result.city).toEqual('VALENCIA');
expect(result.provinceFk).toEqual(2);
expect(result.countryFk).toEqual(2);
expect(result.countryFk).toEqual(1);
// Restores
ctx.args = defaultData;

View File

@ -1,5 +1,64 @@
const UserError = require('vn-loopback/util/user-error');
const validateTin = require('vn-loopback/util/validateTin');
module.exports = Self => {
require('../methods/supplier/filter')(Self);
require('../methods/supplier/getSummary')(Self);
require('../methods/supplier/updateFiscalData')(Self);
Self.validatesPresenceOf('name', {
message: 'The social name cannot be empty'
});
Self.validatesUniquenessOf('name', {
message: 'The supplier name must be unique'
});
Self.validatesPresenceOf('city', {
message: 'City cannot be empty'
});
Self.validatesPresenceOf('nif', {
message: 'The nif cannot be empty'
});
Self.validatesUniquenessOf('nif', {
message: 'TIN must be unique'
});
Self.validateAsync('nif', tinIsValid, {
message: 'Invalid TIN'
});
async function tinIsValid(err, 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;
if (!this.nif || !validateTin(this.nif, code))
err();
done();
}
function isAlpha(value) {
const regexp = new RegExp(/^[ñça-zA-Z0-9\s]*$/i);
return regexp.test(value);
}
Self.observe('before save', async function(ctx) {
let changes = ctx.data || ctx.instance;
let orgData = ctx.currentInstance;
const socialName = changes.name || orgData.name;
const hasChanges = orgData && changes;
const socialNameChanged = hasChanges
&& orgData.socialName != socialName;
if ((socialNameChanged) && !isAlpha(socialName))
throw new UserError('The socialName has an invalid format');
});
};

View File

@ -44,13 +44,15 @@
vn-focus
label="Social name"
ng-model="$ctrl.supplier.name"
rule
info="You can use letters and spaces">
info="You can use letters and spaces"
required="true"
rule>
</vn-textfield>
<vn-textfield
vn-one
label="Tax number"
ng-model="$ctrl.supplier.nif"
required="true"
rule>
</vn-textfield>
</vn-horizontal>
@ -120,7 +122,9 @@
url="Towns/location"
fields="['id', 'name', 'provinceFk']"
show-field="name"
value-field="name">
value-field="name"
required="true"
rule>
<tpl-item>
{{name}}, {{province.name}}
({{province.country.country}})

View File

@ -5,5 +5,6 @@ import './card';
import './descriptor';
import './index/';
import './search-panel';
import './log';
import './summary';
import './fiscal-data';
import './log';

View File

@ -54,7 +54,8 @@
"description": "Fiscal data",
"params": {
"supplier": "$ctrl.supplier"
}
},
"acl": ["administrative"]
}, {
"url" : "/log",
"state": "supplier.card.log",