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

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 };