feat: refs #7119 enhance vehicle filter method with additional parameters and improve SQL query structure
gitea/salix/pipeline/pr-dev There was a failure building this commit Details

This commit is contained in:
Jorge Penadés 2024-12-30 17:31:56 +01:00
parent e9b0b1b7a3
commit 68e42206c8
2 changed files with 79 additions and 57 deletions

View File

@ -1,5 +1,8 @@
const {ParameterizedSQL} = require('loopback-connector');
const {buildFilter, mergeFilters} = require('vn-loopback/util/filter');
module.exports = Self => { module.exports = Self => {
Self.remoteMethod('filter', { Self.remoteMethodCtx('filter', {
description: 'Find all instances of the model matched by filter from the data source.', description: 'Find all instances of the model matched by filter from the data source.',
accessType: 'READ', accessType: 'READ',
accepts: [{ accepts: [{
@ -42,6 +45,9 @@ module.exports = Self => {
}, { }, {
arg: 'isKmTruckRate', arg: 'isKmTruckRate',
type: 'boolean' type: 'boolean'
}, {
arg: 'vehicleStateFk',
type: 'number'
}], }],
returns: { returns: {
type: ['object'], type: ['object'],
@ -53,66 +59,79 @@ module.exports = Self => {
} }
}); });
Self.filter = async(filter, search, id, description, companyFk, tradeMark, numberPlate, warehouseFk, chassis, leasing, countryCodeFk, isKmTruckRate, options) => { Self.filter = async(ctx, filter, options) => {
const conn = Self.dataSource.connector;
const myOptions = {}; const myOptions = {};
const myWhere = {};
const {limit, order, skip, where} = filter;
if (typeof options == 'object') Object.assign(myOptions, options); if (typeof options == 'object') Object.assign(myOptions, options);
if (search) myWhere.or = [{id: search}, {numberPlate: {like: `%${search}%`}}]; const where = buildFilter(ctx.args, (param, value) => {
if (id) myWhere.id = id; switch (param) {
if (description) myWhere.description = {like: `%${description}%`}; case 'search':
if (companyFk) myWhere.companyFk = companyFk; return {or: [{'v.id': value}, {numberPlate: {like: `%${value}%`}}]};
if (tradeMark) myWhere.tradeMark = {like: `%${tradeMark}%`}; case 'id':
if (numberPlate) myWhere.numberPlate = {like: `%${numberPlate}%`}; return {id: value};
if (warehouseFk) myWhere.warehouseFk = warehouseFk; case 'description':
if (chassis) myWhere.chassis = {like: `%${chassis}%`}; return {description: {like: `%${value}%`}};
if (leasing) myWhere.leasing = {like: `%${leasing}%`}; case 'companyFk':
if (countryCodeFk) myWhere.countryCodeFk = countryCodeFk; return {companyFk: value};
if (isKmTruckRate) myWhere.isKmTruckRate = isKmTruckRate; case 'tradeMark':
return {tradeMark: {like: `%${value}%`}};
Object.assign(where || {}, myWhere); case 'numberPlate':
return {numberPlate: {like: `%${value}%`}};
const myFilter = { case 'warehouseFk':
fields: ['id', 'numberPlate', 'tradeMark', 'model', 'm3', 'description', 'isActive', 'warehouseFk', 'companyFk', 'countryCodeFk', 'chassis', 'leasing', 'isKmTruckRate'], return {warehouseFk: value};
include: [ case 'chassis':
{ return {chassis: {like: `%${value}%`}};
relation: 'warehouse', case 'leasing':
scope: { return {leasing: {like: `%${value}%`}};
fields: ['id', 'name'] case 'countryCodeFk':
return {countryCodeFk: value};
case 'isKmTruckRate':
return {isKmTruckRate: value};
case 'vehicleStateFk':
return {vehicleStateFk: value};
} }
},
{
relation: 'company',
scope: {
fields: ['id', 'code']
}
},
{
relation: 'event',
scope: {
fields: ['vehicleFk', 'vehicleStateFk'],
include: {
relation: 'state',
scope: {
fields: ['id', 'state']
}
},
order: ['started DESC'],
limit: 1
}
},
],
where: myWhere,
order,
limit,
skip,
};
return Self.find(myFilter, myOptions).map(v => {
v.__data.event = v.event()[0];
return v;
}); });
const myFilter = mergeFilters(filter, {where});
const stmt = new ParameterizedSQL(`
SELECT v.id,
v.numberPlate,
v.tradeMark,
v.model,
v.m3,
v.description,
v.isActive,
v.countryCodeFk,
v.chassis,
v.leasing,
v.isKmTruckRate,
w.name as warehouse,
c.code as company,
sub.state
FROM vehicle v
LEFT JOIN warehouse w ON w.id = v.warehouseFk
LEFT JOIN company c ON c.id = v.companyFk
LEFT JOIN (
SELECT e.vehicleFk,
e.vehicleStateFk,
s.state,
ROW_NUMBER() OVER (PARTITION BY e.vehicleFk ORDER BY e.started DESC) as rn
FROM vehicleEvent e
LEFT JOIN vehicleState s ON e.vehicleStateFk = s.id
) sub ON sub.vehicleFk = v.id AND sub.rn = 1
`
);
const sqlWhere = conn.makeWhere(myFilter.where);
stmt.merge(sqlWhere);
stmt.merge(conn.makePagination(myFilter));
const sql = ParameterizedSQL.join([stmt], ';');
const result = await conn.executeStmt(sql, myOptions);
return result;
}; };
}; };

View File

@ -50,6 +50,9 @@
}, },
"vin": { "vin": {
"type": "string" "type": "string"
},
"ppeFk": {
"type": "number"
} }
}, },
"relations": { "relations": {