diff --git a/src/apps/private/branches/routes.js b/src/apps/private/branches/routes.js index f4ddf71..310ebce 100644 --- a/src/apps/private/branches/routes.js +++ b/src/apps/private/branches/routes.js @@ -3,10 +3,10 @@ const router = require('express').Router(); const services= require('./services.js'); router.get('/find', services.findList); -router.post('/new', services.postLoad); +router.post('/new', services.postBranch); -router.patch('/:id', services.patchLoad); -router.delete('/:id', services.deleteLoad); +router.patch('/:id', services.patchBranch); +router.delete('/:id', services.deleteBranch); router.get('/:id', services.getById); module.exports = router; diff --git a/src/apps/private/branches/services.js b/src/apps/private/branches/services.js index 2ac6832..1f9d177 100644 --- a/src/apps/private/branches/services.js +++ b/src/apps/private/branches/services.js @@ -90,33 +90,58 @@ const getById = async(req, res) => { } }; -const patchLoad = async(req, res) => { +const patchBranch = async(req, res) => { try{ - console.log( req.body ); - return res.status( 500 ).send({ error : "Not implemented yet" }); + const companyId = req.context.companyId; + const elementId = req.params.id; + const data = req.body; + const branch = await findElementById( elementId , companyId ); + if( !branch ){ + throw "You can't modify this branch"; + } + if( !data ){ + throw "load data not sent"; + } + data.company = companyId; + await Model.findByIdAndUpdate( elementId , data ); + return res.send( await Model.findById( elementId ) ); }catch(error){ console.error( error ); return res.status( 500 ).send({ error }); } }; -const postLoad = async(req, res) => { +const postBranch = async(req, res) => { try{ - console.log( req.body ); - return res.status( 500 ).send({ error : "Not implemented yet" }); + const companyId = req.context.companyId; + const data = req.body; + if( !data ){ + throw "Branch data not sent"; + } + data.company = companyId; + const branch = new Model( data ); + await branch.save(); + return res.send( branch ); }catch(error){ console.error( error ); return res.status( 500 ).send({ error }); } }; -const deleteLoad = async(req, res) => { +const deleteBranch = async(req, res) => { try{ - return res.status( 500 ).send({ error : "Not implemented yet" }); + const companyId = req.context.companyId; + const elementId = req.params.id; + const element = await findElementById( elementId , companyId ); + if(!element){ + throw "You can't delete this branch"; + } + await Model.findByIdAndDelete( elementId ); + return res.send(element); }catch(error){ console.error( error ); return res.status( 500 ).send({ error }); } }; -module.exports = { findList, getById, patchLoad, postLoad, deleteLoad }; +module.exports = { findList, getById, patchBranch, postBranch, deleteBranch }; diff --git a/src/apps/private/loads/services.js b/src/apps/private/loads/services.js index 7112a0d..c883b75 100644 --- a/src/apps/private/loads/services.js +++ b/src/apps/private/loads/services.js @@ -60,10 +60,10 @@ async function findLoads( companyId , query ){ }; } -async function findLoadById( loadId , companyId ){ +async function findElementById( elementId , companyId ){ const filter = { $and : [ - { _id : loadId }, + { _id : elementId }, { $or : [ { company : companyId }, @@ -75,7 +75,7 @@ async function findLoadById( loadId , companyId ){ let retVal = await Model.findOne( filter ).populate( populate_list ); if( retVal ){ retVal = retVal.toObject(); - const no_of_proposals = await ProposalsModel.count({ load : loadId }); + const no_of_proposals = await ProposalsModel.count({ load : elementId }); retVal.no_of_proposals = no_of_proposals; }else{ retVal = {}; @@ -98,8 +98,8 @@ const findList = async(req, res) => { const getById = async(req, res) => { try{ const companyId = req.context.companyId; - const loadId = req.params.id; - res.send( await findLoadById( loadId , companyId ) ); + const elementId = req.params.id; + res.send( await findElementById( elementId , companyId ) ); }catch(error){ console.error( error ); return res.status( 500 ).send({ error }); @@ -109,7 +109,8 @@ const getById = async(req, res) => { const patchLoad = async(req, res) => { try{ const companyId = req.context.companyId; - const loadId = req.params.id; + const elementId = req.params.id; + const permissions = req.context.permissions; const data = req.body; const load = await findElementById( elementId , companyId ); if( !load ){ @@ -121,6 +122,7 @@ 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){ @@ -157,9 +159,9 @@ const postLoad = async(req, res) => { const deleteLoad = async(req, res) => { try{ const companyId = req.context.companyId; - const loadId = req.params.id; + const elementId = req.params.id; const permissions = req.context.permissions; - const load = await findLoadById( loadId , companyId ); + const load = await findElementById( elementId , companyId ); if(!load){ throw "You can't delete this load"; } diff --git a/src/apps/private/vehicles/services.js b/src/apps/private/vehicles/services.js index b19f0b3..9386626 100644 --- a/src/apps/private/vehicles/services.js +++ b/src/apps/private/vehicles/services.js @@ -121,6 +121,7 @@ const patchVehicle = async(req, res) => { if( permissions !== "role_carrier" ){ throw "You can't modify vehicles"; } + data.company = companyId; await Model.findByIdAndUpdate( elementId , data ); return res.send( await Model.findById( elementId ) ); }catch(error){