Added model rewriteDbError() method & fixed tag bug #597 & #662

This commit is contained in:
Joan Sanchez 2018-09-18 10:11:51 +02:00
parent 5c6ab8efc3
commit 9aff95904d
10 changed files with 106 additions and 62 deletions

View File

@ -15,7 +15,7 @@
<vn-title>Niches</vn-title> <vn-title>Niches</vn-title>
<vn-horizontal ng-repeat="niche in niches track by $index"> <vn-horizontal ng-repeat="niche in niches track by $index">
<vn-autocomplete <vn-autocomplete
disabled="niche.id != null"
url="/item/api/Warehouses" url="/item/api/Warehouses"
show-field="name" show-field="name"
value-field="id" value-field="id"

View File

@ -12,6 +12,11 @@
data="itemTags" data="itemTags"
form="form"> form="form">
</vn-watcher> </vn-watcher>
<vn-crud-model
url="/item/api/Tags"
fields="['id','name','isFree', 'sourceTable']"
data="tags">
</vn-crud-model>
<form name="form" ng-submit="$ctrl.onSubmit()"> <form name="form" ng-submit="$ctrl.onSubmit()">
<vn-card pad-large> <vn-card pad-large>
<vn-title>Tags</vn-title> <vn-title>Tags</vn-title>
@ -20,14 +25,12 @@
vn-id="tag" vn-id="tag"
vn-one vn-one
field="itemTag.tagFk" field="itemTag.tagFk"
url="/item/api/Tags" data="tags"
select-fields="['id','name','isFree']"
show-field="name" show-field="name"
label="Tag" label="Tag"
on-change="itemTag.value = null" on-change="itemTag.value = null"
vn-acl="buyer" vn-acl="buyer"
vn-focus vn-focus>
disabled="itemTag.id != null">
</vn-autocomplete> </vn-autocomplete>
<vn-textfield <vn-textfield
ng-show="tag.selection.isFree || tag.selection.isFree == undefined" ng-show="tag.selection.isFree || tag.selection.isFree == undefined"

View File

@ -65,10 +65,10 @@ describe('Item', () => {
.click(selectors.itemBasicData.basicDataButton) .click(selectors.itemBasicData.basicDataButton)
.wait(selectors.itemBasicData.nameInput) .wait(selectors.itemBasicData.nameInput)
.click(selectors.itemTags.tagsButton) .click(selectors.itemTags.tagsButton)
.waitForTextInInput(selectors.itemTags.firstTagSelect, 'Ancho de la base') .waitForTextInInput(selectors.itemTags.firstTagSelect, 'Diámetro')
.getInputValue(selectors.itemTags.firstTagSelect) .getInputValue(selectors.itemTags.firstTagSelect)
.then(result => { .then(result => {
expect(result).toEqual('Ancho de la base'); expect(result).toEqual('Diámetro');
}); });
}); });

View File

@ -1,23 +1,9 @@
const UserError = require('vn-loopback/common/helpers').UserError;
module.exports = Self => { module.exports = Self => {
/* Self.validateAsync('warehouseFk', validateWarehouseUniqueness, { Self.rewriteDbError(function(err) {
message: `The warehouse can't be repeated` 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();
} */
}; };

View File

@ -47,5 +47,7 @@
"The warehouse can't be repeated": "El almacén no puede repetirse", "The warehouse can't be repeated": "El almacén no puede repetirse",
"The tag can't be repeated": "El tag 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", "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"
} }

View File

@ -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);
}
};

View File

@ -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`);
});
});
});

View File

@ -1,21 +1,11 @@
let UserError = require('../helpers').UserError;
module.exports = Self => { module.exports = Self => {
require('../methods/item-tag/filterItemTags')(Self); require('../methods/item-tag/filterItemTags')(Self);
/* Self.validateAsync('tagFk', validateTagUniqueness, { Self.rewriteDbError(function(err) {
message: `The tag can't be repeated` 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();
} */
}; };

View File

@ -155,7 +155,7 @@ module.exports = function(Self) {
}); });
await Promise.all(promises); await Promise.all(promises);
} catch (error) { } catch (error) {
throw error[0]; throw error;
} }
} }
if (actions.create && actions.create.length) { if (actions.create && actions.create.length) {
@ -258,4 +258,5 @@ module.exports = function(Self) {
require('../methods/vn-model/installMethod')(Self); require('../methods/vn-model/installMethod')(Self);
require('../methods/vn-model/validateBinded')(Self); require('../methods/vn-model/validateBinded')(Self);
require('../methods/vn-model/rewriteDbError')(Self);
}; };

View File

@ -1,19 +1,9 @@
module.exports = function(Self) { const UserError = require('vn-loopback/common/helpers').UserError;
/* Self.validateAsync('observationTypeFk', validateObservationUniqueness, {
message: `The observation type can't be repeated` 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();
} */
}; };