From 63ac4928451ac059c4a0edaaa945e8a8c893872f Mon Sep 17 00:00:00 2001 From: jorgebl Date: Fri, 5 Mar 2021 08:45:14 +0100 Subject: [PATCH] Added message for duplicated names --- loopback/locale/es.json | 4 +++- modules/item/back/models/genus.js | 12 ++++++------ modules/item/back/models/specie.js | 9 +++++++++ modules/item/back/models/species.js | 9 --------- 4 files changed, 18 insertions(+), 16 deletions(-) create mode 100644 modules/item/back/models/specie.js delete mode 100644 modules/item/back/models/species.js diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 174a9e093..8d5156842 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -174,5 +174,7 @@ "That item doesn't exists": "Ese artículo no existe", "There's a new urgent ticket": "Hay un nuevo ticket urgente: [{{title}}](https://cau.verdnatura.es/WorkOrder.do?woMode=viewWO&woID={{issueId}})", "Invalid account": "Cuenta inválida", - "Compensation account is empty": "La cuenta para compensar está vacia" + "Compensation account is empty": "La cuenta para compensar está vacia", + "This genus already exist": "Este genus ya existe", + "This specie already exist": "Esta especie ya existe" } \ No newline at end of file diff --git a/modules/item/back/models/genus.js b/modules/item/back/models/genus.js index 462d5efd4..c105e06b4 100644 --- a/modules/item/back/models/genus.js +++ b/modules/item/back/models/genus.js @@ -1,9 +1,9 @@ -module.exports = Self => { - Self.validatesPresenceOf('name', { - message: 'Name cannot be blank' - }); +let UserError = require('vn-loopback/util/user-error'); - Self.validatesUniquenessOf('id', { - message: 'This name already exist.' +module.exports = Self => { + Self.rewriteDbError(function(err) { + if (err.code === 'ER_DUP_ENTRY') + return new UserError(`This genus already exist`); + return err; }); }; diff --git a/modules/item/back/models/specie.js b/modules/item/back/models/specie.js new file mode 100644 index 000000000..e6a03f851 --- /dev/null +++ b/modules/item/back/models/specie.js @@ -0,0 +1,9 @@ +let UserError = require('vn-loopback/util/user-error'); + +module.exports = Self => { + Self.rewriteDbError(function(err) { + if (err.code === 'ER_DUP_ENTRY') + return new UserError(`This specie already exist`); + return err; + }); +}; diff --git a/modules/item/back/models/species.js b/modules/item/back/models/species.js deleted file mode 100644 index 462d5efd4..000000000 --- a/modules/item/back/models/species.js +++ /dev/null @@ -1,9 +0,0 @@ -module.exports = Self => { - Self.validatesPresenceOf('name', { - message: 'Name cannot be blank' - }); - - Self.validatesUniquenessOf('id', { - message: 'This name already exist.' - }); -};