diff --git a/v1/README.md b/v1/README.md index d7b9d11..5ef2244 100644 --- a/v1/README.md +++ b/v1/README.md @@ -432,7 +432,23 @@ Work In Progress __This endpoint is only valid for carriers.__ - - `GET /find` : Find a list of elements with any of the following fields: + - `GET /find` : Find a list of elements (from the company it self only) with any of the following fields: + - categories, + - active_load, + - load_shipper, + - driver, + - vehicle_code, + - vehicle_name, + - vehicle_number, + - circulation_serial_number, + - truck_type, + - tyre_type, + - city, + - state, + - status, + - destino + - `GET /global/find` : Find a list of elements (from all the system) with any of the following fields: + - company, (company id) - categories, - active_load, - load_shipper, diff --git a/v1/src/apps/private/vehicles/routes.js b/v1/src/apps/private/vehicles/routes.js index ec320c0..90c94b5 100644 --- a/v1/src/apps/private/vehicles/routes.js +++ b/v1/src/apps/private/vehicles/routes.js @@ -3,6 +3,7 @@ const router = require('express').Router(); const services= require('./services.js'); router.get('/find', services.findList); +router.get('/global/find', services.globalFindList); router.post('/new', services.postVehicle); router.patch('/:id', services.patchVehicle); diff --git a/v1/src/apps/private/vehicles/services.js b/v1/src/apps/private/vehicles/services.js index 2e7dda0..ff12d54 100644 --- a/v1/src/apps/private/vehicles/services.js +++ b/v1/src/apps/private/vehicles/services.js @@ -11,6 +11,7 @@ const generic = new GenericHandler( Model, null, populate_list ); function getAndFilterList( query ){ const filter_list = []; const { + company, categories, active_load, load_shipper, @@ -27,6 +28,7 @@ function getAndFilterList( query ){ destino } = query; + if( company ) { filter_list.push({ company }); } if( categories ) { filter_list.push({ categories }); } if( active_load ) { filter_list.push({ active_load }); } if( load_shipper ) { filter_list.push({ load_shipper }); } @@ -67,6 +69,26 @@ async function findElements( companyId , query ){ }; } +async function findGlobalElements( query ){ + const { page, elements } = getPagination( query ); + const andFilterList = getAndFilterList( query ); + let filter = null; + + if( andFilterList ){ + filter = { $and : andFilterList }; + }else{ + filter = null; + } + + const { total , limit, skip, data } = await generic.getList( page , elements, filter ); + return { + total, + limit, + skip, + data:data + }; +} + async function findElementById( elementId , companyId ){ let retVal = await Model.findById( elementId ).populate( populate_list ) || {}; return retVal; @@ -84,6 +106,17 @@ const findList = async(req, res) => { } }; +const globalFindList = async(req, res) => { + try{ + const query = req.query || {}; + const retVal = await findGlobalElements( query ); + res.send( retVal ); + }catch(error){ + console.error( error ); + return res.status( 500 ).send({ error }); + } +}; + const getById = async(req, res) => { try{ const companyId = req.context.companyId; @@ -165,4 +198,4 @@ const deleteVehicle = async(req, res) => { } }; -module.exports = { findList, getById, patchVehicle, postVehicle, deleteVehicle }; +module.exports = { findList, globalFindList, getById, patchVehicle, postVehicle, deleteVehicle };