From f8c1e2aacfd2071716d6030ce1224b3bad35b3de Mon Sep 17 00:00:00 2001 From: jorgep Date: Thu, 26 Dec 2024 16:36:50 +0100 Subject: [PATCH] feat: refs #7119 add search and filter capabilities to Vehicle model and update related SQL fixtures --- db/dump/fixtures.before.sql | 2 +- modules/route/back/methods/vehicle/filter.js | 88 ++++++++++++++++++-- modules/route/back/models/vehicle.json | 5 +- 3 files changed, 87 insertions(+), 8 deletions(-) diff --git a/db/dump/fixtures.before.sql b/db/dump/fixtures.before.sql index 5516f1329..0189b1137 100644 --- a/db/dump/fixtures.before.sql +++ b/db/dump/fixtures.before.sql @@ -4064,4 +4064,4 @@ INSERT INTO vn.vehicleEvent (started, finished, vehicleStateFk, description, veh VALUES ('2000-12-01', '2000-12-02', 4, 'cambio de aceite', 5, 103, NULL), ('2000-12-15', '2000-12-18', 2, 'viaje fin de curso', 5, 103, NULL), - ('2000-12-20', '2001-01-01', 3, 'llaves puestas', 203, 103, NULL); \ No newline at end of file + ('2000-12-20', '2001-01-01', 3, 'llaves puestas', 2, 103, NULL); \ No newline at end of file diff --git a/modules/route/back/methods/vehicle/filter.js b/modules/route/back/methods/vehicle/filter.js index 608139a4c..b6ba306ac 100644 --- a/modules/route/back/methods/vehicle/filter.js +++ b/modules/route/back/methods/vehicle/filter.js @@ -3,8 +3,48 @@ module.exports = Self => { description: 'Find all instances of the model matched by filter from the data source.', accessType: 'READ', accepts: [{ + arg: 'filter', + type: 'object', + description: 'Filter defining where, order, skip and limit - must be a JSON-encoded string', + http: {source: 'query'} + }, { + arg: 'search', + type: 'string', + description: 'Searchs the vehicle by id or numberPlate', + http: {source: 'query'} + }, { arg: 'id', type: 'number' + }, { + arg: 'description', + type: 'string' + }, { + arg: 'companyFk', + type: 'number' + }, { + arg: 'tradeMark', + type: 'string' + }, { + arg: 'numberPlate', + type: 'string' + }, { + arg: 'warehouseFk', + type: 'number' + }, { + arg: 'chassis', + type: 'string' + }, { + arg: 'leasing', + type: 'string' + }, { + arg: 'countryCodeFk', + type: 'string' + }, { + arg: 'isKmTruckRate', + type: 'boolean' + }, { + arg: 'vehicleStateFk', + type: 'number' }], returns: { type: ['object'], @@ -16,8 +56,37 @@ module.exports = Self => { } }); - Self.filter = async id => { - const filter = { + Self.filter = async(filter, search, id, description, companyFk, tradeMark, numberPlate, warehouseFk, chassis, leasing, countryCodeFk, isKmTruckRate, vehicleStateFk, options) => { + const models = Self.app.models; + const myOptions = {}; + const myWhere = {}; + const ids = []; + const {limit, order, skip, where} = filter; + + if (typeof options == 'object') Object.assign(myOptions, options); + + if (search) myWhere.or = [{id: search}, {numberPlate: {like: `%${numberPlate}%`}}]; + if (id) ids.push(id); + if (description) myWhere.description = {like: `%${description}%`}; + if (companyFk) myWhere.companyFk = companyFk; + if (tradeMark) myWhere.tradeMark = {like: `%${tradeMark}%`}; + if (numberPlate) myWhere.numberPlate = {like: `%${numberPlate}%`}; + if (warehouseFk) myWhere.warehouseFk = warehouseFk; + if (chassis) myWhere.chassis = {like: `%${chassis}%`}; + if (leasing) myWhere.leasing = {like: `%${leasing}%`}; + if (countryCodeFk) myWhere.countryCodeFk = countryCodeFk; + if (isKmTruckRate) myWhere.isKmTruckRate = isKmTruckRate; + if (vehicleStateFk) { + ids.push(...await models.VehicleEvent.find({ + fields: ['vehicleFk', 'vehicleStateFk'], + where: {vehicleStateFk}}).map(v => v.vehicleFk)); + } + + const idsLeng = ids.length; + if (idsLeng) myWhere.id = idsLeng == 1 ? ids[0] : {inq: ids}; + Object.assign(where || {}, myWhere); + + const myFilter = { fields: ['id', 'numberPlate', 'tradeMark', 'model', 'm3', 'description', 'isActive', 'warehouseFk', 'companyFk', 'countryCodeFk', 'chassis', 'leasing', 'isKmTruckRate'], include: [ { @@ -41,12 +110,21 @@ module.exports = Self => { scope: { fields: ['id', 'state'] } - } + }, + order: ['started DESC'], + limit: 1 } }, - ] + ], + where: myWhere, + order, + limit, + skip, }; - return Self.find(filter); + return Self.find(myFilter, myOptions).map(v => { + v.__data.event = v.event()[0]; + return v; + }); }; }; diff --git a/modules/route/back/models/vehicle.json b/modules/route/back/models/vehicle.json index a98c7cab7..8419ee684 100644 --- a/modules/route/back/models/vehicle.json +++ b/modules/route/back/models/vehicle.json @@ -60,9 +60,10 @@ "foreignKey": "deliveryPointFk" }, "event": { - "type": "belongsTo", + "type": "hasMany", "model": "VehicleEvent", - "foreignKey": "id" + "foreignKey": "vehicleFk", + "property": "id" } }, "scope": {