fixes #4927 supplier.fiscal-data permitir 'Razón social' duplicada #1230

Merged
joan merged 24 commits from 4927-permitir-razonSocial-duplicada into dev 2023-02-02 07:47:39 +00:00
1 changed files with 27 additions and 14 deletions
Showing only changes of commit f30b1ecff1 - Show all commits

View File

@ -113,24 +113,37 @@ module.exports = Self => {
throw new UserError('You can not modify is pay method checked');
});
Self.validateAsync('name', 'countryFk', hasSupplierSameName, {
message: 'A supplier with the same name already exists. Change the country.'
});
async function hasSupplierSameName(err, done) {
if (!this.name || !this.countryFk) done();
const supplier = await Self.app.models.Supplier.findOne(
{
Outdated
Review

hasChanges ya se esta evaluando en el if, no es necesario repetirlo aquí

`hasChanges` ya se esta evaluando en el if, no es necesario repetirlo aquí
where: {
name: this.name,
Outdated
Review

No es necesario evaluar hasChanges

No es necesario evaluar `hasChanges`
countryFk: this.countryFk
},
fields: ['id']
});

Esta duplicada la validació, que confirme Juan açò

Esta duplicada la validació, que confirme Juan açò
Outdated
Review

Si, en principio con la clave UNIQUE que se crea en la tabla es suficiente

Si, en principio con la clave UNIQUE que se crea en la tabla es suficiente
if (supplier && supplier.id != this.id)
err();
done();
}
Self.observe('before save', async function(ctx) {
const changes = ctx.data || ctx.instance;
const orgData = ctx.currentInstance;
const socialName = changes.name || orgData.name;
const hasChanges = orgData && changes;
const socialNameChanged = hasChanges
&& orgData.socialName != socialName;
if (hasChanges) {
const name = changes.name || orgData.name;
const nameChanged = hasChanges && orgData.name != name;
const countryFk = changes.countryFk || orgData.countryFk;
const countryChanged = hasChanges && orgData.countryFk != countryFk;
if (nameChanged || countryChanged) {
if (!isAlpha(name)) throw new UserError('The social name has an invalid format');
const supplier = await Self.app.models.Supplier.findOne({where: {name, countryFk}, fields: ['id']});
if (supplier)
throw new UserError('A supplier with the same name already exists. Change the country.');
}
}
if ((socialNameChanged) && !isAlpha(socialName))
throw new UserError('The social name has an invalid format');
});
};