fixes #4927 supplier.fiscal-data permitir 'Razón social' duplicada #1230
|
@ -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(
|
||||
{
|
||||
|
||||
where: {
|
||||
name: this.name,
|
||||
juan
commented
No es necesario evaluar No es necesario evaluar `hasChanges`
|
||||
countryFk: this.countryFk
|
||||
},
|
||||
fields: ['id']
|
||||
});
|
||||
|
||||
jgallego
commented
Esta duplicada la validació, que confirme Juan açò Esta duplicada la validació, que confirme Juan açò
juan
commented
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');
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue
hasChanges
ya se esta evaluando en el if, no es necesario repetirlo aquí