From 857ca28f43f1972e3b9295f22266b706e76e0b0a Mon Sep 17 00:00:00 2001 From: Josepablo C Date: Fri, 5 Apr 2024 21:21:13 -0600 Subject: [PATCH] fix(proposals): Allow finding proposals from any company --- src/apps/private/loads/services.js | 346 ++++++++++++------------- src/apps/private/proposals/services.js | 17 +- 2 files changed, 178 insertions(+), 185 deletions(-) diff --git a/src/apps/private/loads/services.js b/src/apps/private/loads/services.js index fb8d2fa..5c2f233 100644 --- a/src/apps/private/loads/services.js +++ b/src/apps/private/loads/services.js @@ -1,173 +1,173 @@ -"use strict"; -const { ROOT_PATH, LIB_PATH } = process.env; -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 ProposalsModel = getModel('proposals'); - -const populate_list = ['product', 'company', 'carrier', 'vehicle', 'categories']; -const generic = new GenericHandler( Model, null, populate_list ); - -function getAndFilterList( query ){ - const filter_list = []; - const { - company, - carrier, - vehicle, - driver, - status, - posted_by_name, - load_status, - published_date, - loaded_date, - transit_date, - categories, - product, - shipment_code - } = query; - - if( company ){ filter_list.push( { company } ); } - if( carrier ){ filter_list.push( { carrier } ); } - if( vehicle ){ filter_list.push( { vehicle } ); } - if( driver ){ filter_list.push( { driver } ); } - if( status ){ filter_list.push( { status } ); } - if( posted_by_name ) { filter_list.push({ posted_by_name }); } - if( load_status ) { filter_list.push({ load_status }); } - if( published_date ) { filter_list.push({ published_date }); } - if( loaded_date ) { filter_list.push({ loaded_date }); } - if( transit_date ) { filter_list.push({ transit_date }); } - if( categories ) { filter_list.push({ categories }); } - if( product ) { filter_list.push({ product }); } - if( shipment_code ) { filter_list.push({ shipment_code }); } - - if( filter_list.length == 0 ){ - return null; - } - return filter_list; -} - -async function findLoads( query ){ - const { page, elements } = getPagination( query ); - const andFilterList = getAndFilterList( query ); - let filter; - if( andFilterList ){ - filter = { $and : andFilterList }; - }else{ - filter = null; - } - const { total , limit, skip, data } = await generic.getList( page , elements, filter ); - const load_list = data; - for(let i=0; i { - try{ - const query = req.query || {}; - const retVal = await findLoads( query ); - res.send( retVal ); - }catch(error){ - console.error( error ); - return res.status( 500 ).send({ error }); - } -}; - -const getById = async(req, res) => { - try{ - const elementId = req.params.id; - res.send( await findElementById( elementId ) ); - }catch(error){ - console.error( error ); - return res.status( 500 ).send({ error }); - } -}; - -const patchLoad = async(req, res) => { - try{ - const elementId = req.params.id; - const permissions = req.context.permissions; - const data = req.body; - const load = await findElementById( elementId ); - if( !load ){ - throw "You can't modify this load"; - } - if( !data ){ - throw "load data not sent"; - } - await Model.findByIdAndUpdate( elementId , data ); - return res.send( await Model.findById( elementId ) ); - }catch(error){ - console.error( error ); - return res.status( 500 ).send({ error }); - } -}; - -const postLoad = async(req, res) => { - try{ - const companyId = req.context.companyId; - const userId = req.context.userId; - const user_name = req.context.user.first_name; - const permissions = req.context.permissions; - const data = req.body; - if( !data ){ - throw "Load data not sent"; - } - if(permissions !== "role_shipper" ){ - throw "You can't create loads"; - } - data.company = companyId; - data.posted_by = userId; - data.name = user_name; - const load = new Model( data ); - await load.save(); - return res.send( load ); - }catch(error){ - console.error( error ); - return res.status( 500 ).send({ error }); - } -}; - -const deleteLoad = async(req, res) => { - try{ - const companyId = req.context.companyId; - const elementId = req.params.id; - const permissions = req.context.permissions; - const load = await findElementById( elementId , companyId ); - if(!load){ - throw "You can't delete this load"; - } - if(permissions !== "role_shipper" ){ - throw "You can't delete loads"; - } - await Model.findByIdAndDelete( elementId ); - return res.send(load); - }catch(error){ - console.error( error ); - return res.status( 500 ).send({ error }); - } -}; - -module.exports = { findList, getById, patchLoad, postLoad, deleteLoad }; +"use strict"; +const { ROOT_PATH, LIB_PATH } = process.env; +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 ProposalsModel = getModel('proposals'); + +const populate_list = ['product', 'company', 'carrier', 'vehicle', 'categories']; +const generic = new GenericHandler( Model, null, populate_list ); + +function getAndFilterList( query ){ + const filter_list = []; + const { + company, + carrier, + vehicle, + driver, + status, + posted_by_name, + load_status, + published_date, + loaded_date, + transit_date, + categories, + product, + shipment_code + } = query; + + if( company ){ filter_list.push( { company } ); } + if( carrier ){ filter_list.push( { carrier } ); } + if( vehicle ){ filter_list.push( { vehicle } ); } + if( driver ){ filter_list.push( { driver } ); } + if( status ){ filter_list.push( { status } ); } + if( posted_by_name ) { filter_list.push({ posted_by_name }); } + if( load_status ) { filter_list.push({ load_status }); } + if( published_date ) { filter_list.push({ published_date }); } + if( loaded_date ) { filter_list.push({ loaded_date }); } + if( transit_date ) { filter_list.push({ transit_date }); } + if( categories ) { filter_list.push({ categories }); } + if( product ) { filter_list.push({ product }); } + if( shipment_code ) { filter_list.push({ shipment_code }); } + + if( filter_list.length == 0 ){ + return null; + } + return filter_list; +} + +async function findLoads( query ){ + const { page, elements } = getPagination( query ); + const andFilterList = getAndFilterList( query ); + let filter; + if( andFilterList ){ + filter = { $and : andFilterList }; + }else{ + filter = null; + } + const { total , limit, skip, data } = await generic.getList( page , elements, filter ); + const load_list = data; + for(let i=0; i { + try{ + const query = req.query || {}; + const retVal = await findLoads( query ); + res.send( retVal ); + }catch(error){ + console.error( error ); + return res.status( 500 ).send({ error }); + } +}; + +const getById = async(req, res) => { + try{ + const elementId = req.params.id; + res.send( await findElementById( elementId ) ); + }catch(error){ + console.error( error ); + return res.status( 500 ).send({ error }); + } +}; + +const patchLoad = async(req, res) => { + try{ + const elementId = req.params.id; + const permissions = req.context.permissions; + const data = req.body; + const load = await findElementById( elementId ); + if( !load ){ + throw "You can't modify this load"; + } + if( !data ){ + throw "load data not sent"; + } + await Model.findByIdAndUpdate( elementId , data ); + return res.send( await Model.findById( elementId ) ); + }catch(error){ + console.error( error ); + return res.status( 500 ).send({ error }); + } +}; + +const postLoad = async(req, res) => { + try{ + const companyId = req.context.companyId; + const userId = req.context.userId; + const user_name = req.context.user.first_name; + const permissions = req.context.permissions; + const data = req.body; + if( !data ){ + throw "Load data not sent"; + } + if(permissions !== "role_shipper" ){ + throw "You can't create loads"; + } + data.company = companyId; + data.posted_by = userId; + data.name = user_name; + const load = new Model( data ); + await load.save(); + return res.send( load ); + }catch(error){ + console.error( error ); + return res.status( 500 ).send({ error }); + } +}; + +const deleteLoad = async(req, res) => { + try{ + const companyId = req.context.companyId; + const elementId = req.params.id; + const permissions = req.context.permissions; + const load = await findElementById( elementId , companyId ); + if(!load){ + throw "You can't delete this load"; + } + if(permissions !== "role_shipper" ){ + throw "You can't delete loads"; + } + await Model.findByIdAndDelete( elementId ); + return res.send(load); + }catch(error){ + console.error( error ); + return res.status( 500 ).send({ error }); + } +}; + +module.exports = { findList, getById, patchLoad, postLoad, deleteLoad }; diff --git a/src/apps/private/proposals/services.js b/src/apps/private/proposals/services.js index 7c26360..26ef11f 100644 --- a/src/apps/private/proposals/services.js +++ b/src/apps/private/proposals/services.js @@ -11,8 +11,9 @@ const generic = new GenericHandler( Model, null, populate_list ); function getAndFilterList( query ){ const filter_list = []; - const { categories, branch_name, phone, city, state, truck_type } = query; + const { load, categories, branch_name, phone, city, state, truck_type } = query; + if( load ) { filter_list.push({ load }); } if( categories ) { filter_list.push({ categories }); } if( branch_name ) { filter_list.push({ branch_name }); } if( phone ) { filter_list.push({ phone }); } @@ -26,21 +27,14 @@ function getAndFilterList( query ){ return filter_list; } -async function findElements( companyId , query ){ +async function findElements( query ){ const { page, elements } = getPagination( query ); const andFilterList = getAndFilterList( query ); let filter; if( andFilterList ){ - andFilterList.push({ $or :[ - { shipper : companyId }, - { carrier : companyId } - ]}); filter = { $and : andFilterList }; }else{ - filter = { $or :[ - { shipper : companyId }, - { carrier : companyId } - ]}; + filter = null; } const { total , limit, skip, data } = await generic.getList( page , elements, filter ); return { @@ -68,8 +62,7 @@ async function findElementById( elementId , companyId ){ const findList = async(req, res) => { try{ const query = req.query || {}; - const companyId = req.context.companyId; - const retVal = await findElements( companyId , query ); + const retVal = await findElements( query ); res.send( retVal ); }catch(error){ console.error( error );