fix(loads/branches): enable endpoints
This commit is contained in:
@@ -3,10 +3,10 @@ const router = require('express').Router();
|
|||||||
const services= require('./services.js');
|
const services= require('./services.js');
|
||||||
|
|
||||||
router.get('/find', services.findList);
|
router.get('/find', services.findList);
|
||||||
router.post('/new', services.postLoad);
|
router.post('/new', services.postBranch);
|
||||||
|
|
||||||
router.patch('/:id', services.patchLoad);
|
router.patch('/:id', services.patchBranch);
|
||||||
router.delete('/:id', services.deleteLoad);
|
router.delete('/:id', services.deleteBranch);
|
||||||
router.get('/:id', services.getById);
|
router.get('/:id', services.getById);
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|||||||
@@ -90,33 +90,58 @@ const getById = async(req, res) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const patchLoad = async(req, res) => {
|
const patchBranch = async(req, res) => {
|
||||||
try{
|
try{
|
||||||
console.log( req.body );
|
const companyId = req.context.companyId;
|
||||||
return res.status( 500 ).send({ error : "Not implemented yet" });
|
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){
|
}catch(error){
|
||||||
console.error( error );
|
console.error( error );
|
||||||
return res.status( 500 ).send({ error });
|
return res.status( 500 ).send({ error });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const postLoad = async(req, res) => {
|
const postBranch = async(req, res) => {
|
||||||
try{
|
try{
|
||||||
console.log( req.body );
|
const companyId = req.context.companyId;
|
||||||
return res.status( 500 ).send({ error : "Not implemented yet" });
|
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){
|
}catch(error){
|
||||||
console.error( error );
|
console.error( error );
|
||||||
return res.status( 500 ).send({ error });
|
return res.status( 500 ).send({ error });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const deleteLoad = async(req, res) => {
|
const deleteBranch = async(req, res) => {
|
||||||
try{
|
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){
|
}catch(error){
|
||||||
console.error( error );
|
console.error( error );
|
||||||
return res.status( 500 ).send({ error });
|
return res.status( 500 ).send({ error });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = { findList, getById, patchLoad, postLoad, deleteLoad };
|
module.exports = { findList, getById, patchBranch, postBranch, deleteBranch };
|
||||||
|
|||||||
@@ -60,10 +60,10 @@ async function findLoads( companyId , query ){
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async function findLoadById( loadId , companyId ){
|
async function findElementById( elementId , companyId ){
|
||||||
const filter = {
|
const filter = {
|
||||||
$and : [
|
$and : [
|
||||||
{ _id : loadId },
|
{ _id : elementId },
|
||||||
{
|
{
|
||||||
$or : [
|
$or : [
|
||||||
{ company : companyId },
|
{ company : companyId },
|
||||||
@@ -75,7 +75,7 @@ async function findLoadById( loadId , companyId ){
|
|||||||
let retVal = await Model.findOne( filter ).populate( populate_list );
|
let retVal = await Model.findOne( filter ).populate( populate_list );
|
||||||
if( retVal ){
|
if( retVal ){
|
||||||
retVal = retVal.toObject();
|
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;
|
retVal.no_of_proposals = no_of_proposals;
|
||||||
}else{
|
}else{
|
||||||
retVal = {};
|
retVal = {};
|
||||||
@@ -98,8 +98,8 @@ const findList = async(req, res) => {
|
|||||||
const getById = async(req, res) => {
|
const getById = async(req, res) => {
|
||||||
try{
|
try{
|
||||||
const companyId = req.context.companyId;
|
const companyId = req.context.companyId;
|
||||||
const loadId = req.params.id;
|
const elementId = req.params.id;
|
||||||
res.send( await findLoadById( loadId , companyId ) );
|
res.send( await findElementById( elementId , companyId ) );
|
||||||
}catch(error){
|
}catch(error){
|
||||||
console.error( error );
|
console.error( error );
|
||||||
return res.status( 500 ).send({ error });
|
return res.status( 500 ).send({ error });
|
||||||
@@ -109,7 +109,8 @@ const getById = async(req, res) => {
|
|||||||
const patchLoad = async(req, res) => {
|
const patchLoad = async(req, res) => {
|
||||||
try{
|
try{
|
||||||
const companyId = req.context.companyId;
|
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 data = req.body;
|
||||||
const load = await findElementById( elementId , companyId );
|
const load = await findElementById( elementId , companyId );
|
||||||
if( !load ){
|
if( !load ){
|
||||||
@@ -121,6 +122,7 @@ const patchLoad = async(req, res) => {
|
|||||||
if(permissions !== "role_shipper" ){
|
if(permissions !== "role_shipper" ){
|
||||||
throw "You can't modify loads";
|
throw "You can't modify loads";
|
||||||
}
|
}
|
||||||
|
data.company = companyId;
|
||||||
await Model.findByIdAndUpdate( elementId , data );
|
await Model.findByIdAndUpdate( elementId , data );
|
||||||
return res.send( await Model.findById( elementId ) );
|
return res.send( await Model.findById( elementId ) );
|
||||||
}catch(error){
|
}catch(error){
|
||||||
@@ -157,9 +159,9 @@ const postLoad = async(req, res) => {
|
|||||||
const deleteLoad = async(req, res) => {
|
const deleteLoad = async(req, res) => {
|
||||||
try{
|
try{
|
||||||
const companyId = req.context.companyId;
|
const companyId = req.context.companyId;
|
||||||
const loadId = req.params.id;
|
const elementId = req.params.id;
|
||||||
const permissions = req.context.permissions;
|
const permissions = req.context.permissions;
|
||||||
const load = await findLoadById( loadId , companyId );
|
const load = await findElementById( elementId , companyId );
|
||||||
if(!load){
|
if(!load){
|
||||||
throw "You can't delete this load";
|
throw "You can't delete this load";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -121,6 +121,7 @@ const patchVehicle = async(req, res) => {
|
|||||||
if( permissions !== "role_carrier" ){
|
if( permissions !== "role_carrier" ){
|
||||||
throw "You can't modify vehicles";
|
throw "You can't modify vehicles";
|
||||||
}
|
}
|
||||||
|
data.company = companyId;
|
||||||
await Model.findByIdAndUpdate( elementId , data );
|
await Model.findByIdAndUpdate( elementId , data );
|
||||||
return res.send( await Model.findById( elementId ) );
|
return res.send( await Model.findById( elementId ) );
|
||||||
}catch(error){
|
}catch(error){
|
||||||
|
|||||||
Reference in New Issue
Block a user