fix(proposals): Allow finding proposals from any company

This commit is contained in:
Josepablo C
2024-04-05 21:21:13 -06:00
parent eb08f4abb8
commit 857ca28f43
2 changed files with 178 additions and 185 deletions

View File

@@ -1,173 +1,173 @@
"use strict"; "use strict";
const { ROOT_PATH, LIB_PATH } = process.env; const { ROOT_PATH, LIB_PATH } = process.env;
const { getModel } = require( '../../../lib/Models' ); const { getModel } = require( '../../../lib/Models' );
const { getPagination } = require( `${ROOT_PATH}/${LIB_PATH}/Misc.js` ); const { getPagination } = require( `${ROOT_PATH}/${LIB_PATH}/Misc.js` );
const { GenericHandler } = require( '../../../lib/Handlers//Generic.handler.js' ); const { GenericHandler } = require( '../../../lib/Handlers//Generic.handler.js' );
const Model = getModel('loads'); const Model = getModel('loads');
const ProposalsModel = getModel('proposals'); const ProposalsModel = getModel('proposals');
const populate_list = ['product', 'company', 'carrier', 'vehicle', 'categories']; const populate_list = ['product', 'company', 'carrier', 'vehicle', 'categories'];
const generic = new GenericHandler( Model, null, populate_list ); const generic = new GenericHandler( Model, null, populate_list );
function getAndFilterList( query ){ function getAndFilterList( query ){
const filter_list = []; const filter_list = [];
const { const {
company, company,
carrier, carrier,
vehicle, vehicle,
driver, driver,
status, status,
posted_by_name, posted_by_name,
load_status, load_status,
published_date, published_date,
loaded_date, loaded_date,
transit_date, transit_date,
categories, categories,
product, product,
shipment_code shipment_code
} = query; } = query;
if( company ){ filter_list.push( { company } ); } if( company ){ filter_list.push( { company } ); }
if( carrier ){ filter_list.push( { carrier } ); } if( carrier ){ filter_list.push( { carrier } ); }
if( vehicle ){ filter_list.push( { vehicle } ); } if( vehicle ){ filter_list.push( { vehicle } ); }
if( driver ){ filter_list.push( { driver } ); } if( driver ){ filter_list.push( { driver } ); }
if( status ){ filter_list.push( { status } ); } if( status ){ filter_list.push( { status } ); }
if( posted_by_name ) { filter_list.push({ posted_by_name }); } if( posted_by_name ) { filter_list.push({ posted_by_name }); }
if( load_status ) { filter_list.push({ load_status }); } if( load_status ) { filter_list.push({ load_status }); }
if( published_date ) { filter_list.push({ published_date }); } if( published_date ) { filter_list.push({ published_date }); }
if( loaded_date ) { filter_list.push({ loaded_date }); } if( loaded_date ) { filter_list.push({ loaded_date }); }
if( transit_date ) { filter_list.push({ transit_date }); } if( transit_date ) { filter_list.push({ transit_date }); }
if( categories ) { filter_list.push({ categories }); } if( categories ) { filter_list.push({ categories }); }
if( product ) { filter_list.push({ product }); } if( product ) { filter_list.push({ product }); }
if( shipment_code ) { filter_list.push({ shipment_code }); } if( shipment_code ) { filter_list.push({ shipment_code }); }
if( filter_list.length == 0 ){ if( filter_list.length == 0 ){
return null; return null;
} }
return filter_list; return filter_list;
} }
async function findLoads( query ){ async function findLoads( query ){
const { page, elements } = getPagination( query ); const { page, elements } = getPagination( query );
const andFilterList = getAndFilterList( query ); const andFilterList = getAndFilterList( query );
let filter; let filter;
if( andFilterList ){ if( andFilterList ){
filter = { $and : andFilterList }; filter = { $and : andFilterList };
}else{ }else{
filter = null; filter = null;
} }
const { total , limit, skip, data } = await generic.getList( page , elements, filter ); const { total , limit, skip, data } = await generic.getList( page , elements, filter );
const load_list = data; const load_list = data;
for(let i=0; i<load_list.length; i++){ for(let i=0; i<load_list.length; i++){
const load_id = load_list[ i ].id; const load_id = load_list[ i ].id;
load_list[i] = load_list[i].toObject(); load_list[i] = load_list[i].toObject();
const no_of_proposals = await ProposalsModel.count({ load : load_id }); const no_of_proposals = await ProposalsModel.count({ load : load_id });
load_list[i].no_of_proposals = no_of_proposals; load_list[i].no_of_proposals = no_of_proposals;
} }
return { return {
total, total,
limit, limit,
skip, skip,
data : load_list data : load_list
}; };
} }
async function findElementById( elementId ){ async function findElementById( elementId ){
let retVal = await Model.findById( elementId ).populate( populate_list ); let retVal = await Model.findById( elementId ).populate( populate_list );
if( retVal ){ if( retVal ){
retVal = retVal.toObject(); retVal = retVal.toObject();
const no_of_proposals = await ProposalsModel.count({ load : elementId }); const no_of_proposals = await ProposalsModel.count({ load : elementId });
retVal.no_of_proposals = no_of_proposals; retVal.no_of_proposals = no_of_proposals;
}else{ }else{
retVal = {}; retVal = {};
} }
return retVal; return retVal;
} }
const findList = async(req, res) => { const findList = async(req, res) => {
try{ try{
const query = req.query || {}; const query = req.query || {};
const retVal = await findLoads( query ); const retVal = await findLoads( query );
res.send( retVal ); res.send( retVal );
}catch(error){ }catch(error){
console.error( error ); console.error( error );
return res.status( 500 ).send({ error }); return res.status( 500 ).send({ error });
} }
}; };
const getById = async(req, res) => { const getById = async(req, res) => {
try{ try{
const elementId = req.params.id; const elementId = req.params.id;
res.send( await findElementById( elementId ) ); res.send( await findElementById( elementId ) );
}catch(error){ }catch(error){
console.error( error ); console.error( error );
return res.status( 500 ).send({ error }); return res.status( 500 ).send({ error });
} }
}; };
const patchLoad = async(req, res) => { const patchLoad = async(req, res) => {
try{ try{
const elementId = req.params.id; const elementId = req.params.id;
const permissions = req.context.permissions; const permissions = req.context.permissions;
const data = req.body; const data = req.body;
const load = await findElementById( elementId ); const load = await findElementById( elementId );
if( !load ){ if( !load ){
throw "You can't modify this load"; throw "You can't modify this load";
} }
if( !data ){ if( !data ){
throw "load data not sent"; throw "load data not sent";
} }
await Model.findByIdAndUpdate( elementId , data ); await Model.findByIdAndUpdate( elementId , data );
return res.send( await Model.findById( elementId ) ); return res.send( await Model.findById( elementId ) );
}catch(error){ }catch(error){
console.error( error ); console.error( error );
return res.status( 500 ).send({ error }); return res.status( 500 ).send({ error });
} }
}; };
const postLoad = async(req, res) => { const postLoad = async(req, res) => {
try{ try{
const companyId = req.context.companyId; const companyId = req.context.companyId;
const userId = req.context.userId; const userId = req.context.userId;
const user_name = req.context.user.first_name; const user_name = req.context.user.first_name;
const permissions = req.context.permissions; const permissions = req.context.permissions;
const data = req.body; const data = req.body;
if( !data ){ if( !data ){
throw "Load data not sent"; throw "Load data not sent";
} }
if(permissions !== "role_shipper" ){ if(permissions !== "role_shipper" ){
throw "You can't create loads"; throw "You can't create loads";
} }
data.company = companyId; data.company = companyId;
data.posted_by = userId; data.posted_by = userId;
data.name = user_name; data.name = user_name;
const load = new Model( data ); const load = new Model( data );
await load.save(); await load.save();
return res.send( load ); return res.send( load );
}catch(error){ }catch(error){
console.error( error ); console.error( error );
return res.status( 500 ).send({ error }); return res.status( 500 ).send({ error });
} }
}; };
const deleteLoad = async(req, res) => { const deleteLoad = async(req, res) => {
try{ try{
const companyId = req.context.companyId; const companyId = req.context.companyId;
const elementId = req.params.id; const elementId = req.params.id;
const permissions = req.context.permissions; const permissions = req.context.permissions;
const load = await findElementById( elementId , companyId ); const load = await findElementById( elementId , companyId );
if(!load){ if(!load){
throw "You can't delete this load"; throw "You can't delete this load";
} }
if(permissions !== "role_shipper" ){ if(permissions !== "role_shipper" ){
throw "You can't delete loads"; throw "You can't delete loads";
} }
await Model.findByIdAndDelete( elementId ); await Model.findByIdAndDelete( elementId );
return res.send(load); return res.send(load);
}catch(error){ }catch(error){
console.error( error ); console.error( error );
return res.status( 500 ).send({ error }); return res.status( 500 ).send({ error });
} }
}; };
module.exports = { findList, getById, patchLoad, postLoad, deleteLoad }; module.exports = { findList, getById, patchLoad, postLoad, deleteLoad };

View File

@@ -11,8 +11,9 @@ const generic = new GenericHandler( Model, null, populate_list );
function getAndFilterList( query ){ function getAndFilterList( query ){
const filter_list = []; 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( categories ) { filter_list.push({ categories }); }
if( branch_name ) { filter_list.push({ branch_name }); } if( branch_name ) { filter_list.push({ branch_name }); }
if( phone ) { filter_list.push({ phone }); } if( phone ) { filter_list.push({ phone }); }
@@ -26,21 +27,14 @@ function getAndFilterList( query ){
return filter_list; return filter_list;
} }
async function findElements( companyId , query ){ async function findElements( query ){
const { page, elements } = getPagination( query ); const { page, elements } = getPagination( query );
const andFilterList = getAndFilterList( query ); const andFilterList = getAndFilterList( query );
let filter; let filter;
if( andFilterList ){ if( andFilterList ){
andFilterList.push({ $or :[
{ shipper : companyId },
{ carrier : companyId }
]});
filter = { $and : andFilterList }; filter = { $and : andFilterList };
}else{ }else{
filter = { $or :[ filter = null;
{ shipper : companyId },
{ carrier : companyId }
]};
} }
const { total , limit, skip, data } = await generic.getList( page , elements, filter ); const { total , limit, skip, data } = await generic.getList( page , elements, filter );
return { return {
@@ -68,8 +62,7 @@ async function findElementById( elementId , companyId ){
const findList = async(req, res) => { const findList = async(req, res) => {
try{ try{
const query = req.query || {}; const query = req.query || {};
const companyId = req.context.companyId; const retVal = await findElements( query );
const retVal = await findElements( companyId , query );
res.send( retVal ); res.send( retVal );
}catch(error){ }catch(error){
console.error( error ); console.error( error );