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

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