feat: Adding Loads/Vehicle events to populate data among different
tables fix(proposals): When patch event happens without is_accepted field, this is detected as a rejection event.
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
const { getModel } = require( '../../../lib/Models' );
|
||||
const { getPagination, genKey } = require( '../../../lib/Misc.js' );
|
||||
const { GenericHandler } = require( '../../../lib/Handlers/Generic.handler.js' );
|
||||
const { onPatchEvent } = require('../../../lib/Handlers/Loads.handler');
|
||||
const Model = getModel('loads');
|
||||
const CompanyModel = getModel('companies');
|
||||
const ProposalsModel = getModel('proposals');
|
||||
@@ -274,19 +275,81 @@ const getById = async(req, res) => {
|
||||
}
|
||||
};
|
||||
|
||||
function getDataToModify( data ){
|
||||
/**
|
||||
* Take the only fields from model that
|
||||
* should be modifiable by the client.
|
||||
* The rest are populated on demand by the event handlers.
|
||||
*/
|
||||
let data_fields = {
|
||||
alert_list : null,
|
||||
origin_warehouse : null,
|
||||
destination_warehouse : null,
|
||||
origin: null,
|
||||
origin_geo: null,
|
||||
destination: null,
|
||||
destination_geo: null,
|
||||
categories : null,
|
||||
product : null,
|
||||
truck_type : null,
|
||||
tyre_type : null,
|
||||
weight : null,
|
||||
estimated_cost : null,
|
||||
distance : null,
|
||||
actual_cost : null,
|
||||
status : null,
|
||||
load_status : null,
|
||||
contract_start_date : null,
|
||||
contract_end_date : null,
|
||||
est_loading_date : null,
|
||||
est_unloading_date : null,
|
||||
published_date : null,
|
||||
loaded_date : null,
|
||||
transit_date : null,
|
||||
delivered_date : null,
|
||||
load_status_updated : null,
|
||||
notes : null,
|
||||
payment_term : null,
|
||||
terms_and_conditions : null,
|
||||
};
|
||||
|
||||
let filtered_data = {};
|
||||
|
||||
if( Object.keys( data_fields ).length === 0 ){
|
||||
throw "nothing to change";
|
||||
}
|
||||
|
||||
for ( const [key, value] of Object.entries( data_fields ) ) {
|
||||
if( Object.hasOwn( data, key ) ){
|
||||
filtered_data[ key ] = data[ key ];
|
||||
}
|
||||
}
|
||||
|
||||
if( Object.keys( filtered_data ).length === 0 ){
|
||||
throw "nothing to change";
|
||||
}
|
||||
|
||||
return filtered_data;
|
||||
}
|
||||
const patchLoad = async(req, res) => {
|
||||
try{
|
||||
const elementId = req.params.id;
|
||||
const permissions = req.context.permissions;
|
||||
const data = req.body;
|
||||
const load = await findElementById( elementId );
|
||||
|
||||
if( !load ){
|
||||
throw "You can't modify this load";
|
||||
}
|
||||
if( !data ){
|
||||
|
||||
if( !req.body ){
|
||||
throw "load data not sent";
|
||||
}
|
||||
|
||||
const data = getDataToModify( req.body );
|
||||
|
||||
await Model.findByIdAndUpdate( elementId , data );
|
||||
|
||||
await onPatchEvent( req.context.userId, elementId, data );
|
||||
|
||||
return res.send( await Model.findById( elementId ) );
|
||||
}catch(error){
|
||||
console.error( error );
|
||||
@@ -310,7 +373,7 @@ const postLoad = async(req, res) => {
|
||||
data.company = companyId;
|
||||
data.posted_by = userId;
|
||||
data.name = user_name;
|
||||
const load = new Model( data );
|
||||
const load = new Model( data );/// ToDo, check data content and normalize Dates!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
await load.save();
|
||||
|
||||
const id = "" + load._id;
|
||||
|
||||
@@ -108,19 +108,55 @@ const getById = async(req, res) => {
|
||||
}
|
||||
};
|
||||
|
||||
function getDataToModify( data ){
|
||||
/**
|
||||
* Take the only fields from model that
|
||||
* should be modifiable by the client.
|
||||
* The rest are populated on demand by the event handlers.
|
||||
*/
|
||||
let data_fields = {
|
||||
vehicle : null,
|
||||
comment : null,
|
||||
is_accepted : null,
|
||||
};
|
||||
|
||||
let filtered_data = {};
|
||||
|
||||
if( Object.keys( data_fields ).length === 0 ){
|
||||
throw "nothing to change";
|
||||
}
|
||||
|
||||
for ( const [key, value] of Object.entries( data_fields ) ) {
|
||||
if( Object.hasOwn( data, key ) ){
|
||||
filtered_data[ key ] = data[ key ];
|
||||
}
|
||||
}
|
||||
|
||||
if( Object.keys( filtered_data ).length === 0 ){
|
||||
throw "nothing to change";
|
||||
}
|
||||
|
||||
return filtered_data;
|
||||
}
|
||||
const patchProposal = async(req, res) => {
|
||||
try{
|
||||
const elementId = req.params.id;
|
||||
const proposal = await Model.findById( elementId );
|
||||
const data = req.body;
|
||||
|
||||
if( !proposal ){
|
||||
throw "You can't modify this proposal";
|
||||
}
|
||||
if( !data ){
|
||||
if( !req.body ){
|
||||
throw "proposal data not sent";
|
||||
}
|
||||
|
||||
/// Only get data to apply, filter out the rest
|
||||
const data = getDataToModify( req.body );
|
||||
|
||||
await Model.findByIdAndUpdate( elementId , data );
|
||||
|
||||
await onPatchEvent( req.context.userId, elementId, data );
|
||||
|
||||
return res.send( await Model.findById( elementId ) );
|
||||
}catch(error){
|
||||
console.error( error );
|
||||
@@ -130,13 +166,16 @@ const patchProposal = async(req, res) => {
|
||||
|
||||
const postProposal = async(req, res) => {
|
||||
try{
|
||||
const data = req.body;
|
||||
if( !data ){
|
||||
if( !req.body ){
|
||||
throw "proposal data not sent";
|
||||
}
|
||||
const proposal = new Model( data );
|
||||
await proposal.save();
|
||||
let data = req.body;
|
||||
|
||||
data.bidder = req.context.userId;
|
||||
data.carrier = req.context.companyId;
|
||||
const proposal = new Model( data );
|
||||
|
||||
await proposal.save();
|
||||
await onPostEvent( req.context.userId, proposal.id, data);
|
||||
return res.send( proposal );
|
||||
}catch(error){
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
const { getModel } = require( '../../../lib/Models' );
|
||||
const { getPagination, genKey } = require( '../../../lib/Misc' );
|
||||
const { GenericHandler } = require( '../../../lib/Handlers/Generic.handler' );
|
||||
const { onPatchEvent } = require('../../../lib/Handlers/Vehicles.handler');
|
||||
const Model = getModel('vehicles');
|
||||
const CompanyModel = getModel('companies');
|
||||
|
||||
@@ -139,6 +140,59 @@ const getById = async(req, res) => {
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
function getDataToModify( data ){
|
||||
/**
|
||||
* Take the only fields from model that
|
||||
* should be modifiable by the client.
|
||||
* The rest are populated on demand by the event handlers.
|
||||
*/
|
||||
let data_fields = {
|
||||
vehicle_name: null,
|
||||
vehicle_number: null,
|
||||
circulation_serial_number: null,
|
||||
trailer_plate_1: null,
|
||||
trailer_plate_2: null,
|
||||
truck_type: null,
|
||||
tyre_type: null,
|
||||
city: null,
|
||||
state: null,
|
||||
background_tracking: null,
|
||||
status: null,
|
||||
categories: null,
|
||||
posted_by: null,
|
||||
published_date: null,
|
||||
available_date: null,
|
||||
is_available: null,
|
||||
active_load: null,
|
||||
load_shipper: null,
|
||||
available_in: null,
|
||||
destino: null,
|
||||
driver: null,
|
||||
notes: null,
|
||||
last_location_lat: null,
|
||||
last_location_lng: null,
|
||||
last_location_time: null,
|
||||
};
|
||||
|
||||
let filtered_data = {};
|
||||
|
||||
if( Object.keys( data_fields ).length === 0 ){
|
||||
throw "nothing to change";
|
||||
}
|
||||
|
||||
for ( const [key, value] of Object.entries( data_fields ) ) {
|
||||
if( Object.hasOwn( data, key ) ){
|
||||
filtered_data[ key ] = data[ key ];
|
||||
}
|
||||
}
|
||||
|
||||
if( Object.keys( filtered_data ).length === 0 ){
|
||||
throw "nothing to change";
|
||||
}
|
||||
|
||||
return filtered_data;
|
||||
}
|
||||
const patchVehicle = async(req, res) => {
|
||||
try{
|
||||
const companyId = req.context.companyId;
|
||||
@@ -146,20 +200,27 @@ const patchVehicle = async(req, res) => {
|
||||
const permissions = req.context.permissions;
|
||||
const company = await CompanyModel.findById( companyId );
|
||||
const vehicle = await findElementById( elementId , companyId );
|
||||
const data = req.body;
|
||||
|
||||
if( !vehicle ){
|
||||
throw "You can't modify this vehicle";
|
||||
}
|
||||
if( !data ){
|
||||
if( !req.body ){
|
||||
throw "Vehicle data not sent";
|
||||
}
|
||||
if( permissions !== "role_carrier" ){
|
||||
throw "You can't modify vehicles";
|
||||
}
|
||||
|
||||
/// Only get data to apply, filter out the rest
|
||||
const data = getDataToModify( req.body );
|
||||
|
||||
data.company = companyId;
|
||||
data.company_name = company.company_name;
|
||||
|
||||
await Model.findByIdAndUpdate( elementId , data );
|
||||
|
||||
await onPatchEvent( req.context.userId, elementId, data );
|
||||
|
||||
return res.send( await Model.findById( elementId ) );
|
||||
}catch(error){
|
||||
console.error( error );
|
||||
|
||||
Reference in New Issue
Block a user