feat(v1:ehicles): Adding global/find endpoint

This commit is contained in:
Josepablo C
2024-08-13 21:16:13 -06:00
parent 9b24704f76
commit db6af2e7da
3 changed files with 52 additions and 2 deletions

View File

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

View File

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