Files
ETAApi/v1/src/apps/public/public-load-tracking/services.js
2024-10-11 20:51:26 -06:00

60 lines
1.7 KiB
JavaScript

"use strict";
const { getModel } = require( '../../../lib/Models' );
const Model = getModel('loads');
const vehicle_projection = ['background_tracking','status','last_location_lat',
'last_location_lng','last_location_geo','last_location_time']
const user_projection = ['first_name','last_name','middle_name']
const company_projection = ["company_name"]
const populate_list = [
{path:'posted_by',select: user_projection },
{path:'vehicle',select: vehicle_projection },
{path:'company',select: company_projection },
'posted_by_name',
'categories'
];
const getById = async(req, res) => {
try{
const elementId = req.params.id;
const select = [
"categories",
"shipment_code",
"truck_type",
"published_date",
"createdAt",
"status",
"load_status",
"weight",
"product",
"est_loading_date",
"est_unloading_date",
"origin.city",
"origin.state",
"origin.lat",
"origin.lng",
"destination.city",
"destination.state",
"destination.lat",
"destination.lng",
];
// const load = await Model.findOne( { _id : elementId , load_status : "Transit" } , select ).populate( populate_list );
const load = await Model.findById( elementId , select ).populate( populate_list );
if( load ){
return res.send( load );
}else{
return res.status(400).send({
error : `Load [${elementId}] not found!`
})
}
}catch(error){
console.error( error );
return res.status( 500 ).send({ error });
}
};
module.exports = { getById };