fix: Remove role based queries, simplify API

This commit is contained in:
Josepablo C
2024-03-27 23:01:53 -06:00
parent 9e7f38392b
commit f6edb0ea9a
2 changed files with 18 additions and 18 deletions

View File

@@ -62,19 +62,8 @@ async function findLoads( companyId , query ){
};
}
async function findElementById( elementId , companyId ){
const filter = {
$and : [
{ _id : elementId },
{
$or : [
{ company : companyId },
{ carrier : companyId }
]
}
]
};
let retVal = await Model.findOne( filter ).populate( populate_list );
async function findElementById( elementId ){
let retVal = await Model.findById( elementId ).populate( populate_list );
if( retVal ){
retVal = retVal.toObject();
const no_of_proposals = await ProposalsModel.count({ load : elementId });
@@ -99,9 +88,8 @@ const findList = async(req, res) => {
const getById = async(req, res) => {
try{
const companyId = req.context.companyId;
const elementId = req.params.id;
res.send( await findElementById( elementId , companyId ) );
res.send( await findElementById( elementId ) );
}catch(error){
console.error( error );
return res.status( 500 ).send({ error });
@@ -110,11 +98,10 @@ const getById = async(req, res) => {
const patchLoad = async(req, res) => {
try{
const companyId = req.context.companyId;
const elementId = req.params.id;
const permissions = req.context.permissions;
const data = req.body;
const load = await findElementById( elementId , companyId );
const load = await findElementById( elementId );
if( !load ){
throw "You can't modify this load";
}
@@ -124,7 +111,6 @@ const patchLoad = async(req, res) => {
if(permissions !== "role_shipper" ){
throw "You can't modify loads";
}
data.company = companyId;
await Model.findByIdAndUpdate( elementId , data );
return res.send( await Model.findById( elementId ) );
}catch(error){