#837 crear refactor de activeSalesPerson, activeBuyer en uno solo

This commit is contained in:
Gerard 2018-11-27 08:50:13 +01:00
parent 23d8d43fbe
commit 0b74df5dcb
4 changed files with 67 additions and 126 deletions

View File

@ -1,62 +0,0 @@
module.exports = Self => {
Self.remoteMethod('activeBuyer', {
description: 'Returns actives workers with Buyer role',
accessType: 'READ',
accepts: [{
arg: 'filter',
type: 'Object',
required: false,
description: 'Filter defining where and paginated data',
http: {source: 'query'}
}],
returns: {
type: 'Worker',
root: true
},
http: {
path: `/activeBuyer`,
verb: 'get'
}
});
Self.activeBuyer = async filter => {
let sqlWhere = '';
let sqlLimit = '';
let sqlOffset = '';
let params = [];
if (filter.where) {
if (filter.where.firstName) {
sqlWhere = `AND (worker.firstName LIKE ? OR worker.name LIKE ?)`;
let search = where.firstName.like;
params.push(search);
params.push(search);
}
if (filter.where.id) {
sqlWhere = `AND worker.id = ?`;
params.push(filter.where.id);
}
}
if (filter.limit) {
sqlLimit = `LIMIT ?`;
params.push(filter.limit);
}
if (filter.skip) {
sqlOffset = `OFFSET ?`;
params.push(filter.skip);
}
let query =
`SELECT worker.id, worker.firstName, worker.name
FROM vn.worker
JOIN account.user user ON user.id = worker.userFk
JOIN account.role role ON role.name = 'buyer'
JOIN account.roleRole inheritance ON inheritance.role = user.role AND inheritance.inheritsFrom = role.id
WHERE user.active IS TRUE ${sqlWhere}
ORDER BY worker.firstName ASC
${sqlLimit} ${sqlOffset}`;
return await Self.rawSql(query, params);
};
};

View File

@ -1,62 +0,0 @@
module.exports = Self => {
Self.remoteMethod('activeSalesPerson', {
description: 'Returns actives workers with salesperson role',
accessType: 'READ',
accepts: [{
arg: 'filter',
type: 'Object',
required: false,
description: 'Filter defining where and paginated data',
http: {source: 'query'}
}],
returns: {
type: 'Worker',
root: true
},
http: {
path: `/activeSalesPerson`,
verb: 'get'
}
});
Self.activeSalesPerson = async filter => {
let sqlWhere = '';
let sqlLimit = '';
let sqlOffset = '';
let params = [];
if (filter.where) {
if (filter.where.firstName) {
sqlWhere = `AND (worker.firstName LIKE ? OR worker.name LIKE ?)`;
let search = where.firstName.like;
params.push(search);
params.push(search);
}
if (filter.where.id) {
sqlWhere = `AND worker.id = ?`;
params.push(filter.where.id);
}
}
if (filter.limit) {
sqlLimit = `LIMIT ?`;
params.push(filter.limit);
}
if (filter.skip) {
sqlOffset = `OFFSET ?`;
params.push(filter.skip);
}
let query =
`SELECT worker.id, worker.firstName, worker.name
FROM vn.worker
JOIN account.user user ON user.id = worker.userFk
JOIN account.role role ON role.name = 'salesPerson'
JOIN account.roleRole inheritance ON inheritance.role = user.role AND inheritance.inheritsFrom = role.id
WHERE user.active IS TRUE ${sqlWhere}
ORDER BY worker.firstName ASC
${sqlLimit} ${sqlOffset}`;
return await Self.rawSql(query, params);
};
};

View File

@ -0,0 +1,66 @@
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
const buildFilter = require('../../filter.js').buildFilter;
const mergeFilters = require('../../filter.js').mergeFilters;
module.exports = Self => {
Self.remoteMethod('activeWorkersWithRole', {
description: 'Returns actives workers with salesperson role',
accessType: 'READ',
accepts: [{
arg: 'filter',
type: 'Object',
description: 'Filter defining where and paginated data',
required: true
}],
returns: {
type: 'Worker',
root: true
},
http: {
path: `/activeWorkersWithRole`,
verb: 'get'
}
});
Self.activeWorkersWithRole = async filter => {
let conn = Self.dataSource.connector;
if (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 {
'w.firstName': {like: `%${value}%`},
'w.name': {like: `%${value}%`},
};
case 'id':
return {'w.id': value};
}
});
let myFilter = {
where: {'u.active': true}
};
console.log(filter.where);
myFilter = mergeFilters(myFilter, clientFilter);
let stmt = new ParameterizedSQL(
`SELECT DISTINCT w.id, w.firstName, w.name
FROM worker w
JOIN account.user u ON u.id = w.userFk
JOIN account.roleRole i ON i.role = u.role
JOIN account.role r ON r.id = i.inheritsFrom`
);
stmt.merge(conn.makeSuffix(myFilter));
return await conn.executeStmt(stmt);
};
};

View File

@ -4,13 +4,12 @@ let isMultiple = require('../helpers').isMultiple;
module.exports = Self => { module.exports = Self => {
// Methods // Methods
require('../methods/client/activeWorkersWithRole')(Self);
require('../methods/client/getCard')(Self); require('../methods/client/getCard')(Self);
require('../methods/client/createWithUser')(Self); require('../methods/client/createWithUser')(Self);
require('../methods/client/listWorkers')(Self); require('../methods/client/listWorkers')(Self);
require('../methods/client/hasCustomerRole')(Self); require('../methods/client/hasCustomerRole')(Self);
require('../methods/client/isValidClient')(Self); require('../methods/client/isValidClient')(Self);
require('../methods/client/activeSalesPerson')(Self);
require('../methods/client/activeBuyer')(Self);
require('../methods/client/addressesPropagateRe')(Self); require('../methods/client/addressesPropagateRe')(Self);
require('../methods/client/getDebt')(Self); require('../methods/client/getDebt')(Self);
require('../methods/client/getMana')(Self); require('../methods/client/getMana')(Self);