feat: Adding loads and load-attachments endpoints
This commit is contained in:
11
sections/load-attachments/routes.js
Normal file
11
sections/load-attachments/routes.js
Normal 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;
|
||||
118
sections/load-attachments/services.js
Normal file
118
sections/load-attachments/services.js
Normal 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 };
|
||||
Reference in New Issue
Block a user