From a637aa2a2c1712970c2737440c28c2e57d7c07ee Mon Sep 17 00:00:00 2001 From: Josepablo C Date: Thu, 4 Jul 2024 23:41:17 -0600 Subject: [PATCH] feat: Adding load sort field and load/calendar endpoint --- README.md | 162 +++++++++++++++++++++----- src/apps/private/lib/context/index.js | 2 +- src/apps/private/loads/routes.js | 1 + src/apps/private/loads/services.js | 89 +++++++++++++- src/lib/Handlers/Generic.handler.js | 15 ++- 5 files changed, 232 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index e08a8e4..65669c4 100644 --- a/README.md +++ b/README.md @@ -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 + ### /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. diff --git a/src/apps/private/lib/context/index.js b/src/apps/private/lib/context/index.js index b6800ec..8f585fd 100644 --- a/src/apps/private/lib/context/index.js +++ b/src/apps/private/lib/context/index.js @@ -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(); diff --git a/src/apps/private/loads/routes.js b/src/apps/private/loads/routes.js index f4ddf71..038099a 100644 --- a/src/apps/private/loads/routes.js +++ b/src/apps/private/loads/routes.js @@ -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); diff --git a/src/apps/private/loads/services.js b/src/apps/private/loads/services.js index 0581965..a410822 100644 --- a/src/apps/private/loads/services.js +++ b/src/apps/private/loads/services.js @@ -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 { + 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 }; diff --git a/src/lib/Handlers/Generic.handler.js b/src/lib/Handlers/Generic.handler.js index c06e7b0..32e8431 100644 --- a/src/lib/Handlers/Generic.handler.js +++ b/src/lib/Handlers/Generic.handler.js @@ -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,