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

160
README.md
View File

@@ -223,6 +223,14 @@ Get public fields from registered vehicles.
- `GET /published`: List only latest published vehicles.
- `GET /location`: List only location from vehicles in status Free.
### /news
- `GET /` : Get a list of elements with pagination.
- `GET /find` : Find a list of elements with any of the following fields:
- regex
- `GET /:id` : Get data from specific element.
- `GET /download/:image_name`: Download image from DB.
## Private Endpoints
The following list of endpoints requires a JWT.
@@ -230,23 +238,23 @@ The following list of endpoints requires a JWT.
- `GET /loads`: List loads related to my company.
- `GET /load-attachments`: List load attachments related to my company or load id.
### /public-load-tracking
## Test Endpoint
- `GET /:id`: Get tracking data from load id
### Test Endpoint
A private endpoint to test the JWT and the api response.
- `POST /apitest`: Return whatever is sent on the body, queries and parameters.
- `GET /version`: Return the API version.
## Private endpoints
Registered resources:
### /account
Complete the register process
- `POST /register` : Creates the company from the body data provided and assigns account to this new company.
#### POST /account/register
Send company data to complete the register.
@@ -279,51 +287,121 @@ Returns a company object
### /branches
Work In Progress
- `GET /find` : Find a list of elements with any of the following fields:
- categories
- branch_name
- phone
- city
- state
- truck_type
- `POST /new` : Creates a new element.
- `PATCH /:id` : Updates data from specific element.
- `DELETE /:id` : Delete element from company. Returns the element data.
- `GET /:id` : Get data from specific element.
### /budgets
Work In Progress
- `GET /find` : Find a list of elements with any of the following fields:
- client
- material
- origin
- destination
- truck_type
- num_tons
- price_per_ton
- tonnage
- pickup_distance
- delivery_distance
- warehouse_distance
- total_km_travel
- cost_per_liter
- fuel_price_per_liter
- other_fuel_expenses
- total_fuel_consumed
- total_cost_fuel
- driver_salary
- accomadation_allowance
- other_administrative_expenses
- total_before_tax
- total_utility_per_km
- total_profit
- profit_percentage
- total_administrative_expenses
- `POST /new` : Creates a new element.
- `PATCH /:id` : Updates data from specific element.
- `DELETE /:id` : Delete element from company. Returns the element data.
- `GET /:id` : Get data from specific element.
### /calendars
Work In Progress
- `GET /find` : Find a list of elements with any of the following fields:
- date_start
- date_end
- load_status
### /companies
Same as public-companies but with full pagination.
### /dashboard
Work In Progress
- `GET /own` : Get own company data.
- `PATCH /own` : Update own company data.
- `GET /shipper` : Get list of shipper companies with pagination using the following filters: company_type, company_name, truck_type, categories, company_state, company_city
- `GET /carrier` : Get list of carrier companies with pagination using the following filters: company_type, company_name, truck_type, categories, company_state, company_city
- `GET /users/:companyId` : Get the list of users within a company.
- `GET /:id` : Get data from specific company.
### /load-attachments
Work In Progress
- `POST /loading/:id` : Upload/Update a loading attachment.
- `POST /downloading/:id` : Upload/Update a download attachment
- `GET /load/:id` : Get the list of attachment ids from load.
- `GET /:id` : Get attachment file.
- `GET /` : Get attachment list from company.
### /loads
Work In Progress
- `GET /find` : Find a list of elements with any of the following fields:
- company
- carrier
- vehicle
- driver
- status
- posted_by_name
- load_status
- published_date
- loaded_date
- transit_date
- categories
- product
- shipment_code
- $sort[ field ] : -1/1 ; Sort result by field name
- `GET /calendar` : Find a list of elements with any of the following fields:
- load_status_updated[gte] : Date grater than.
- load_status_updated[lte] : Date less than.
- load_status : string enumerator ['Published', 'Loading', 'Transit', 'Downloading', 'Delivered'].
- $sort[ field ] : -1/1 ; Sort result by field name
- `POST /new` : Creates a new element.
- `PATCH /:id` : Updates data from specific element.
- `DELETE /:id` : Delete element from company. Returns the element data.
- `GET /:id` : Get data from specific element.
### /mailer
##### Example of filter and sort:
Work In Progress
- `GET /calendar?$sort[createdAt]=-1&load_status_updated[gte]=2022-08-25&load_status_updated[lte]=2022-05-25&load_status=Loading`: Get loads within date range 2022-08-25 -> 2022-05-25 with load_state "Loading" and sort it by createdAt field.
### /news
Work In Progress
### /orders
Work In Progress
- `GET /loads/find?$sort[createdAt]=-1&load_status=Loading`: Get loads with status "Loading" sorted by createdAt.
### /proposals
Work In Progress
### /trackings
Work In Progress
- `GET /find` : Find a list of elements with any of the following fields:
- load
- categories
- branch_name
- phone, city
- state
- truck_type
- `POST /new` : Creates a new element.
- `PATCH /:id` : Updates data from specific element.
- `DELETE /:id` : Delete element from company. Returns the element data.
- `GET /:id` : Get data from specific element.
### /upload
@@ -341,4 +419,24 @@ Work In Progress
### /vehicles
Work In Progress
__This endpoint is only valid for carriers.__
- `GET /find` : Find a list of elements with any of the following fields:
- categories,
- active_load,
- load_shipper,
- driver,
- vehicle_code,
- vehicle_name,
- vehicle_number,
- circulation_serial_number,
- truck_type,
- tyre_type,
- city,
- state,
- status,
- destino
- `POST /new` : Creates a new element.
- `PATCH /:id` : Updates data from specific element.
- `DELETE /:id` : Delete element from company. Returns the element data.
- `GET /:id` : Get data from specific element.

View File

@@ -12,7 +12,7 @@ async function middleware( req, res, next ){
user : await usersModel.findById( userID , { password : 0 , session_token : 0 , session_token_exp : 0 } ).populate('company')
}
req.context.userId = req.context.user.id;
req.context.companyId = req.context.user.company || null;
req.context.companyId = req.context.user.company.id || null;
req.context.job_role = req.context.user.job_role || null;
req.context.permissions = req.context.user.permissions || null;
next();

View File

@@ -3,6 +3,7 @@ const router = require('express').Router();
const services= require('./services.js');
router.get('/find', services.findList);
router.get('/calendar', services.findCalendarList);
router.post('/new', services.postLoad);
router.patch('/:id', services.patchLoad);

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 };

View File

@@ -44,9 +44,15 @@ class GenericHandler{
return await query.exec();
}
async getList( page, elements, filter=null, projection=null ){
async getList( page, elements, filter=null, projection=null, sort=null ){
const { query , total, skip } = await getPageQuery( page , elements, this.Model, filter, projection );
if( sort ){
query.sort( sort );
}
const list = await this.populateQuery( query );
return {
total : total,
limit : elements,
@@ -70,8 +76,13 @@ class GenericHandler{
* @param {*} projection
* @returns { total , limit, skip, query}
*/
async getListQuery( page, elements , filter=null, projection=null ){
async getListQuery( page, elements , filter=null, projection=null, sort=null ){
const { query , total, skip } = await getPageQuery( page , elements, this.Model, filter, projection );
if( sort ){
query.sort( sort );
}
return {
total : total,
limit : elements,