#918 item.tag rellenar el color secundario gris no lo guarda

This commit is contained in:
Gerard 2018-12-19 11:20:17 +01:00
parent f89cb0e661
commit fde76cdd28
5 changed files with 103 additions and 46 deletions

View File

@ -5,11 +5,12 @@
link="{itemFk: $ctrl.$stateParams.id}"
include="$ctrl.include"
order="priority ASC"
data="itemTags">
on-row-change="$ctrl.getSourceTable(obj)"
data="$ctrl.itemTags">
</vn-crud-model>
<vn-watcher
vn-id="watcher"
data="itemTags"
data="$ctrl.itemTags"
form="form">
</vn-watcher>
<vn-crud-model
@ -20,7 +21,7 @@
<form name="form" ng-submit="$ctrl.onSubmit()" compact>
<vn-card pad-large>
<vn-title>Tags</vn-title>
<vn-horizontal ng-repeat="itemTag in itemTags">
<vn-horizontal ng-repeat="itemTag in $ctrl.itemTags">
<vn-autocomplete
vn-one
vn-id="tag"
@ -44,12 +45,12 @@
<vn-autocomplete
ng-show="tag.selection.isFree === false"
vn-three
url="{{$ctrl.getSourceTable(tag.selection)}}"
url="{{$ctrl.sourceTables[itemTag.id].url}}"
search-function="{name: {regexp: $search}}"
label="Value"
field="itemTag.value"
show-field="name"
value-field="name"
show-field="{{$ctrl.sourceTables[itemTag.id].field}}"
value-field="{{$ctrl.sourceTables[itemTag.id].field}}"
vn-acl="buyer">
</vn-autocomplete>
<vn-textfield

View File

@ -10,6 +10,40 @@ class Controller {
fields: ['id', 'name', 'isFree', 'sourceTable']
}
};
this.sourceTables = {};
}
set itemTags(value) {
if (value) {
value.forEach(tag => {
this.getSourceTable(tag);
});
this._itemTags = value;
}
}
get itemTags() {
return this._itemTags;
}
getSourceTable(obj) {
let sourceTable;
this.sourceTables[obj.id] = {};
let tag = obj.tag;
if (!tag || !tag.sourceTable && (tag.isFree === true || tag.isFree === undefined))
sourceTable = null;
else if (tag.sourceTable) {
sourceTable = '/api/' + tag.sourceTable.charAt(0).toUpperCase() +
tag.sourceTable.substring(1) + 's';
this.sourceTables[obj.id].field = 'name';
} else {
sourceTable = `/api/ItemTags/filterItemTags/${tag.id}`;
this.sourceTables[obj.id].field = 'value';
}
this.sourceTables[obj.id].url = sourceTable;
}
add() {
@ -28,16 +62,6 @@ class Controller {
return max + 1;
}
getSourceTable(selection) {
if (!selection || selection.isFree === true)
return null;
if (selection.sourceTable) {
return '/api/' + selection.sourceTable.charAt(0).toUpperCase() +
selection.sourceTable.substring(1) + 's';
} else if (selection.sourceTable == null)
return `/api/ItemTags/filterItemTags/${selection.id}`;
}
onSubmit() {
this.$scope.watcher.check();
this.$scope.model.save().then(() => {

View File

@ -1,7 +1,7 @@
import './index.js';
import {crudModel} from '../../../helpers/crudModelHelper';
describe('Item', () => {
fdescribe('Item', () => {
describe('Component vnItemTags', () => {
let $componentController;
let $scope;
@ -19,6 +19,46 @@ describe('Item', () => {
controller = $componentController('vnItemTags', {$scope});
}));
describe('itemTags setter', () => {
it('should call getSourceTable one time for each element in the value array', () => {
spyOn(controller, 'getSourceTable');
let itemTags = [
{id: 1},
{id: 2},
{id: 3},
{id: 4}
];
controller.itemTags = itemTags;
expect(controller.getSourceTable.calls.count()).toEqual(4);
expect(controller.itemTags).toEqual(itemTags);
});
});
describe('getSourceTable', () => {
it('should return null when the property isFree equals true', () => {
let tag = {id: 1};
controller.getSourceTable(tag);
expect(controller.sourceTables[1].url).toBe(null);
});
it('should return the route of the model in loopback with the first char of the string uppercase and adding a s', () => {
let tag = {id: 1, tag: {sourceTable: 'ink'}};
controller.getSourceTable(tag);
expect(controller.sourceTables[1].url).toBe('/api/Inks');
});
it('should return the route filteritemtags with the id of the selection', () => {
let tag = {id: 1, tag: {id: 3, sourceTable: null, isFree: false}};
controller.getSourceTable(tag);
expect(controller.sourceTables[1].url).toBe('/api/ItemTags/filterItemTags/3');
});
});
describe('getHighestPriority', () => {
it('should return the highest priority value + 1 from the array', () => {
let result = controller.getHighestPriority();
@ -33,28 +73,5 @@ describe('Item', () => {
expect(result).toEqual(1);
});
});
describe('getSourceTable', () => {
it('should return null when the property isFree equals true', () => {
let selection = {isFree: true};
let result = controller.getSourceTable(selection);
expect(result).toBe(null);
});
it('should return the route of the model in loopback with the first char of the string uppercase and adding a s', () => {
let selection = {sourceTable: "ink"};
let result = controller.getSourceTable(selection);
expect(result).toBe("/api/Inks");
});
it('should return the route filteritemtags with the id of the selection', () => {
let selection = {id: 3, sourceTable: null, isFree: false};
let result = controller.getSourceTable(selection);
expect(result).toBe("/api/ItemTags/filterItemTags/3");
});
});
});
});

View File

@ -1,3 +1,6 @@
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
const mergeFilters = require('../../filter.js').mergeFilters;
module.exports = Self => {
Self.remoteMethod('filterItemTags', {
description: 'Returns the distinct values of a tag property',
@ -8,6 +11,10 @@ module.exports = Self => {
required: true,
description: 'The foreign key from tag table',
http: {source: 'path'}
}, {
arg: 'filter',
type: 'Object',
description: `Filter defining where, order, offset, and limit - must be a JSON-encoded string`
}],
returns: {
root: true,
@ -19,8 +26,16 @@ module.exports = Self => {
}
});
Self.filterItemTags = async tagFk => {
let query = `SELECT DISTINCT(value) AS name FROM vn.itemTag WHERE tagFk = ?`;
return await Self.rawSql(query, [tagFk]);
Self.filterItemTags = async (tagFk, filter) => {
let conn = Self.dataSource.connector;
let where = {tagFk: tagFk};
myFilter = mergeFilters(filter, {where});
stmt = new ParameterizedSQL(
`SELECT DISTINCT(value)
FROM itemTag`);
stmt.merge(conn.makeSuffix(myFilter));
return await conn.executeStmt(stmt);
};
};

View File

@ -1,9 +1,9 @@
const app = require(`${servicesDir}/item/server/server`);
describe('item filterItemTags()', () => {
it('should call the filterItemTags method', async() => {
let [result] = await app.models.ItemTag.filterItemTags(1);
it('should filter ItemTags table', async () => {
let [result] = await app.models.ItemTag.filterItemTags(1, {});
expect(result.name).toEqual('Blue');
expect(result.value).toEqual('Blue');
});
});