diff --git a/client/item/src/niche/index.html b/client/item/src/niche/index.html
index 5cba83f51..e4c8342f9 100644
--- a/client/item/src/niche/index.html
+++ b/client/item/src/niche/index.html
@@ -15,7 +15,7 @@
Niches
+
+
{
.click(selectors.itemBasicData.basicDataButton)
.wait(selectors.itemBasicData.nameInput)
.click(selectors.itemTags.tagsButton)
- .waitForTextInInput(selectors.itemTags.firstTagSelect, 'Ancho de la base')
+ .waitForTextInInput(selectors.itemTags.firstTagSelect, 'Diámetro')
.getInputValue(selectors.itemTags.firstTagSelect)
.then(result => {
- expect(result).toEqual('Ancho de la base');
+ expect(result).toEqual('Diámetro');
});
});
diff --git a/services/item/common/models/item-niche.js b/services/item/common/models/item-niche.js
index b9b52c180..7cc683efb 100644
--- a/services/item/common/models/item-niche.js
+++ b/services/item/common/models/item-niche.js
@@ -1,23 +1,9 @@
+const UserError = require('vn-loopback/common/helpers').UserError;
+
module.exports = Self => {
-/* Self.validateAsync('warehouseFk', validateWarehouseUniqueness, {
- message: `The warehouse can't be repeated`
+ Self.rewriteDbError(function(err) {
+ if (err.code === 'ER_DUP_ENTRY')
+ return new UserError(`The warehouse can't be repeated`);
+ return err;
});
-
- async function validateWarehouseUniqueness(err, done) {
- let where = {
- itemFk: this.itemFk,
- warehouseFk: this.warehouseFk
- };
-
- if (this.id != null)
- where.id = {neq: this.id};
-
- let warehouseExists = await Self.findOne({where: where});
- console.log(warehouseExists);
-
- if (warehouseExists)
- err();
-
- done();
- } */
};
diff --git a/services/loopback/common/locale/es.json b/services/loopback/common/locale/es.json
index f13c016c2..609f8a903 100644
--- a/services/loopback/common/locale/es.json
+++ b/services/loopback/common/locale/es.json
@@ -47,5 +47,7 @@
"The warehouse can't be repeated": "El almacén no puede repetirse",
"The tag can't be repeated": "El tag no puede repetirse",
"The observation type can't be repeated": "El tipo de observación no puede repetirse",
- "A claim with that sale already exists": "Ya existe una reclamación para esta línea"
+ "A claim with that sale already exists": "Ya existe una reclamación para esta línea",
+ "asdas": "asdas",
+ "Duplicated warehouse": "Duplicated warehouse"
}
\ No newline at end of file
diff --git a/services/loopback/common/methods/vn-model/rewriteDbError.js b/services/loopback/common/methods/vn-model/rewriteDbError.js
new file mode 100644
index 000000000..57f49202d
--- /dev/null
+++ b/services/loopback/common/methods/vn-model/rewriteDbError.js
@@ -0,0 +1,53 @@
+module.exports = Self => {
+ Self.rewriteDbError = function(replaceErrFunc) {
+ this.once('attached', () => {
+ let realUpsert = this.upsert;
+ this.upsert = async(data, options, cb) => {
+ if (options instanceof Function) {
+ cb = options;
+ options = null;
+ }
+
+ try {
+ await realUpsert.call(this, data, options);
+ if (cb) cb();
+ } catch (err) {
+ let myErr = replaceErr(err, replaceErrFunc);
+ if (cb)
+ cb(myErr);
+ else
+ throw myErr;
+ }
+ };
+
+ let realCreate = this.create;
+ this.create = async(data, options, cb) => {
+ if (options instanceof Function) {
+ cb = options;
+ options = null;
+ }
+
+ try {
+ await realCreate.call(this, data, options);
+ if (cb) cb();
+ } catch (err) {
+ let myErr = replaceErr(err, replaceErrFunc);
+ if (cb)
+ cb(myErr);
+ else
+ throw myErr;
+ }
+ };
+ });
+ };
+
+ function replaceErr(err, replaceErrFunc) {
+ if (Array.isArray(err)) {
+ let errs = [];
+ for (let e of err)
+ errs.push(replaceErrFunc(e));
+ return errs;
+ }
+ return replaceErrFunc(err);
+ }
+};
diff --git a/services/loopback/common/methods/vn-model/specs/rewriteDbError.spec.js b/services/loopback/common/methods/vn-model/specs/rewriteDbError.spec.js
new file mode 100644
index 000000000..84777770d
--- /dev/null
+++ b/services/loopback/common/methods/vn-model/specs/rewriteDbError.spec.js
@@ -0,0 +1,19 @@
+const app = require('../../../../../item/server/server');
+
+describe('Model rewriteDbError()', () => {
+ it('should extend rewriteDbError properties to any model passed', () => {
+ let exampleModel = app.models.ItemNiche;
+
+ expect(exampleModel.rewriteDbError).toBeDefined();
+ });
+
+ it('should handle a duplicated warehouse error', async() => {
+ let itemNiche = {itemFk: 1, warehouseFK: 1, code: 'A1'};
+ let error;
+ await app.models.ItemNiche.create(itemNiche).catch(e => {
+ error = e;
+ }).finally(() => {
+ expect(error.message).toEqual(`The warehouse can't be repeated`);
+ });
+ });
+});
diff --git a/services/loopback/common/models/item-tag.js b/services/loopback/common/models/item-tag.js
index 837c656d1..5521ef68c 100644
--- a/services/loopback/common/models/item-tag.js
+++ b/services/loopback/common/models/item-tag.js
@@ -1,21 +1,11 @@
+let UserError = require('../helpers').UserError;
+
module.exports = Self => {
require('../methods/item-tag/filterItemTags')(Self);
-/* Self.validateAsync('tagFk', validateTagUniqueness, {
- message: `The tag can't be repeated`
+ Self.rewriteDbError(function(err) {
+ if (err.code === 'ER_DUP_ENTRY')
+ return new UserError(`The tag can't be repeated`);
+ return err;
});
-
- async function validateTagUniqueness(err, done) {
- let tagExists = await Self.findOne({
- where: {
- itemFk: this.itemFk,
- tagFk: this.tagFk
- }
- });
-
- if (tagExists)
- err();
-
- done();
- } */
};
diff --git a/services/loopback/common/models/vn-model.js b/services/loopback/common/models/vn-model.js
index b9d18d912..deaf64387 100644
--- a/services/loopback/common/models/vn-model.js
+++ b/services/loopback/common/models/vn-model.js
@@ -155,7 +155,7 @@ module.exports = function(Self) {
});
await Promise.all(promises);
} catch (error) {
- throw error[0];
+ throw error;
}
}
if (actions.create && actions.create.length) {
@@ -258,4 +258,5 @@ module.exports = function(Self) {
require('../methods/vn-model/installMethod')(Self);
require('../methods/vn-model/validateBinded')(Self);
+ require('../methods/vn-model/rewriteDbError')(Self);
};
diff --git a/services/ticket/common/models/ticket-observation.js b/services/ticket/common/models/ticket-observation.js
index 50fc68331..3d15e9b0e 100644
--- a/services/ticket/common/models/ticket-observation.js
+++ b/services/ticket/common/models/ticket-observation.js
@@ -1,19 +1,9 @@
-module.exports = function(Self) {
- /* Self.validateAsync('observationTypeFk', validateObservationUniqueness, {
- message: `The observation type can't be repeated`
+const UserError = require('vn-loopback/common/helpers').UserError;
+
+module.exports = Self => {
+ Self.rewriteDbError(function(err) {
+ if (err.code === 'ER_DUP_ENTRY')
+ return new UserError(`The observation type can't be repeated`);
+ return err;
});
-
- async function validateObservationUniqueness(err, done) {
- let observationExists = await Self.findOne({
- where: {
- ticketFk: this.ticketFk,
- observationTypeFk: this.observationTypeFk
- }
- });
-
- if (observationExists)
- err();
-
- done();
- } */
};