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-horizontal ng-repeat="niche in niches track by $index">
<vn-autocomplete
disabled="niche.id != null"
url="/item/api/Warehouses"
show-field="name"
value-field="id"

View File

@ -12,6 +12,11 @@
data="itemTags"
form="form">
</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()">
<vn-card pad-large>
<vn-title>Tags</vn-title>
@ -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>
</vn-autocomplete>
<vn-textfield
ng-show="tag.selection.isFree || tag.selection.isFree == undefined"

View File

@ -65,10 +65,10 @@ describe('Item', () => {
.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');
});
});

View File

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

View File

@ -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"
}

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 => {
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();
} */
};

View File

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

View File

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