From 9aff95904dde0e878b29e18d3382e565a9d5d71f Mon Sep 17 00:00:00 2001 From: Joan Date: Tue, 18 Sep 2018 10:11:51 +0200 Subject: [PATCH 1/2] Added model rewriteDbError() method & fixed tag bug #597 & #662 --- client/item/src/niche/index.html | 2 +- client/item/src/tags/index.html | 11 ++-- .../item-module/04_create_item_tags.spec.js | 4 +- services/item/common/models/item-niche.js | 26 +++------ services/loopback/common/locale/es.json | 4 +- .../common/methods/vn-model/rewriteDbError.js | 53 +++++++++++++++++++ .../vn-model/specs/rewriteDbError.spec.js | 19 +++++++ services/loopback/common/models/item-tag.js | 22 +++----- services/loopback/common/models/vn-model.js | 3 +- .../common/models/ticket-observation.js | 24 +++------ 10 files changed, 106 insertions(+), 62 deletions(-) create mode 100644 services/loopback/common/methods/vn-model/rewriteDbError.js create mode 100644 services/loopback/common/methods/vn-model/specs/rewriteDbError.spec.js 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 + +
Tags @@ -20,14 +25,12 @@ vn-id="tag" vn-one field="itemTag.tagFk" - url="/item/api/Tags" - select-fields="['id','name','isFree']" + data="tags" show-field="name" label="Tag" on-change="itemTag.value = null" vn-acl="buyer" - vn-focus - disabled="itemTag.id != null"> + vn-focus> { .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(); - } */ }; From 469e6f8076d3ac2d3ec54c4f8387d8ec58846e8e Mon Sep 17 00:00:00 2001 From: Joan Date: Tue, 18 Sep 2018 13:34:11 +0200 Subject: [PATCH 2/2] fixed itemDiary warehouseFk select #671 --- client/item/src/diary/index.html | 2 +- client/item/src/diary/index.js | 24 +++++++++++------------- client/item/src/diary/index.spec.js | 11 ++++++++++- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/client/item/src/diary/index.html b/client/item/src/diary/index.html index cdc389b55..6458699e9 100644 --- a/client/item/src/diary/index.html +++ b/client/item/src/diary/index.html @@ -1,7 +1,7 @@ diff --git a/client/item/src/diary/index.js b/client/item/src/diary/index.js index 826211c27..e6ace8739 100644 --- a/client/item/src/diary/index.js +++ b/client/item/src/diary/index.js @@ -10,12 +10,11 @@ class Controller { this.$window = $window; this.$translate = $translate; } -/* - $postLink() { - if (this.item) - this.filterBuilder(); + + get item() { + return this._item; } - */ + set item(value) { this._item = value; @@ -23,14 +22,12 @@ class Controller { where: {itemFk: this.$stateParams.id} }; - if (this.$stateParams.warehouseFk) - this.warehouseFk = this.$stateParams.warehouseFk; - else if (value) - this.warehouseFk = value.itemType.warehouseFk; - } - - get item() { - return this._item; + this.$scope.$$postDigest(() => { + if (this.$stateParams.warehouseFk) + this.warehouseFk = this.$stateParams.warehouseFk; + else if (value) + this.warehouseFk = value.itemType.warehouseFk; + }); } set warehouseFk(value) { @@ -41,6 +38,7 @@ class Controller { }); this.filter.where.warehouseFk = value; + this.$scope.model.refresh(); } get warehouseFk() { diff --git a/client/item/src/diary/index.spec.js b/client/item/src/diary/index.spec.js index 4e17653ad..72c90c90f 100644 --- a/client/item/src/diary/index.spec.js +++ b/client/item/src/diary/index.spec.js @@ -1,4 +1,5 @@ import './index.js'; +import {crudModel} from '../../../helpers/crudModelHelper'; describe('Item', () => { describe('Component vnItemDiary', () => { @@ -20,7 +21,7 @@ describe('Item', () => { $httpBackend.when('GET', /\/locale\/\w+\/[a-z]{2}\.json/).respond({}); $scope = $rootScope.$new(); controller = $componentController('vnItemDiary', {$scope: $scope, $stateParams}); - controller.$scope.model = {}; + controller.$scope.model = crudModel; })); describe('isToday()', () => { @@ -75,16 +76,24 @@ describe('Item', () => { describe('set item()', () => { it(`should set warehouseFk property based on itemType warehouseFk`, () => { + spyOn(controller.$scope, '$$postDigest').and.callThrough(); controller.item = {id: 1, itemType: {warehouseFk: 1}}; + expect(controller.$scope.$$postDigest).toHaveBeenCalledWith(jasmine.any(Function)); + $scope.$digest(); + expect(controller.warehouseFk).toEqual(1); expect(controller.item.id).toEqual(1); }); it(`should set warehouseFk property based on url query warehouseFk`, () => { + spyOn(controller.$scope, '$$postDigest').and.callThrough(); controller.$stateParams.warehouseFk = 4; controller.item = {id: 1, itemType: {warehouseFk: 1}}; + expect(controller.$scope.$$postDigest).toHaveBeenCalledWith(jasmine.any(Function)); + $scope.$digest(); + expect(controller.warehouseFk).toEqual(4); expect(controller.item.id).toEqual(1); });