refs #4927 modified hook to check name & country
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Alexandre Riera 2023-01-13 08:38:29 +01:00
parent 8b61fa8b02
commit 41671e8a3a
2 changed files with 17 additions and 8 deletions

View File

@ -252,5 +252,6 @@
"Receipt's bank was not found": "No se encontró el banco del recibo",
"This receipt was not compensated": "Este recibo no ha sido compensado",
"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."
}

View File

@ -116,13 +116,21 @@ module.exports = Self => {
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 ((socialNameChanged) && !isAlpha(socialName))
throw new UserError('The social name has an invalid format');
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.');
}
}
});
};