feat: refs #7119 add search and filter capabilities to Vehicle model and update related SQL fixtures
gitea/salix/pipeline/pr-dev This commit looks good Details

This commit is contained in:
Jorge Penadés 2024-12-26 16:36:50 +01:00
parent fffd095ab3
commit f8c1e2aacf
3 changed files with 87 additions and 8 deletions

View File

@ -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);
('2000-12-20', '2001-01-01', 3, 'llaves puestas', 2, 103, NULL);

View File

@ -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;
});
};
};

View File

@ -60,9 +60,10 @@
"foreignKey": "deliveryPointFk"
},
"event": {
"type": "belongsTo",
"type": "hasMany",
"model": "VehicleEvent",
"foreignKey": "id"
"foreignKey": "vehicleFk",
"property": "id"
}
},
"scope": {