feat: Adding load sort field and load/calendar endpoint

This commit is contained in:
Josepablo C
2024-07-04 23:41:17 -06:00
parent 3fa2263e28
commit a637aa2a2c
5 changed files with 232 additions and 37 deletions

View File

@@ -48,16 +48,21 @@ function getAndFilterList( query ){
}
async function findLoads( query ){
const { $sort } = 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 { total , limit, skip, data } = await generic.getList( page , elements, filter, null, $sort );
const load_list = data;
for(let i=0; i<load_list.length; i++){
const load_id = load_list[ i ].id;
load_list[i] = load_list[i].toObject();
@@ -72,6 +77,73 @@ async function findLoads( query ){
};
}
async function findCalendarLoads( userId, companyId, query ){
const select = [
"company",
"carrier",
"posted_by",
"bidder",
"status",
"load_status",
"published_date",
"load_status_updated",
"loaded_date",
"transit_date",
"delivered_date",
"createdAt"
];
const { date, load_status , $sort } = query;
if( ! date ){
throw "Date field is required";
}
const { page, elements } = getPagination( query );
const andFilterList = [
{
"load_status_updated" : {
$gte : new Date( date["gte"] ),
$lte : new Date( date["lte"] )
}
},
{
$or : [
{"bidder" : userId},
{"posted_by" : userId},
{"company" : companyId},
{"carrier" : companyId}
]
}
]
if( load_status ){
andFilterList.push( { load_status } )
}
const filter = {
$and : andFilterList,
};
const {
total,
limit,
skip,
query : model_query,
} = await generic.getListQuery( page , elements, filter , select );
if( $sort ){
model_query.sort( $sort );
}
const data = await model_query.exec();
return {
total,
limit,
skip,
data
};
}
async function findElementById( elementId ){
let retVal = await Model.findById( elementId ).populate( populate_list );
if( retVal ){
@@ -84,6 +156,19 @@ async function findElementById( elementId ){
return retVal;
}
const findCalendarList = async(req, res) => {
try{
const query = req.query || {};
const companyId = req.context.companyId;
const userId = req.context.userId;
const retVal = await findCalendarLoads( userId, companyId, query );
res.send( retVal );
}catch(error){
console.error( error );
return res.status( 500 ).send({ error });
}
}
const findList = async(req, res) => {
try{
const query = req.query || {};
@@ -176,4 +261,4 @@ const deleteLoad = async(req, res) => {
}
};
module.exports = { findList, getById, patchLoad, postLoad, deleteLoad };
module.exports = { findCalendarList, findList, getById, patchLoad, postLoad, deleteLoad };