fix(loads): company_name regex is now enabled

This commit is contained in:
Josepablo C
2024-07-05 00:27:26 -06:00
parent 2ff3ef91c3
commit 85c37a225d
2 changed files with 22 additions and 6 deletions

View File

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