diff --git a/modules/client/front/address/edit/index.js b/modules/client/front/address/edit/index.js index d588812fa..30201b880 100644 --- a/modules/client/front/address/edit/index.js +++ b/modules/client/front/address/edit/index.js @@ -52,7 +52,7 @@ export default class Controller extends Section { if (!this.address.provinceFk) this.address.provinceFk = province.id; - if (postcodes.length === 1) + if (!this.address.postalCode && postcodes.length === 1) this.address.postalCode = postcodes[0].code; } diff --git a/modules/client/front/fiscal-data/index.js b/modules/client/front/fiscal-data/index.js index 6aed6e304..65129d3f8 100644 --- a/modules/client/front/fiscal-data/index.js +++ b/modules/client/front/fiscal-data/index.js @@ -128,7 +128,7 @@ export default class Controller extends Section { if (!this.client.countryFk) this.client.countryFk = country.id; - if (postcodes.length === 1) + if (!this.client.postcode && postcodes.length === 1) this.client.postcode = postcodes[0].code; } diff --git a/modules/supplier/back/models/supplier.js b/modules/supplier/back/models/supplier.js index 1e5773a6b..37c94c266 100644 --- a/modules/supplier/back/models/supplier.js +++ b/modules/supplier/back/models/supplier.js @@ -30,6 +30,27 @@ module.exports = Self => { message: 'Invalid TIN' }); + Self.validatesLengthOf('postCode', { + allowNull: true, + allowBlank: true, + min: 3, max: 10 + }); + + Self.validateAsync('postCode', hasValidPostcode, { + message: `The postcode doesn't exist. Please enter a correct one` + }); + + async function hasValidPostcode(err, done) { + if (!this.postcode) + return done(); + + const models = Self.app.models; + const postcode = await models.Postcode.findById(this.postcode); + + if (!postcode) err(); + done(); + } + async function tinIsValid(err, done) { const filter = { fields: ['code'], diff --git a/modules/supplier/front/fiscal-data/index.html b/modules/supplier/front/fiscal-data/index.html index a36a98441..6dda60941 100644 --- a/modules/supplier/front/fiscal-data/index.html +++ b/modules/supplier/front/fiscal-data/index.html @@ -59,7 +59,7 @@ diff --git a/modules/supplier/front/fiscal-data/index.js b/modules/supplier/front/fiscal-data/index.js index 56588247b..f2929c91f 100644 --- a/modules/supplier/front/fiscal-data/index.js +++ b/modules/supplier/front/fiscal-data/index.js @@ -38,7 +38,7 @@ export default class Controller extends Section { if (!this.supplier.countryFk) this.supplier.countryFk = country.id; - if (postcodes.length === 1) + if (!this.supplier.postCode && postcodes.length === 1) this.supplier.postCode = postcodes[0].code; }