From 0f8b81f62a3a6c62497c3f3913f6d3c259c2956b Mon Sep 17 00:00:00 2001 From: "Josepablo C." Date: Thu, 16 Nov 2023 23:40:08 -0600 Subject: [PATCH] feat: Adding filters to public-companies --- src/apps/public/public-companies/services.js | 42 ++++++++++++++++++-- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/src/apps/public/public-companies/services.js b/src/apps/public/public-companies/services.js index c90d9ec..40e4156 100644 --- a/src/apps/public/public-companies/services.js +++ b/src/apps/public/public-companies/services.js @@ -26,6 +26,23 @@ function join_field_list( obj_with_fields , list_of_fields ) return obj_with_fields; } +function getAndFilterList( query ){ + const filter_list = []; + const { company_type, company_name, truck_type, categories, company_state, company_city } = query; + + if( company_name ){ filter_list.push( { company_name } ); } + if( company_type ){ filter_list.push( { company_type } ); } + if( company_state ){ filter_list.push( { company_state } ); } + if( company_city ){ filter_list.push( { company_city } ); } + if( truck_type ){ filter_list.push( { truck_type } ); } + if( categories ){ filter_list.push( { categories } ); } + + if( filter_list.length == 0 ){ + return null; + } + return filter_list; +} + async function getListByType( type , req ){ const filter = { "company_type" : type , "is_hidden" : false }; const select = [ @@ -49,6 +66,13 @@ async function getListByType( type , req ){ }else{ query_elements = elements; } + + const andFilterList = getAndFilterList( req.query ); + + if( andFilterList ){ + filter.$and = andFilterList; + } + const queryVal = await generic.getList(page , query_elements, filter, select ); const data_list = queryVal.data; for(let i=0; i { - const retVal = await getListByType( "Shipper" , req ); - res.send( retVal ); + try{ + const retVal = await getListByType( "Shipper" , req ); + res.send( retVal ); + } catch ( err ){ + console.error( err ); + return res.status(500).send({ error : "Public-Companies(Carriers): Internal error" }); + } }; const getListCarriers = async(req, res) => { - const retVal = await getListByType( "Carrier" , req ); - res.send( retVal ); + try{ + const retVal = await getListByType( "Carrier" , req ); + res.send( retVal ); + } catch ( err ){ + console.error( err ); + return res.status(500).send({ error : "Public-Companies(Carriers): Internal error" }); + } }; const getUserLists = async(req, res) => {