refactor(item): activeBuyers
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Alex Moreno 2021-11-16 13:56:09 +01:00
parent 23b567046b
commit f27420c14a
6 changed files with 50 additions and 84 deletions

View File

@ -1,42 +0,0 @@
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
const buildFilter = require('vn-loopback/util/filter').buildFilter;
const mergeFilters = require('vn-loopback/util/filter').mergeFilters;
module.exports = Self => {
Self.activeWorkers = async(query, filter) => {
let conn = Self.dataSource.connector;
if (filter.where && filter.where.and && Array.isArray(filter.where.and)) {
let where = {};
filter.where.and.forEach(element => {
where[Object.keys(element)[0]] = Object.values(element)[0];
});
filter.where = where;
}
let clientFilter = Object.assign({}, filter);
clientFilter.where = buildFilter(filter.where, (param, value) => {
switch (param) {
case 'role':
return {'r.name': value};
case 'firstName':
return {or: [
{'w.firstName': {like: `%${value}%`}},
{'w.lastName': {like: `%${value}%`}},
{'u.name': {like: `%${value}%`}},
{'u.nickname': {like: `%${value}%`}}
]};
case 'id':
return {'w.id': value};
}
});
let myFilter = {
where: {'u.active': true}
};
myFilter = mergeFilters(myFilter, clientFilter);
let stmt = new ParameterizedSQL(query);
stmt.merge(conn.makeSuffix(myFilter));
return conn.executeStmt(stmt);
};
};

View File

@ -0,0 +1,45 @@
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
const mergeFilters = require('vn-loopback/util/filter').mergeFilters;
module.exports = Self => {
Self.remoteMethod('activeBuyers', {
description: 'Returns a list of agencies from a warehouse',
accepts: [{
arg: 'filter',
type: 'object',
description: `Filter defining where, order, offset, and limit - must be a JSON-encoded string`
}],
returns: {
type: ['object'],
root: true
},
http: {
path: `/activeBuyers`,
verb: 'GET'
}
});
Self.activeBuyers = async(filter, options) => {
const conn = Self.dataSource.connector;
const where = {isActive: true};
let myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
filter = mergeFilters(filter, {where});
let stmt = new ParameterizedSQL(
`SELECT DISTINCT w.id workerFk, w.firstName, w.lastName, u.name, u.nickname
FROM worker w
JOIN itemType it ON it.workerFk = w.id
JOIN account.user u ON u.id = w.id
JOIN item i ON i.typeFk = it.id`,
null, myOptions);
stmt.merge(conn.makeSuffix(filter));
console.log(stmt);
return conn.executeStmt(stmt);
};
};

View File

@ -1,35 +0,0 @@
module.exports = Self => {
Self.remoteMethod('activeWithBuyer', {
description: 'Returns active workers in itemType',
accessType: 'READ',
accepts: [{
arg: 'filter',
type: 'Object',
description: 'Filter defining where and paginated data',
required: false
}],
returns: {
type: ['object'],
root: true
},
http: {
path: `/activeWithBuyer`,
verb: 'GET'
}
});
Self.activeWithBuyer = async(filter, options) => {
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const query =
`SELECT DISTINCT w.id, w.firstName, w.lastName, u.name, u.nickname
FROM worker w
JOIN itemType i ON i.workerFk = w.id
JOIN account.user u ON u.id = w.id`;
return Self.activeWorkers(query, filter, myOptions);
};
};

View File

@ -44,7 +44,7 @@ module.exports = Self => {
description: 'Whether the the item is or not active',
},
{
arg: 'salesPersonFk',
arg: 'buyerFk',
type: 'integer',
description: 'The buyer of the item',
},

View File

@ -14,8 +14,7 @@ module.exports = Self => {
require('../methods/item/getWasteByWorker')(Self);
require('../methods/item/getWasteByItem')(Self);
require('../methods/item/createIntrastat')(Self);
require('../methods/item/active')(Self);
require('../methods/item/activeWithBuyer')(Self);
require('../methods/item/activeBuyers')(Self);
Self.validatesPresenceOf('originFk', {message: 'Cannot be blank'});

View File

@ -40,12 +40,11 @@
<vn-horizontal>
<vn-autocomplete
vn-one
disabled="false"
ng-model="filter.salesPersonFk"
url="Items/activeWithBuyer"
ng-model="filter.buyerFk"
url="Items/activeBuyers"
show-field="nickname"
search-function="{firstName: $search}"
value-field="id"
value-field="workerFk"
label="Buyer">
</vn-autocomplete>
</vn-horizontal>