feat: Private Groups + Load-Templates
Private Groups enabled based on the following assumptions: * There is only one private group. * The companies added to the private group is able to find private loads in the directory. * The companies added to the private group is able to find private companies in the directory. * The driver requires a new endpoint to read load resources no matter the privacy rules (as long as they are added to the load as driver). * The private group can be updated by anyone within the company. * The privacy is enabled on the company information by enabling `privacy=true`. * The privacy is enabled on the loads information by enabling `privacy=true`. * When looking for loads/companies by default the search is with `privacy=false` which returns public elements. When `privacy=true`is given, the results are limited to private elements only (not including public elements). Load-Templates enabled based on the following assumptions: * Anyone can CRUD the templates, there is no find feature (for now). It is assumed that the number of templates is limited.
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
"use strict";
|
||||
const { ROOT_PATH, MODELS_PATH, HANDLERS_PATH, LIB_PATH } = process.env;
|
||||
const { getModel } = require( `${ROOT_PATH}/${MODELS_PATH}` );
|
||||
const { getModel } = require( '../../../lib/Models' );
|
||||
const { GenericHandler } = require( '../../../lib/Handlers/Generic.handler.js' );
|
||||
const { getPagination } = require( `${ROOT_PATH}/${LIB_PATH}/Misc.js` );
|
||||
const { getPagination } = require( '../../../lib/Misc.js' );
|
||||
|
||||
const usersModel = getModel('users');
|
||||
const companiesModel = getModel('companies');
|
||||
const companyGroupsModel = getModel('company_groups');
|
||||
const branchesModel = getModel('branches');
|
||||
const vehiclesModel = getModel('vehicles');
|
||||
const loadsModel = getModel('loads');
|
||||
@@ -46,21 +47,60 @@ function getAndFilterList( query ){
|
||||
return filter_list;
|
||||
}
|
||||
|
||||
async function getListByType( type , req ){
|
||||
const filter = { "company_type" : type , "is_hidden" : false };
|
||||
const select = [
|
||||
"rfc",
|
||||
"company_name",
|
||||
"company_type",
|
||||
"company_code",
|
||||
"company_city",
|
||||
"company_state",
|
||||
"createdAt",
|
||||
"membership",
|
||||
"categories",
|
||||
"truck_type",
|
||||
"company_description"
|
||||
];
|
||||
const companySelectField =
|
||||
[
|
||||
"rfc",
|
||||
"company_name",
|
||||
"company_type",
|
||||
"company_code",
|
||||
"company_city",
|
||||
"company_state",
|
||||
"createdAt",
|
||||
"membership",
|
||||
"categories",
|
||||
"truck_type",
|
||||
"company_description",
|
||||
"privacy"
|
||||
];
|
||||
async function getCompanyIdListFromGroups( companyId ){
|
||||
const privateGroups = await companyGroupsModel.find({
|
||||
allowedCompanies: { $in: [companyId] }
|
||||
});
|
||||
|
||||
if( !privateGroups ){
|
||||
return null;
|
||||
}
|
||||
|
||||
const companiesIds = privateGroups.map((group) => group.owner);
|
||||
|
||||
return companiesIds;
|
||||
}
|
||||
|
||||
async function getListByType( companyId, type , req ){
|
||||
const { privacy } = req.query;
|
||||
const privacyVal = ( privacy && ( privacy >= 1 || privacy.toLowerCase() === 'true' ))? true: false;
|
||||
|
||||
let filter;
|
||||
if( privacyVal ){
|
||||
const companiesIds = await getCompanyIdListFromGroups( companyId ) || [];
|
||||
filter = {
|
||||
_id : { $in : companiesIds },
|
||||
company_type: type,
|
||||
is_hidden: false,
|
||||
privacy: true,
|
||||
}
|
||||
}else{
|
||||
filter = {
|
||||
company_type: type,
|
||||
is_hidden: false,
|
||||
$or : [
|
||||
{ privacy : false },
|
||||
{ privacy : { $exists : false } }
|
||||
]
|
||||
};
|
||||
}
|
||||
|
||||
const select = companySelectField;
|
||||
const { $sort } = req.query;
|
||||
const { elements , page } = getPagination( req.query );
|
||||
let query_elements;
|
||||
@@ -143,7 +183,8 @@ async function getCompanyById( req , res ) {
|
||||
|
||||
async function getListShippers( req , res ) {
|
||||
try{
|
||||
const retVal = await getListByType( "Shipper" , req );
|
||||
const companyId = req.context.companyId;
|
||||
const retVal = await getListByType( companyId, "Shipper" , req );
|
||||
res.send( retVal );
|
||||
}catch( error ){
|
||||
console.error( error );
|
||||
@@ -153,7 +194,8 @@ async function getListShippers( req , res ) {
|
||||
|
||||
async function getListCarriers( req , res ) {
|
||||
try{
|
||||
const retVal = await getListByType( "Carrier" , req );
|
||||
const companyId = req.context.companyId;
|
||||
const retVal = await getListByType( companyId, "Carrier" , req );
|
||||
res.send( retVal );
|
||||
}catch( error ){
|
||||
console.error( error );
|
||||
|
||||
Reference in New Issue
Block a user