fix: code for search-function

This commit is contained in:
Vicent Llopis 2022-09-22 13:54:58 +02:00
parent 0fb7487191
commit 95e5b1a970
1 changed files with 26 additions and 4 deletions

View File

@ -1,8 +1,15 @@
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
module.exports = Self => {
Self.remoteMethodCtx('getItemTypeWorker', {
description: 'Returns the workers that appear in itemType',
accessType: 'READ',
accepts: [],
accepts: [{
arg: 'filter',
type: 'Object',
description: 'Filter defining where and paginated data',
required: true
}],
returns: {
type: ['object'],
root: true
@ -13,8 +20,9 @@ module.exports = Self => {
}
});
Self.getItemTypeWorker = async(ctx, options) => {
Self.getItemTypeWorker = async(ctx, filter, options) => {
const myOptions = {};
const conn = Self.dataSource.connector;
let tx;
if (typeof options == 'object')
@ -32,11 +40,25 @@ module.exports = Self => {
JOIN worker w ON w.id = it.workerFk
JOIN account.user u ON u.id = w.id`;
const result = await Self.rawSql(query);
let stmt = new ParameterizedSQL(query);
if (filter.where) {
const value = filter.where.firstName;
const myFilter = {
where: {or: [
{'w.firstName': {like: `%${value}%`}},
{'w.lastName': {like: `%${value}%`}},
{'u.name': {like: `%${value}%`}},
{'u.nickname': {like: `%${value}%`}}
]}
};
stmt.merge(conn.makeSuffix(myFilter));
}
if (tx) await tx.commit();
return result;
return conn.executeStmt(stmt);
} catch (e) {
if (tx) await tx.rollback();
throw e;