feat: Adding loads and load-attachments endpoints

This commit is contained in:
2023-10-06 20:01:04 -06:00
parent 0b37ea1ac1
commit d019017600
11 changed files with 257 additions and 12 deletions

View File

@@ -27,8 +27,8 @@ function getPagination( query ){
async function queryPage( page, elements, model, filter=null, projection=null){ async function queryPage( page, elements, model, filter=null, projection=null){
const skip = elements * page; const skip = elements * page;
const total = await model.count(); const total = await model.count( filter );
const list = await model.find( filter, projection, { skip : skip , limit : elements } ); const list = await model.find( filter , projection, { skip : skip , limit : elements } );
return { return {
total : total, total : total,
limit : elements, limit : elements,

View File

@@ -0,0 +1,19 @@
const mongoose = require('mongoose');
const { Schema } = mongoose;
const schema = new Schema({
status: { type: String, default: 'Draft', enum: ['Draft', 'Done'] },/*Once in Done state, no changes are allowed.*/
updatedAt: {
type: Date,
default : () => Date.now()
},
type: { type: String, enum: ['Loading', 'Downloading'], required : true },
company: { type: Schema.Types.ObjectId, ref: 'companies' }, //shipper
carrier: { type: Schema.Types.ObjectId, ref: 'companies', required: true }, // carrier
load: { type: Schema.Types.ObjectId, ref: 'loads', required: true },
author: { type: Schema.Types.ObjectId, ref: 'users', required: true },
doneAt: { type: Date }
});
module.exports = mongoose.model( "loadattachments", schema );

View File

@@ -50,7 +50,7 @@ const schema = new Schema({
type: pointSchema, type: pointSchema,
}, },
categories: [{ type: Schema.Types.ObjectId, ref: 'product-categories' }], categories: [{ type: Schema.Types.ObjectId, ref: 'productcategories' }],
product: { type: Schema.Types.ObjectId, ref: 'products' }, product: { type: Schema.Types.ObjectId, ref: 'products' },
truck_type: { type: String }, truck_type: { type: String },
@@ -80,7 +80,7 @@ const schema = new Schema({
loaded_date: { type: Date }, loaded_date: { type: Date },
transit_date: { type: Date }, transit_date: { type: Date },
delivered_date: { type: Date }, delivered_date: { type: Date },
load_status_updated: { type: Date, default: Date.now() }, load_status_updated: { type: Date, default: () => Date.now() },
notes: { type: String }, notes: { type: String },
payment_term: { type: String }, payment_term: { type: String },

View File

@@ -7,11 +7,15 @@ const jwtSecret = apiConfig.authentication.jwtSecret;
function middleware( req, res, next ){ function middleware( req, res, next ){
if( req.JWT ){ if( req.JWT ){
req.JWT.isValid = false; req.JWT.isValid = false;
req.JWT.payload = jwt.verify( req.JWT.raw, jwtSecret ); try{
if( !req.JWT.payload ){ req.JWT.payload = jwt.verify( req.JWT.raw, jwtSecret );
if( !req.JWT.payload ){
return res.status(401).send({error:"Unauthorized",code:401});
}else{
req.JWT.isValid = true;
}
}catch( err ){
return res.status(401).send({error:"Unauthorized",code:401}); return res.status(401).send({error:"Unauthorized",code:401});
}else{
req.JWT.isValid = true;
} }
next(); next();
}else{ }else{

View File

@@ -0,0 +1,11 @@
'use strict';
const router = require('express').Router();
const services= require('./services.js');
router.post('/loading/:id', services.postLoadingAttachment );
router.post('/downloading/:id', services.postDownloadingAttachment );
router.get('/load/:id', services.getLoadAttachmentList );
router.get('/:id', services.getAttachment );
router.get('/', services.getAttachmentList );
module.exports = router;

View File

@@ -0,0 +1,118 @@
"use strict";
const { ROOT_PATH, LIB_PATH, MODELS_PATH, HANDLERS_PATH } = process.env;
const { getPagination , queryPage } = require( `${ROOT_PATH}/${LIB_PATH}/Misc.js` );
const Model = require( `${ROOT_PATH}/${MODELS_PATH}/load-attachments.model.js` );
const UserModel = require( `${ROOT_PATH}/${MODELS_PATH}/users.model.js` );
const LoadsModel = require( `${ROOT_PATH}/${MODELS_PATH}/loads.model.js` );
async function getAuthorizationFilter( userId ){
const user = await UserModel.findById( userId );
const companyId = user.company.toString();
return {
$or: [
{ company : companyId },
{ carrier : companyId },
]
};
}
const getAttachment = async(req, res) => {
const attachmentId = req.query.id;
const CompanyAccessFilter = await getAuthorizationFilter( req.JWT.payload.sub );
const filter = {
$and : [
{ _id : attachmentId },
CompanyAccessFilter
]
};
const retVal = await Model.findOne( filter ) || {};
res.send( retVal );
};
const getAttachmentList = async(req, res) => {
const filter = await getAuthorizationFilter( req.JWT.payload.sub );
const { page , elements } = getPagination( req.query );
const retVal = await queryPage( page, elements, Model, filter );
res.send( retVal );
};
const getLoadAttachmentList = async(req, res) => {
const loadId = req.query.id;
const CompanyAccessFilter = await getAuthorizationFilter( req.JWT.payload.sub );
const filter = {
$and : [
{ load : loadId },
CompanyAccessFilter
]
};
const { page , elements } = getPagination( req.query );
const retVal = await queryPage( page, elements, Model, filter );
res.send( retVal );
};
async function getLoadById( loadId , companyId ){
const filter = {
$and : [
{ _id : loadId },
{
$or: [
{ company : companyId },
{ carrier : companyId },
]
}
]
};
return await Model.findOne( filter ) || null;
}
async function createLoadAttachment( type , userId , loadId ){
const user = await UserModel.findById( userId );
const companyId = user.company.toString();
const load = await getLoadById( loadId , companyId );
const prevAttachment = (load)? await Model.findOne({ load : loadId , type : type }) : null;
let attachment = null;
if( load && !prevAttachment ){
attachment = new Model({
type : type,
carrier : companyId,
load : loadId,
author : userId
});
await attachment.save();
}
else if( load && prevAttachment ){
prevAttachment.updatedAt = new Date.now();
await prevAttachment.save();
attachment = prevAttachment;
}else{
/**
* load is not valid => I don't have access to this load!
*/
attachment = null;
}
return attachment;
}
const postLoadingAttachment = async(req, res) => {
const loadId = req.params.id;
const attachment = await createLoadAttachment( "Loading", req.JWT.payload.sub , loadId );
if( attachment ){
res.send( attachment );
}else{
res.status(401).send({error:"Unauthorized",code:401});
}
};
const postDownloadingAttachment = async(req, res) => {
const loadId = req.params.id;
const attachment = await createLoadAttachment( "Downloading", req.JWT.payload.sub , loadId );
if( attachment ){
res.send( attachment );
}else{
res.status(401).send({error:"Unauthorized",code:401});
}
};
module.exports = { getAttachment, getAttachmentList, getLoadAttachmentList, postLoadingAttachment, postDownloadingAttachment };

8
sections/loads/routes.js Normal file
View File

@@ -0,0 +1,8 @@
'use strict';
const router = require('express').Router();
const services= require('./services.js');
router.get('/', services.getLoadsList);
router.get('/:id', services.getLoad);
module.exports = router;

View File

@@ -0,0 +1,38 @@
"use strict";
const { ROOT_PATH, LIB_PATH, MODELS_PATH, HANDLERS_PATH } = process.env;
const { getPagination , queryPage } = require( `${ROOT_PATH}/${LIB_PATH}/Misc.js` );
const Model = require( `${ROOT_PATH}/${MODELS_PATH}/loads.model.js` );
const UserModel = require( `${ROOT_PATH}/${MODELS_PATH}/users.model.js` );
async function getAuthorizationFilter( userId ){
const user = await UserModel.findById( userId );
const companyId = user.company.toString();
return {
$or: [
{ company : companyId },
{ carrier : companyId },
]
};
}
const getLoadsList = async(req, res) => {
const filter = await getAuthorizationFilter( req.JWT.payload.sub );
const { page , elements } = getPagination( req.query );
const retVal = await queryPage( page , elements, Model, filter );
res.send( retVal );
};
const getLoad = async(req, res) => {
const loadId = req.params.id;
const CompanyAccessFilter = await getAuthorizationFilter( req.JWT.payload.sub );
const filter = {
$and : [
{ _id : loadId },
CompanyAccessFilter
]
};
const retVal = await Model.findOne( filter ).populate('product').populate('categories') || {};
res.send( retVal );
};
module.exports = { getLoadsList, getLoad };

View File

@@ -0,0 +1,9 @@
'use strict';
const router = require('express').Router();
const services= require('./services.js');
router.get('/', services.getProductsList);
router.get('/find', services.findProductsList);
router.get('/:id', services.getProduct);
module.exports = router;

View File

@@ -0,0 +1,28 @@
"use strict";
const { ROOT_PATH, LIB_PATH, MODELS_PATH, HANDLERS_PATH } = process.env;
const { getPagination , queryPage } = require( `${ROOT_PATH}/${LIB_PATH}/Misc.js` );
const Model = require( `${ROOT_PATH}/${MODELS_PATH}/products.model.js` );
const getProductsList = async(req, res) => {
const { page , elements } = getPagination( req.query );
const retVal = await queryPage( page , elements, Model );
res.send( retVal );
};
const findProductsList = async(req, res) => {
let filter=null;
if( req.query.regex ){
const re = new RegExp( req.query.regex );
filter = { "name" : { $regex: re, $options: 'i' }};
}
const { page , elements } = getPagination( req.query );
const retVal = await queryPage( page, elements, Model, filter );
res.send( retVal );
};
const getProduct = async(req, res) => {
const retVal = await Model.findById( req.params.id );
res.send( retVal );
};
module.exports = { getProductsList, findProductsList, getProduct };

View File

@@ -5,11 +5,14 @@ const { ROOT_PATH , LIB_PATH } = process.env;
const router = require('express').Router(); const router = require('express').Router();
const jwtValidator = require( `${ROOT_PATH}/${LIB_PATH}/jwtValidator.js` ); const jwtValidator = require( `${ROOT_PATH}/${LIB_PATH}/jwtValidator.js` );
const countries = require('./countries/routes.js');
const cities = require('./cities/routes.js'); const cities = require('./cities/routes.js');
const countries = require('./countries/routes.js');
const loadAttachments = require('./load-attachments/routes.js');
const loads = require('./loads/routes.js');
const metaData = require('./meta-data/routes.js'); const metaData = require('./meta-data/routes.js');
const metaGroups = require('./meta-groups/routes.js'); const metaGroups = require('./meta-groups/routes.js');
const productCategories = require('./product-categories/routes.js'); const productCategories = require('./product-categories/routes.js');
const products = require('./products/routes.js');
const publicVehicles = require('./public-vehicles/routes.js'); const publicVehicles = require('./public-vehicles/routes.js');
const states = require('./states/routes.js'); const states = require('./states/routes.js');
const test = require('./test/routes.js'); const test = require('./test/routes.js');
@@ -20,17 +23,21 @@ router.use('/cities', cities);
router.use('/meta-data', metaData); router.use('/meta-data', metaData);
router.use('/meta-groups', metaGroups); router.use('/meta-groups', metaGroups);
router.use('/product-categories', productCategories); router.use('/product-categories', productCategories);
router.use('/products', products);
router.use("/public-vehicles", publicVehicles); router.use("/public-vehicles", publicVehicles);
router.use('/states', states); router.use('/states', states);
router.use("/test", test); router.use("/test", test);
router.use( jwtValidator.middleware ); router.use( jwtValidator.middleware );
router.use("/users", users); router.use('/users', users);
router.use('/load-attachments', loadAttachments );
router.use('/loads', loads);
router.use('/orders', test); router.use('/orders', test);
router.use('/companies', test); router.use('/companies', test);
router.use('/loads', test);
router.use('/vehicles', test); router.use('/vehicles', test);
router.use('/mailer', test); router.use('/mailer', test);
router.use('/authmanagement', test); router.use('/authmanagement', test);
@@ -39,7 +46,6 @@ router.use('/checkAccount', test);
router.use('/proposals', test); router.use('/proposals', test);
router.use('/bootresolvers', test); router.use('/bootresolvers', test);
router.use('/budgets', test); router.use('/budgets', test);
router.use('/products', test);
router.use('/news', test); router.use('/news', test);
router.use('/branches', test); router.use('/branches', test);
router.use('/trackings', test); router.use('/trackings', test);
@@ -47,4 +53,8 @@ router.use('/upload', test);
router.use('/calendars', test); router.use('/calendars', test);
router.use('/dashboard', test); router.use('/dashboard', test);
/**
* TODO: Add extra middleware to enable endpoints to lock admin resources.
*/
module.exports = router; module.exports = router;