feat(v1:ehicles): Adding global/find endpoint
This commit is contained in:
18
v1/README.md
18
v1/README.md
@@ -432,7 +432,23 @@ Work In Progress
|
|||||||
|
|
||||||
__This endpoint is only valid for carriers.__
|
__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,
|
- categories,
|
||||||
- active_load,
|
- active_load,
|
||||||
- load_shipper,
|
- load_shipper,
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ const router = require('express').Router();
|
|||||||
const services= require('./services.js');
|
const services= require('./services.js');
|
||||||
|
|
||||||
router.get('/find', services.findList);
|
router.get('/find', services.findList);
|
||||||
|
router.get('/global/find', services.globalFindList);
|
||||||
router.post('/new', services.postVehicle);
|
router.post('/new', services.postVehicle);
|
||||||
|
|
||||||
router.patch('/:id', services.patchVehicle);
|
router.patch('/:id', services.patchVehicle);
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ const generic = new GenericHandler( Model, null, populate_list );
|
|||||||
function getAndFilterList( query ){
|
function getAndFilterList( query ){
|
||||||
const filter_list = [];
|
const filter_list = [];
|
||||||
const {
|
const {
|
||||||
|
company,
|
||||||
categories,
|
categories,
|
||||||
active_load,
|
active_load,
|
||||||
load_shipper,
|
load_shipper,
|
||||||
@@ -27,6 +28,7 @@ function getAndFilterList( query ){
|
|||||||
destino
|
destino
|
||||||
} = query;
|
} = query;
|
||||||
|
|
||||||
|
if( company ) { filter_list.push({ company }); }
|
||||||
if( categories ) { filter_list.push({ categories }); }
|
if( categories ) { filter_list.push({ categories }); }
|
||||||
if( active_load ) { filter_list.push({ active_load }); }
|
if( active_load ) { filter_list.push({ active_load }); }
|
||||||
if( load_shipper ) { filter_list.push({ load_shipper }); }
|
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 ){
|
async function findElementById( elementId , companyId ){
|
||||||
let retVal = await Model.findById( elementId ).populate( populate_list ) || {};
|
let retVal = await Model.findById( elementId ).populate( populate_list ) || {};
|
||||||
return retVal;
|
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) => {
|
const getById = async(req, res) => {
|
||||||
try{
|
try{
|
||||||
const companyId = req.context.companyId;
|
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 };
|
||||||
|
|||||||
Reference in New Issue
Block a user