151 lines
4.9 KiB
JavaScript
151 lines
4.9 KiB
JavaScript
"use strict";
|
|
const { ROOT_PATH, LIB_PATH, MODELS_PATH, API_CONFIG } = process.env;
|
|
const { getPagination , getPage } = require( `${ROOT_PATH}/${LIB_PATH}/Misc.js` );
|
|
const apiConfig = require( `${ROOT_PATH}/${API_CONFIG}` );
|
|
|
|
const { S3Client, PutObjectCommand } = require('@aws-sdk/client-s3');
|
|
const s3Client = new S3Client({
|
|
region : apiConfig.S3.region,
|
|
credentials : {
|
|
accessKeyId : apiConfig.S3.accessKeyId,
|
|
secretAccessKey : apiConfig.S3.secretAccessKey
|
|
}
|
|
});
|
|
const s3Bucket = apiConfig.S3.bucket;
|
|
const s3BucketKey = apiConfig.S3.key;
|
|
|
|
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.params.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 getPage( page, elements, Model, filter );
|
|
res.send( retVal );
|
|
};
|
|
|
|
const getLoadAttachmentList = async(req, res) => {
|
|
const loadId = req.params.id;
|
|
const CompanyAccessFilter = await getAuthorizationFilter( req.JWT.payload.sub );
|
|
console.log( loadId );
|
|
const filter = {
|
|
$and : [
|
|
{ load : loadId },
|
|
CompanyAccessFilter
|
|
]
|
|
};
|
|
const { page , elements } = getPagination( req.query );
|
|
const retVal = await getPage( page, elements, Model, filter );
|
|
res.send( retVal );
|
|
};
|
|
|
|
async function getLoadById( loadId , companyId ){
|
|
const filter = {
|
|
$and : [
|
|
{ _id : loadId },
|
|
{
|
|
$or: [
|
|
{ company : companyId },
|
|
{ carrier : companyId },
|
|
]
|
|
}
|
|
]
|
|
};
|
|
return await LoadsModel.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 = Date.now();
|
|
await prevAttachment.save();
|
|
attachment = prevAttachment;
|
|
}else{
|
|
/**
|
|
* load is not valid => I don't have access to this load!
|
|
*/
|
|
attachment = null;
|
|
}
|
|
return attachment;
|
|
}
|
|
|
|
async function uploadFile( bucket, key, file , obj_id ){
|
|
const params = {
|
|
Bucket: bucket,
|
|
Key : `${key}/${obj_id}`,
|
|
ContentType : file.mimetype,
|
|
Body : file.data
|
|
};
|
|
const s3resp = await s3Client.send( new PutObjectCommand( params ) );
|
|
return s3resp;
|
|
}
|
|
const postLoadingAttachment = async(req, res) => {
|
|
const loadId = req.params.id;
|
|
const attachment = await createLoadAttachment( "Loading", req.JWT.payload.sub , loadId );
|
|
const file = req.files.attachment;
|
|
if( attachment && file ){
|
|
const s3resp = await uploadFile( s3Bucket, s3BucketKey, file , attachment._id );
|
|
res.send( attachment );
|
|
}else if( !file ){
|
|
res.status(400).send({ error : "attachment file not found" , code: 400 });
|
|
}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 );
|
|
const file = req.files.attachment;
|
|
if( attachment && file ){
|
|
const s3resp = await uploadFile( s3Bucket, s3BucketKey, file , attachment._id );
|
|
res.send( attachment );
|
|
}else if( !file ){
|
|
res.status(400).send({ error : "attachment file not found" , code: 400 });
|
|
}else{
|
|
res.status(401).send({error:"Unauthorized",code:401});
|
|
}
|
|
};
|
|
|
|
module.exports = { getAttachment, getAttachmentList, getLoadAttachmentList, postLoadingAttachment, postDownloadingAttachment };
|