feat: Adding Budgets endpoint and missing methods for loads

This commit is contained in:
Josepablo C
2024-03-27 23:40:17 -06:00
parent f6edb0ea9a
commit 1e14e05a54
7 changed files with 227 additions and 12 deletions

View File

@@ -0,0 +1,12 @@
'use strict';
const router = require('express').Router();
const services= require('./services.js');
router.get('/find', services.findList);
router.post('/new', services.postBudget);
router.patch('/:id', services.patchBudget);
router.delete('/:id', services.deleteBudget);
router.get('/:id', services.getById);
module.exports = router;

View File

@@ -0,0 +1,178 @@
"use strict";
const { ROOT_PATH, LIB_PATH, MODELS_PATH, HANDLERS_PATH } = process.env;
const { getModel } = require( '../../../lib/Models' );
const { getPagination } = require( `${ROOT_PATH}/${LIB_PATH}/Misc.js` );
const { GenericHandler } = require( `${ROOT_PATH}/${HANDLERS_PATH}/Generic.handler.js` );
const Model = getModel('budgets');
const populate_list = ['company'];
const generic = new GenericHandler( Model, null, populate_list );
function getAndFilterList( query ){
const filter_list = [];
const {
client,
material,
origin,
destination,
truck_type,
num_tons,
price_per_ton,
tonnage,
pickup_distance,
delivery_distance,
warehouse_distance,
total_km_travel,
cost_per_liter,
fuel_price_per_liter,
other_fuel_expenses,
total_fuel_consumed,
total_cost_fuel,
driver_salary,
accomadation_allowance,
other_administrative_expenses,
total_before_tax,
total_utility_per_km,
total_profit,
profit_percentage,
total_administrative_expenses,
} = query;
if( client ) { filter_list.push({ client }); }
if( material ) { filter_list.push({ material }); }
if( origin ) { filter_list.push({ origin }); }
if( destination ) { filter_list.push({ destination }); }
if( truck_type ) { filter_list.push({ truck_type }); }
if( num_tons ) { filter_list.push({ num_tons }); }
if( price_per_ton ) { filter_list.push({ price_per_ton }); }
if( tonnage ) { filter_list.push({ tonnage }); }
if( pickup_distance ) { filter_list.push({ pickup_distance }); }
if( delivery_distance ) { filter_list.push({ delivery_distance }); }
if( warehouse_distance ) { filter_list.push({ warehouse_distance }); }
if( total_km_travel ) { filter_list.push({ total_km_travel }); }
if( cost_per_liter ) { filter_list.push({ cost_per_liter }); }
if( fuel_price_per_liter ) { filter_list.push({ fuel_price_per_liter }); }
if( other_fuel_expenses ) { filter_list.push({ other_fuel_expenses }); }
if( total_fuel_consumed ) { filter_list.push({ total_fuel_consumed }); }
if( total_cost_fuel ) { filter_list.push({ total_cost_fuel }); }
if( driver_salary ) { filter_list.push({ driver_salary }); }
if( accomadation_allowance ) { filter_list.push({ accomadation_allowance }); }
if( other_administrative_expenses ) { filter_list.push({ other_administrative_expenses }); }
if( total_before_tax ) { filter_list.push({ total_before_tax }); }
if( total_utility_per_km ) { filter_list.push({ total_utility_per_km }); }
if( total_profit ) { filter_list.push({ total_profit }); }
if( profit_percentage ) { filter_list.push({ profit_percentage }); }
if( total_administrative_expenses ) { filter_list.push({ total_administrative_expenses }); }
if( filter_list.length == 0 ){
return null;
}
return filter_list;
}
async function findElements( companyId , query ){
const { page, elements } = getPagination( query );
const andFilterList = getAndFilterList( query );
let filter;
if( andFilterList ){
andFilterList.push({ company : companyId });
filter = { $and : andFilterList };
}else{
filter = { company : companyId };
}
const { total , limit, skip, data } = await generic.getList( page , elements, filter );
return {
total,
limit,
skip,
data:data
};
}
async function findElementById( elementId , companyId ){
const filter = {
$and : [
{ _id : elementId },
{ company : companyId }
]
};
let retVal = await Model.findOne( filter ).populate( populate_list ) || {};
return retVal;
}
const findList = async(req, res) => {
try{
const query = req.query || {};
const companyId = req.context.companyId;
const retVal = await findElements( companyId , query );
res.send( retVal );
}catch(error){
console.error( error );
return res.status( 500 ).send({ error });
}
};
const getById = async(req, res) => {
try{
const companyId = req.context.companyId;
const elementId = req.params.id;
res.send( await findElementById( elementId , companyId ) );
}catch(error){
console.error( error );
return res.status( 500 ).send({ error });
}
};
const patchBudget = async(req, res) => {
try{
const elementId = req.params.id;
const budget = await Model.findById( elementId );
const data = req.body;
if( !budget ){
throw "You can't modify this budget";
}
if( !data ){
throw "budget data not sent";
}
await Model.findByIdAndUpdate( elementId , data );
return res.send( await Model.findById( elementId ) );
}catch(error){
console.error( error );
return res.status( 500 ).send({ error });
}
};
const postBudget = async(req, res) => {
try{
const companyId = req.context.companyId;
const data = req.body;
if( !data ){
throw "budget data not sent";
}
data.company = companyId;
const budget = new Model( data );
await budget.save();
return res.send( budget );
}catch(error){
console.error( error );
return res.status( 500 ).send({ error });
}
};
const deleteBudget = async(req, res) => {
try{
const elementId = req.params.id;
const budget = await Model.findById( elementId );
if(!budget){
throw "You can't delete this budget";
}
await Model.findByIdAndDelete( elementId );
return res.send(budget);
}catch(error){
console.error( error );
return res.status( 500 ).send({ error });
}
};
module.exports = { findList, getById, patchBudget, postBudget, deleteBudget };

View File

@@ -7,6 +7,7 @@ const jwtValidator = require( `${ROOT_PATH}/${LIB_PATH}/jwtValidator.js` );
const context = require( './lib/context' ); const context = require( './lib/context' );
const account = require('./account/routes.js'); const account = require('./account/routes.js');
const budgets = require('./budgets/routes.js')
const branches = require('./branches/routes.js'); const branches = require('./branches/routes.js');
const companies = require('./companies/routes.js'); const companies = require('./companies/routes.js');
const loadAttachments = require('./load-attachments/routes.js'); const loadAttachments = require('./load-attachments/routes.js');
@@ -19,6 +20,7 @@ router.use( jwtValidator.middleware );
router.use( context.middleware ); router.use( context.middleware );
router.use('/account', account); router.use('/account', account);
router.use('/budgets', budgets);
router.use('/branches', branches); router.use('/branches', branches);
router.use('/companies', companies); router.use('/companies', companies);
router.use('/load-attachments', loadAttachments ); router.use('/load-attachments', loadAttachments );
@@ -32,7 +34,6 @@ router.use('/orders', test);
router.use('/mailer', test); router.use('/mailer', test);
router.use('/memberships', test); router.use('/memberships', test);
router.use('/bootresolvers', test); router.use('/bootresolvers', test);
router.use('/budgets', test);
router.use('/news', test); router.use('/news', test);
router.use('/branches', test); router.use('/branches', test);
router.use('/trackings', test); router.use('/trackings', test);

View File

@@ -90,8 +90,17 @@ const getById = async(req, res) => {
const patchProposal = async(req, res) => { const patchProposal = async(req, res) => {
try{ try{
console.log( req.body ); const elementId = req.params.id;
return res.status( 500 ).send({ error : "Not implemented yet" }); const proposal = await Model.findById( elementId );
const data = req.body;
if( !proposal ){
throw "You can't modify this proposal";
}
if( !data ){
throw "proposal data not sent";
}
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 });
@@ -100,8 +109,13 @@ const patchProposal = async(req, res) => {
const postProposal = async(req, res) => { const postProposal = async(req, res) => {
try{ try{
console.log( req.body ); const data = req.body;
return res.status( 500 ).send({ error : "Not implemented yet" }); if( !data ){
throw "proposal data not sent";
}
const proposal = new Model( data );
await proposal.save();
return res.send( proposal );
}catch(error){ }catch(error){
console.error( error ); console.error( error );
return res.status( 500 ).send({ error }); return res.status( 500 ).send({ error });
@@ -110,7 +124,13 @@ const postProposal = async(req, res) => {
const deleteProposal = async(req, res) => { const deleteProposal = async(req, res) => {
try{ try{
return res.status( 500 ).send({ error : "Not implemented yet" }); const elementId = req.params.id;
const proposal = await Model.findById( elementId );
if(!proposal){
throw "You can't delete this proposal";
}
await Model.findByIdAndDelete( elementId );
return res.send(proposal);
}catch(error){ }catch(error){
console.error( error ); console.error( error );
return res.status( 500 ).send({ error }); return res.status( 500 ).send({ error });

View File

@@ -38,11 +38,15 @@ const getProfileData = async(req, res) => {
const patchProfileData = async(req, res) => { const patchProfileData = async(req, res) => {
try{ try{
const data = req.body;
if( (data.email) && (data.email === req.context.user.email ) ){
delete data.email;
}
if( req.body.job_role ){ if( req.body.job_role ){
/// You can't change your own role /// You can't change your own role
delete req.body.job_role; delete data.job_role;
} }
const user = await patchUserData( req.context.user.id , req.body ); const user = await patchUserData( req.context.user.id , data );
res.send( user ); res.send( user );
}catch( error ){ }catch( error ){
console.error( error ); console.error( error );

View File

@@ -16,9 +16,9 @@
} }
}, },
"version" : { "version" : {
"version" : "1.0.14", "version" : "1.1.0",
"name": "ETA Beta", "name": "ETA Beta",
"date":"10/2023" "date":"03/2024"
}, },
"S3" : { "S3" : {
"accessKeyId": "AKIAXTQEUF6MLCHTUIKW", "accessKeyId": "AKIAXTQEUF6MLCHTUIKW",

View File

@@ -16,9 +16,9 @@
} }
}, },
"version" : { "version" : {
"version" : "1.0.14", "version" : "1.1.0",
"name": "ETA Beta", "name": "ETA Beta",
"date":"10/2023" "date":"03/2024"
}, },
"S3" : { "S3" : {
"accessKeyId": "AKIAXTQEUF6MLCHTUIKW", "accessKeyId": "AKIAXTQEUF6MLCHTUIKW",