feat: Split v1 and v2 apis
This commit is contained in:
294
v1/src/apps/private/loads/services.js
Normal file
294
v1/src/apps/private/loads/services.js
Normal file
@@ -0,0 +1,294 @@
|
||||
"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 CompanyModel = getModel('companies');
|
||||
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 {
|
||||
companyId,
|
||||
carrier,
|
||||
vehicle,
|
||||
driver,
|
||||
status,
|
||||
posted_by,
|
||||
posted_by_name,
|
||||
load_status,
|
||||
published_date,
|
||||
loaded_date,
|
||||
transit_date,
|
||||
categories,
|
||||
product,
|
||||
shipment_code
|
||||
} = query;
|
||||
|
||||
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 } ); }
|
||||
if( status ){ filter_list.push( { status } ); }
|
||||
if( posted_by ) { filter_list.push({ posted_by }); }
|
||||
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 { $sort, company_name } = query;
|
||||
const { page, elements } = getPagination( 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.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;
|
||||
|
||||
for(let i=0; i<load_list.length; i++){
|
||||
const load_id = load_list[ i ].id;
|
||||
load_list[i] = load_list[i].toObject();
|
||||
const no_of_proposals = await ProposalsModel.count({ load : load_id });
|
||||
load_list[i].no_of_proposals = no_of_proposals;
|
||||
}
|
||||
return {
|
||||
total,
|
||||
limit,
|
||||
skip,
|
||||
data : load_list
|
||||
};
|
||||
}
|
||||
|
||||
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",
|
||||
"shipment_code",
|
||||
"est_loading_date",
|
||||
"est_unloading_date"
|
||||
];
|
||||
const { date, load_status , $sort } = query;
|
||||
const { global } = query;
|
||||
|
||||
if( ! date ){
|
||||
throw "Date field is required";
|
||||
}
|
||||
|
||||
const { page, elements } = getPagination( query );
|
||||
|
||||
const orFilterList = [
|
||||
{"company" : companyId},
|
||||
{"carrier" : companyId}
|
||||
]
|
||||
|
||||
const andFilterList = [
|
||||
{
|
||||
"est_loading_date" : {
|
||||
$gte : new Date( date["gte"] ),
|
||||
$lte : new Date( date["lte"] )
|
||||
}
|
||||
},
|
||||
{
|
||||
$or : orFilterList
|
||||
}
|
||||
]
|
||||
|
||||
if( !(global) || (global == 0) ){
|
||||
andFilterList.push( {
|
||||
$or : [
|
||||
{"bidder" : userId},
|
||||
{"posted_by" : userId},
|
||||
]
|
||||
} );
|
||||
}
|
||||
|
||||
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 ){
|
||||
retVal = retVal.toObject();
|
||||
const no_of_proposals = await ProposalsModel.count({ load : elementId });
|
||||
retVal.no_of_proposals = no_of_proposals;
|
||||
}else{
|
||||
retVal = {};
|
||||
}
|
||||
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 || {};
|
||||
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();
|
||||
|
||||
const id = "" + load._id;
|
||||
const shipment_code = "ETA-" + id.substring( 0 , 6 );
|
||||
await Model.findByIdAndUpdate( id , {
|
||||
shipment_code
|
||||
});
|
||||
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 = { findCalendarList, findList, getById, patchLoad, postLoad, deleteLoad };
|
||||
Reference in New Issue
Block a user