diff --git a/README.md b/README.md index 5296bf2..0e88c67 100644 --- a/README.md +++ b/README.md @@ -356,7 +356,7 @@ This endpoint is part of /loads endpoint ### /loads - `GET /find` : Find a list of elements with any of the following fields: - - company + - companyId - carrier - vehicle - driver @@ -369,6 +369,8 @@ This endpoint is part of /loads endpoint - categories - product - shipment_code + - company_name[$regex] : Regex string to find company_name + - company_name[$options] : Regex options from MongoDB filter description - $sort[ field ] : -1/1 ; Sort result by field name - `GET /calendar` : Find a list of elements with any of the following fields: - load_status_updated[gte] : Date grater than. diff --git a/src/apps/private/loads/services.js b/src/apps/private/loads/services.js index a410822..be88417 100644 --- a/src/apps/private/loads/services.js +++ b/src/apps/private/loads/services.js @@ -4,6 +4,7 @@ const { getModel } = require( '../../../lib/Models' ); const { getPagination } = require( `${ROOT_PATH}/${LIB_PATH}/Misc.js` ); const { GenericHandler } = require( '../../../lib/Handlers//Generic.handler.js' ); const Model = getModel('loads'); +const CompanyModel = getModel('companies'); const ProposalsModel = getModel('proposals'); const populate_list = ['product', 'company', 'carrier', 'vehicle', 'categories']; @@ -12,7 +13,7 @@ const generic = new GenericHandler( Model, null, populate_list ); function getAndFilterList( query ){ const filter_list = []; const { - company, + companyId, carrier, vehicle, driver, @@ -27,7 +28,7 @@ function getAndFilterList( query ){ shipment_code } = query; - if( company ){ filter_list.push( { company } ); } + if( companyId ){ filter_list.push( { company : companyId } ); } if( carrier ){ filter_list.push( { carrier } ); } if( vehicle ){ filter_list.push( { vehicle } ); } if( driver ){ filter_list.push( { driver } ); } @@ -48,17 +49,30 @@ function getAndFilterList( query ){ } async function findLoads( query ){ - const { $sort } = query; + const { $sort, company_name } = query; const { page, elements } = getPagination( query ); - const andFilterList = getAndFilterList( query ); + const andFilterList = getAndFilterList( query ) || []; let filter; + + if( company_name ){ + /* Populate list of company ids with match on the company_name */ + const company_list = await CompanyModel.find( { company_name }, [ "id" ] ); + const or_company_list = [] + company_list.forEach( (item) =>{ + or_company_list.push({"company" : item.id}); + }) + andFilterList.push({ + $or : or_company_list + }); + } - if( andFilterList ){ + if( andFilterList.length > 0 ){ filter = { $and : andFilterList }; }else{ filter = null; } + const { total , limit, skip, data } = await generic.getList( page , elements, filter, null, $sort ); const load_list = data;