feat: Adding findLoads service per account type (driver, shipper, carrier)
This commit is contained in:
216
v1/src/apps/private/loads/carrier.services.js
Normal file
216
v1/src/apps/private/loads/carrier.services.js
Normal file
@@ -0,0 +1,216 @@
|
|||||||
|
"use strict";
|
||||||
|
const { getModel } = require( '../../../lib/Models' );
|
||||||
|
const { getPagination, normalizeDate } = require( '../../../lib/Misc.js' );
|
||||||
|
const { GenericHandler } = require( '../../../lib/Handlers/Generic.handler.js' );
|
||||||
|
const Model = getModel('loads');
|
||||||
|
const CompanyModel = getModel('companies');
|
||||||
|
const ProposalsModel = getModel('proposals');
|
||||||
|
const companyGroupsModel = getModel('company_groups');
|
||||||
|
|
||||||
|
const carrier_projection = [
|
||||||
|
'company_name',
|
||||||
|
'company_code',
|
||||||
|
'createdAt',
|
||||||
|
'rfc'
|
||||||
|
];
|
||||||
|
|
||||||
|
const vehicle_projection = [
|
||||||
|
'vehicle_code',
|
||||||
|
'truck_type',
|
||||||
|
'driver',
|
||||||
|
'categories',
|
||||||
|
'circulation_serial_number',
|
||||||
|
'trailer_plate_1',
|
||||||
|
'trailer_plate_2',
|
||||||
|
'notes',
|
||||||
|
'city',
|
||||||
|
'available_in'
|
||||||
|
];
|
||||||
|
const user_projection = ['first_name','last_name','middle_name'];
|
||||||
|
const populate_list = [
|
||||||
|
'product',
|
||||||
|
'company',
|
||||||
|
'categories',
|
||||||
|
'origin_warehouse',
|
||||||
|
'destination_warehouse',
|
||||||
|
{path:'carrier',select: carrier_projection },
|
||||||
|
{path:'vehicle',select: vehicle_projection, populate : { path : 'categories' } },
|
||||||
|
{path:'driver',select: user_projection },
|
||||||
|
{path:'bidder',select: user_projection },
|
||||||
|
];
|
||||||
|
const generic = new GenericHandler( Model, null, populate_list );
|
||||||
|
|
||||||
|
function getAndFilterList( query ){
|
||||||
|
const filter_list = [];
|
||||||
|
const {
|
||||||
|
company,
|
||||||
|
carrier,
|
||||||
|
vehicle,
|
||||||
|
driver,
|
||||||
|
status,
|
||||||
|
posted_by,
|
||||||
|
posted_by_name,
|
||||||
|
load_status,
|
||||||
|
published_date,
|
||||||
|
loaded_date,
|
||||||
|
transit_date,
|
||||||
|
categories,
|
||||||
|
product,
|
||||||
|
shipment_code,
|
||||||
|
origin_warehouse,
|
||||||
|
destination_warehouse,
|
||||||
|
est_loading_date,
|
||||||
|
est_unloading_date,
|
||||||
|
alert_list,
|
||||||
|
truck_type,
|
||||||
|
state,
|
||||||
|
city,
|
||||||
|
} = 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 ) { 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( origin_warehouse ) { filter_list.push({ origin_warehouse }); }
|
||||||
|
if( destination_warehouse ) { filter_list.push({ destination_warehouse }); }
|
||||||
|
if( alert_list ) { filter_list.push({ alert_list }); }
|
||||||
|
if( truck_type ) { filter_list.push({ truck_type }); }
|
||||||
|
|
||||||
|
if( state ) {
|
||||||
|
filter_list.push({
|
||||||
|
$or:[
|
||||||
|
{ "origin.state": state },
|
||||||
|
{ "destination.state": state },
|
||||||
|
]
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if( city ) {
|
||||||
|
filter_list.push({
|
||||||
|
$or:[
|
||||||
|
{ "origin.city": city },
|
||||||
|
{ "destination.city": city },
|
||||||
|
]
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if( est_loading_date ) {
|
||||||
|
if( (est_loading_date.gte == undefined) || (est_loading_date.gte == null) ){
|
||||||
|
throw "est_loading_date[gte] is required";
|
||||||
|
}
|
||||||
|
if( (est_loading_date.lte == undefined) || (est_loading_date.lte == null) ){
|
||||||
|
throw "est_loading_date[lte] is required";
|
||||||
|
}
|
||||||
|
filter_list.push({
|
||||||
|
"est_loading_date" : {
|
||||||
|
$gte : normalizeDate( est_loading_date["gte"] ),
|
||||||
|
$lte : normalizeDate( est_loading_date["lte"] )
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if( est_unloading_date ) {
|
||||||
|
if( (est_unloading_date.gte == undefined) || (est_unloading_date.gte == null) ){
|
||||||
|
throw "est_unloading_date[gte] is required";
|
||||||
|
}
|
||||||
|
if( (est_unloading_date.lte == undefined) || (est_unloading_date.lte == null) ){
|
||||||
|
throw "est_unloading_date[lte] is required";
|
||||||
|
}
|
||||||
|
filter_list.push({
|
||||||
|
"est_unloading_date" : {
|
||||||
|
$gte : normalizeDate( est_unloading_date["gte"] ),
|
||||||
|
$lte : normalizeDate( est_unloading_date["lte"] )
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if( filter_list.length == 0 ){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return filter_list;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getCompanyIdListFromGroups( companyId ){
|
||||||
|
const privateGroups = await companyGroupsModel.find({
|
||||||
|
allowedCompanies: { $in: [companyId] }
|
||||||
|
});
|
||||||
|
|
||||||
|
if( !privateGroups ){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const companiesIds = privateGroups.map((group) => group.owner);
|
||||||
|
|
||||||
|
return companiesIds;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function findLoads( companyId, query ){
|
||||||
|
const { $sort, company_name } = query;
|
||||||
|
const { page, elements } = getPagination( query );
|
||||||
|
const andFilterList = getAndFilterList( query ) || [];
|
||||||
|
|
||||||
|
const { privacy } = query;
|
||||||
|
const privacyVal = ( privacy && ( privacy >= 1 || privacy.toLowerCase() === 'true' ))? true: false;
|
||||||
|
|
||||||
|
let filter = {};
|
||||||
|
|
||||||
|
if( privacyVal ){
|
||||||
|
const companiesIds = await getCompanyIdListFromGroups( companyId ) || [];
|
||||||
|
filter = {
|
||||||
|
company : { $in : companiesIds },
|
||||||
|
privacy: true,
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
filter = {
|
||||||
|
$or : [
|
||||||
|
{ privacy : false },
|
||||||
|
{ privacy : { $exists : false } }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { findLoads };
|
||||||
185
v1/src/apps/private/loads/driver.services.js
Normal file
185
v1/src/apps/private/loads/driver.services.js
Normal file
@@ -0,0 +1,185 @@
|
|||||||
|
"use strict";
|
||||||
|
const { getModel } = require( '../../../lib/Models' );
|
||||||
|
const { getPagination, genKey, normalizeDate } = require( '../../../lib/Misc.js' );
|
||||||
|
const { GenericHandler } = require( '../../../lib/Handlers/Generic.handler.js' );
|
||||||
|
const { onPatchEvent } = require('../../../lib/Handlers/Loads.handler');
|
||||||
|
const Model = getModel('loads');
|
||||||
|
const CompanyModel = getModel('companies');
|
||||||
|
const ProposalsModel = getModel('proposals');
|
||||||
|
const companyGroupsModel = getModel('company_groups');
|
||||||
|
|
||||||
|
const carrier_projection = [
|
||||||
|
'company_name',
|
||||||
|
'company_code',
|
||||||
|
'createdAt',
|
||||||
|
'rfc'
|
||||||
|
];
|
||||||
|
|
||||||
|
const vehicle_projection = [
|
||||||
|
'vehicle_code',
|
||||||
|
'truck_type',
|
||||||
|
'driver',
|
||||||
|
'categories',
|
||||||
|
'circulation_serial_number',
|
||||||
|
'trailer_plate_1',
|
||||||
|
'trailer_plate_2',
|
||||||
|
'notes',
|
||||||
|
'city',
|
||||||
|
'available_in'
|
||||||
|
];
|
||||||
|
const user_projection = ['first_name','last_name','middle_name'];
|
||||||
|
const populate_list = [
|
||||||
|
'product',
|
||||||
|
'company',
|
||||||
|
'categories',
|
||||||
|
'origin_warehouse',
|
||||||
|
'destination_warehouse',
|
||||||
|
{path:'carrier',select: carrier_projection },
|
||||||
|
{path:'vehicle',select: vehicle_projection, populate : { path : 'categories' } },
|
||||||
|
{path:'driver',select: user_projection },
|
||||||
|
{path:'bidder',select: user_projection },
|
||||||
|
];
|
||||||
|
const generic = new GenericHandler( Model, null, populate_list );
|
||||||
|
|
||||||
|
function getAndFilterList( query ){
|
||||||
|
const filter_list = [];
|
||||||
|
const {
|
||||||
|
company,
|
||||||
|
carrier,
|
||||||
|
vehicle,
|
||||||
|
driver,
|
||||||
|
status,
|
||||||
|
posted_by,
|
||||||
|
posted_by_name,
|
||||||
|
load_status,
|
||||||
|
published_date,
|
||||||
|
loaded_date,
|
||||||
|
transit_date,
|
||||||
|
categories,
|
||||||
|
product,
|
||||||
|
shipment_code,
|
||||||
|
origin_warehouse,
|
||||||
|
destination_warehouse,
|
||||||
|
est_loading_date,
|
||||||
|
est_unloading_date,
|
||||||
|
alert_list,
|
||||||
|
truck_type,
|
||||||
|
state,
|
||||||
|
city,
|
||||||
|
} = 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 ) { 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( origin_warehouse ) { filter_list.push({ origin_warehouse }); }
|
||||||
|
if( destination_warehouse ) { filter_list.push({ destination_warehouse }); }
|
||||||
|
if( alert_list ) { filter_list.push({ alert_list }); }
|
||||||
|
if( truck_type ) { filter_list.push({ truck_type }); }
|
||||||
|
|
||||||
|
if( state ) {
|
||||||
|
filter_list.push({
|
||||||
|
$or:[
|
||||||
|
{ "origin.state": state },
|
||||||
|
{ "destination.state": state },
|
||||||
|
]
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if( city ) {
|
||||||
|
filter_list.push({
|
||||||
|
$or:[
|
||||||
|
{ "origin.city": city },
|
||||||
|
{ "destination.city": city },
|
||||||
|
]
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if( est_loading_date ) {
|
||||||
|
if( (est_loading_date.gte == undefined) || (est_loading_date.gte == null) ){
|
||||||
|
throw "est_loading_date[gte] is required";
|
||||||
|
}
|
||||||
|
if( (est_loading_date.lte == undefined) || (est_loading_date.lte == null) ){
|
||||||
|
throw "est_loading_date[lte] is required";
|
||||||
|
}
|
||||||
|
filter_list.push({
|
||||||
|
"est_loading_date" : {
|
||||||
|
$gte : normalizeDate( est_loading_date["gte"] ),
|
||||||
|
$lte : normalizeDate( est_loading_date["lte"] )
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if( est_unloading_date ) {
|
||||||
|
if( (est_unloading_date.gte == undefined) || (est_unloading_date.gte == null) ){
|
||||||
|
throw "est_unloading_date[gte] is required";
|
||||||
|
}
|
||||||
|
if( (est_unloading_date.lte == undefined) || (est_unloading_date.lte == null) ){
|
||||||
|
throw "est_unloading_date[lte] is required";
|
||||||
|
}
|
||||||
|
filter_list.push({
|
||||||
|
"est_unloading_date" : {
|
||||||
|
$gte : normalizeDate( est_unloading_date["gte"] ),
|
||||||
|
$lte : normalizeDate( est_unloading_date["lte"] )
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { findLoads };
|
||||||
@@ -3,7 +3,6 @@ const router = require('express').Router();
|
|||||||
const services= require('./services.js');
|
const services= require('./services.js');
|
||||||
|
|
||||||
router.get('/find', services.findList);
|
router.get('/find', services.findList);
|
||||||
router.get('/driver', services.findDriverList);
|
|
||||||
router.get('/calendar', services.findCalendarList);
|
router.get('/calendar', services.findCalendarList);
|
||||||
router.post('/new', services.postLoad);
|
router.post('/new', services.postLoad);
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,11 @@ const { getModel } = require( '../../../lib/Models' );
|
|||||||
const { getPagination, genKey, normalizeDate } = require( '../../../lib/Misc.js' );
|
const { getPagination, genKey, normalizeDate } = require( '../../../lib/Misc.js' );
|
||||||
const { GenericHandler } = require( '../../../lib/Handlers/Generic.handler.js' );
|
const { GenericHandler } = require( '../../../lib/Handlers/Generic.handler.js' );
|
||||||
const { onPatchEvent } = require('../../../lib/Handlers/Loads.handler');
|
const { onPatchEvent } = require('../../../lib/Handlers/Loads.handler');
|
||||||
|
|
||||||
|
const CarrierServices = require('./carrier.services.js');
|
||||||
|
const ShipperServices = require('./shipper.services.js');
|
||||||
|
const DriverServices = require('./driver.services.js');
|
||||||
|
|
||||||
const Model = getModel('loads');
|
const Model = getModel('loads');
|
||||||
const CompanyModel = getModel('companies');
|
const CompanyModel = getModel('companies');
|
||||||
const ProposalsModel = getModel('proposals');
|
const ProposalsModel = getModel('proposals');
|
||||||
@@ -156,49 +161,6 @@ async function getCompanyIdListFromGroups( companyId ){
|
|||||||
return companiesIds;
|
return companiesIds;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function findDriverLoads( 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 findLoads( companyId, query ){
|
async function findLoads( companyId, query ){
|
||||||
const { $sort, company_name } = query;
|
const { $sort, company_name } = query;
|
||||||
const { page, elements } = getPagination( query );
|
const { page, elements } = getPagination( query );
|
||||||
@@ -357,7 +319,29 @@ const findList = async(req, res) => {
|
|||||||
try{
|
try{
|
||||||
const companyId = req.context.companyId;
|
const companyId = req.context.companyId;
|
||||||
const query = req.query || {};
|
const query = req.query || {};
|
||||||
const retVal = await findLoads( companyId, query );
|
|
||||||
|
let retVal;
|
||||||
|
|
||||||
|
switch( req.context.user?.company?.company_type ){
|
||||||
|
case "Shipper":
|
||||||
|
console.log("Shipper loads finder");
|
||||||
|
retVal = await ShipperServices.findLoads( companyId, query );
|
||||||
|
break;
|
||||||
|
case "Carrier":
|
||||||
|
if( req.context.user?.job_role === "driver" ){
|
||||||
|
console.log("Driver loads finder");
|
||||||
|
retVal = await DriverServices.findLoads( query );
|
||||||
|
}else{
|
||||||
|
console.log("Carrier load finder");
|
||||||
|
retVal = await CarrierServices.findLoads( companyId, query );
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
console.log( req.context.user.company.company_type );
|
||||||
|
console.log( req.context.user.job_role );
|
||||||
|
retVal = await CarrierServices.findLoads( companyId, query );
|
||||||
|
break;
|
||||||
|
}
|
||||||
res.send( retVal );
|
res.send( retVal );
|
||||||
}catch(error){
|
}catch(error){
|
||||||
console.error( error );
|
console.error( error );
|
||||||
@@ -365,17 +349,6 @@ const findList = async(req, res) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const findDriverList = async ( req, res ) => {
|
|
||||||
try{
|
|
||||||
const query = req.query || {};
|
|
||||||
const retVal = await findDriverLoads( companyId, query );
|
|
||||||
res.send( retVal );
|
|
||||||
}catch(error){
|
|
||||||
console.error( 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;
|
||||||
@@ -610,4 +583,4 @@ const deleteLoad = async(req, res) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = { findCalendarList, findList, findDriverList, getById, patchLoad, postLoad, deleteLoad };
|
module.exports = { findCalendarList, findList, getById, patchLoad, postLoad, deleteLoad };
|
||||||
|
|||||||
199
v1/src/apps/private/loads/shipper.services.js
Normal file
199
v1/src/apps/private/loads/shipper.services.js
Normal file
@@ -0,0 +1,199 @@
|
|||||||
|
"use strict";
|
||||||
|
const { getModel } = require( '../../../lib/Models' );
|
||||||
|
const { getPagination, genKey, normalizeDate } = require( '../../../lib/Misc.js' );
|
||||||
|
const { GenericHandler } = require( '../../../lib/Handlers/Generic.handler.js' );
|
||||||
|
const { onPatchEvent } = require('../../../lib/Handlers/Loads.handler');
|
||||||
|
const Model = getModel('loads');
|
||||||
|
const CompanyModel = getModel('companies');
|
||||||
|
const ProposalsModel = getModel('proposals');
|
||||||
|
const companyGroupsModel = getModel('company_groups');
|
||||||
|
|
||||||
|
const carrier_projection = [
|
||||||
|
'company_name',
|
||||||
|
'company_code',
|
||||||
|
'createdAt',
|
||||||
|
'rfc'
|
||||||
|
];
|
||||||
|
|
||||||
|
const vehicle_projection = [
|
||||||
|
'vehicle_code',
|
||||||
|
'truck_type',
|
||||||
|
'driver',
|
||||||
|
'categories',
|
||||||
|
'circulation_serial_number',
|
||||||
|
'trailer_plate_1',
|
||||||
|
'trailer_plate_2',
|
||||||
|
'notes',
|
||||||
|
'city',
|
||||||
|
'available_in'
|
||||||
|
];
|
||||||
|
const user_projection = ['first_name','last_name','middle_name'];
|
||||||
|
const populate_list = [
|
||||||
|
'product',
|
||||||
|
'company',
|
||||||
|
'categories',
|
||||||
|
'origin_warehouse',
|
||||||
|
'destination_warehouse',
|
||||||
|
{path:'carrier',select: carrier_projection },
|
||||||
|
{path:'vehicle',select: vehicle_projection, populate : { path : 'categories' } },
|
||||||
|
{path:'driver',select: user_projection },
|
||||||
|
{path:'bidder',select: user_projection },
|
||||||
|
];
|
||||||
|
const generic = new GenericHandler( Model, null, populate_list );
|
||||||
|
|
||||||
|
function getAndFilterList( query ){
|
||||||
|
const filter_list = [];
|
||||||
|
const {
|
||||||
|
company,
|
||||||
|
carrier,
|
||||||
|
vehicle,
|
||||||
|
driver,
|
||||||
|
status,
|
||||||
|
posted_by,
|
||||||
|
posted_by_name,
|
||||||
|
load_status,
|
||||||
|
published_date,
|
||||||
|
loaded_date,
|
||||||
|
transit_date,
|
||||||
|
categories,
|
||||||
|
product,
|
||||||
|
shipment_code,
|
||||||
|
origin_warehouse,
|
||||||
|
destination_warehouse,
|
||||||
|
est_loading_date,
|
||||||
|
est_unloading_date,
|
||||||
|
alert_list,
|
||||||
|
truck_type,
|
||||||
|
state,
|
||||||
|
city,
|
||||||
|
} = 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 ) { 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( origin_warehouse ) { filter_list.push({ origin_warehouse }); }
|
||||||
|
if( destination_warehouse ) { filter_list.push({ destination_warehouse }); }
|
||||||
|
if( alert_list ) { filter_list.push({ alert_list }); }
|
||||||
|
if( truck_type ) { filter_list.push({ truck_type }); }
|
||||||
|
|
||||||
|
if( state ) {
|
||||||
|
filter_list.push({
|
||||||
|
$or:[
|
||||||
|
{ "origin.state": state },
|
||||||
|
{ "destination.state": state },
|
||||||
|
]
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if( city ) {
|
||||||
|
filter_list.push({
|
||||||
|
$or:[
|
||||||
|
{ "origin.city": city },
|
||||||
|
{ "destination.city": city },
|
||||||
|
]
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if( est_loading_date ) {
|
||||||
|
if( (est_loading_date.gte == undefined) || (est_loading_date.gte == null) ){
|
||||||
|
throw "est_loading_date[gte] is required";
|
||||||
|
}
|
||||||
|
if( (est_loading_date.lte == undefined) || (est_loading_date.lte == null) ){
|
||||||
|
throw "est_loading_date[lte] is required";
|
||||||
|
}
|
||||||
|
filter_list.push({
|
||||||
|
"est_loading_date" : {
|
||||||
|
$gte : normalizeDate( est_loading_date["gte"] ),
|
||||||
|
$lte : normalizeDate( est_loading_date["lte"] )
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if( est_unloading_date ) {
|
||||||
|
if( (est_unloading_date.gte == undefined) || (est_unloading_date.gte == null) ){
|
||||||
|
throw "est_unloading_date[gte] is required";
|
||||||
|
}
|
||||||
|
if( (est_unloading_date.lte == undefined) || (est_unloading_date.lte == null) ){
|
||||||
|
throw "est_unloading_date[lte] is required";
|
||||||
|
}
|
||||||
|
filter_list.push({
|
||||||
|
"est_unloading_date" : {
|
||||||
|
$gte : normalizeDate( est_unloading_date["gte"] ),
|
||||||
|
$lte : normalizeDate( est_unloading_date["lte"] )
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if( filter_list.length == 0 ){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return filter_list;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getCompanyIdListFromGroups( companyId ){
|
||||||
|
const privateGroups = await companyGroupsModel.find({
|
||||||
|
allowedCompanies: { $in: [companyId] }
|
||||||
|
});
|
||||||
|
|
||||||
|
if( !privateGroups ){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const companiesIds = privateGroups.map((group) => group.owner);
|
||||||
|
|
||||||
|
return companiesIds;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function findLoads( companyId, 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { findLoads };
|
||||||
@@ -1,8 +1,11 @@
|
|||||||
'user strict';
|
'user strict';
|
||||||
const { ROOT_PATH, HANDLERS_PATH, LIB_PATH } = process.env;
|
const { ROOT_PATH, HANDLERS_PATH, LIB_PATH } = process.env;
|
||||||
const { getModel } = require( '../Models' );
|
const { getModel } = require( '../Models' );
|
||||||
|
const apiConfig = require( '../../config/apiConfig.json' );
|
||||||
const { GenericHandler } = require( '../Handlers/Generic.handler' );
|
const { GenericHandler } = require( '../Handlers/Generic.handler' );
|
||||||
const { getPagination, genKey } = require( '../../lib/Misc' );
|
const { getPagination, genKey, toSha256 } = require( '../../lib/Misc' );
|
||||||
|
|
||||||
|
const pwd_secret = apiConfig.authentication.pwdSecret;
|
||||||
|
|
||||||
const usersModel = getModel('users');
|
const usersModel = getModel('users');
|
||||||
const companiesModel = getModel('companies');
|
const companiesModel = getModel('companies');
|
||||||
@@ -91,7 +94,6 @@ async function findUsers( query ){
|
|||||||
|
|
||||||
function clean_user_data( data , company ){
|
function clean_user_data( data , company ){
|
||||||
/// Avoid modifying sensitive fields.
|
/// Avoid modifying sensitive fields.
|
||||||
if( data.password ){ delete data.password; }
|
|
||||||
if( data.company ){ delete data.company; }
|
if( data.company ){ delete data.company; }
|
||||||
if( data.job_role ){
|
if( data.job_role ){
|
||||||
/** You can only modify the role of a user if it is in the following list */
|
/** You can only modify the role of a user if it is in the following list */
|
||||||
@@ -106,6 +108,10 @@ function clean_user_data( data , company ){
|
|||||||
data.job_role = "staff";
|
data.job_role = "staff";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if( data.password ){
|
||||||
|
/// Generate safe password
|
||||||
|
data.password = toSha256( data.password + pwd_secret );
|
||||||
|
}
|
||||||
if( data.permissions ){ delete data.permissions; }
|
if( data.permissions ){ delete data.permissions; }
|
||||||
if( company ){
|
if( company ){
|
||||||
if( company.company_type === 'Shipper' ){
|
if( company.company_type === 'Shipper' ){
|
||||||
|
|||||||
Reference in New Issue
Block a user