refs #4927 modified hook to check name & country
This commit is contained in:
parent
8b61fa8b02
commit
41671e8a3a
|
@ -252,5 +252,6 @@
|
||||||
"Receipt's bank was not found": "No se encontró el banco del recibo",
|
"Receipt's bank was not found": "No se encontró el banco del recibo",
|
||||||
"This receipt was not compensated": "Este recibo no ha sido compensado",
|
"This receipt was not compensated": "Este recibo no ha sido compensado",
|
||||||
"Client's email was not found": "No se encontró el email del cliente",
|
"Client's email was not found": "No se encontró el email del cliente",
|
||||||
"Aplicación bloqueada por el usuario 9": "Aplicación bloqueada por el usuario 9"
|
"Aplicación bloqueada por el usuario 9": "Aplicación bloqueada por el usuario 9",
|
||||||
|
"A supplier with the same name already exists. Change the country.": "Un proveedor con el mismo nombre ya existe. Cambie el país."
|
||||||
}
|
}
|
|
@ -116,13 +116,21 @@ module.exports = Self => {
|
||||||
Self.observe('before save', async function(ctx) {
|
Self.observe('before save', async function(ctx) {
|
||||||
const changes = ctx.data || ctx.instance;
|
const changes = ctx.data || ctx.instance;
|
||||||
const orgData = ctx.currentInstance;
|
const orgData = ctx.currentInstance;
|
||||||
|
|
||||||
const socialName = changes.name || orgData.name;
|
|
||||||
const hasChanges = orgData && changes;
|
const hasChanges = orgData && changes;
|
||||||
const socialNameChanged = hasChanges
|
|
||||||
&& orgData.socialName != socialName;
|
|
||||||
|
|
||||||
if ((socialNameChanged) && !isAlpha(socialName))
|
if (hasChanges) {
|
||||||
throw new UserError('The social name has an invalid format');
|
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.');
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue