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:
Josepablo C
2025-07-14 16:37:22 -06:00
parent 494d8cad7f
commit 9ffa97ac0b
10 changed files with 350 additions and 34 deletions

View File

@@ -0,0 +1,60 @@
'user strict';
const { getModel } = require( '../../../Models' );
const loadsModel = getModel('loads');
const vehiclesModel = getModel('vehicles');
const proposalsModel = getModel('proposals');
/**
* When a load is delivered, notify all involved parties
* @param {*} userId -> Responsible of the change
* @param {*} elementId -> Element Affected
*/
async function onDelivered( userId, elementId ){
const load = await loadsModel.findById( elementId );
const proposal_list = await proposalsModel.find({
load: load.id,
is_accepted: true,
is_completed: false
});
const vehicle_list = await vehiclesModel.find({
active_load: load.id
});
const current_date = new Date();
/// Update Load: Remove vehicle and driver reference for data safety.
await loadsModel.findByIdAndUpdate( proposal.load, {
driver: null,
vehicle: null,
status: "Closed",
delivered_date: current_date,
load_status_updated: current_date,
});
/// Update proposals related to this load. Ideally, just one.
// remove vehicle for data safety.
for( const proposal of proposal_list ){
await proposalsModel.findByIdAndUpdate(
proposal.id,
{
is_completed : true,
vehicle : null,
}
);
}
/// Update vehicles related to this load. Ideally, just one.
for( const vehicle of vehicle_list ){
await vehiclesModel.findByIdAndUpdate(
vehicle.id,
{
active_load: null,
load_shipper: null,
status: "Free"
}
);
}
}
module.exports = { onDelivered };

View File

@@ -13,7 +13,9 @@ const notificationsModel = getModel('notifications');
const productsModel = getModel('products');
/**
* When the proposal is created then the load owner should be notified
* When the proposal is created then the load owner should be notified.
* @param {*} userId -> Responsible of the change
* @param {*} proposalId -> Proposal Affected
*/
async function onProposalCreate( userId, proposalId ){
const proposal = await proposalsModel.findById( proposalId );
@@ -32,7 +34,9 @@ async function onProposalCreate( userId, proposalId ){
}
/**
* When a proposal is removed from the load, it is considered as rejected
* When a proposal is removed from the load, it is considered as rejected.
* @param {*} userId -> Responsible of the change
* @param {*} proposalId -> Proposal Affected
*/
async function onProposalRejected( userId, proposalId ){
const proposal = await proposalsModel.findById( proposalId );
@@ -56,7 +60,9 @@ async function onProposalRejected( userId, proposalId ){
}
/**
* When a proposal is accepted by the shipper
* When a proposal is accepted by the shipper.
* @param {*} userId -> Responsible of the change
* @param {*} proposalId -> Proposal Affected
*/
async function onProposalAccepted( userId, proposalId ){
const shipper_user = await usersModel.findById( userId );
@@ -86,4 +92,21 @@ async function onProposalAccepted( userId, proposalId ){
await onAcceptedEvents.sendNotification( proposal, load );
}
module.exports = { onProposalCreate, onProposalRejected, onProposalAccepted };
/**
* When a vehicle changes from the original proposal
* @param {*} userId -> Responsible of the change
* @param {*} proposalId -> Proposal Affected
*/
async function onProposalVehicleChanged( userId, proposalId ){
const proposal = await proposalsModel.findById( proposalId );
const vehicle = await vehiclesModel.findById( proposal.vehicle );
/// Update Load:
/// Add driver and vehicle
await loadsModel.findByIdAndUpdate( proposal.load, {
driver : vehicle.driver,
vehicle : proposal.vehicle
} );
}
module.exports = { onProposalCreate, onProposalRejected, onProposalAccepted, onProposalVehicleChanged };

View File

@@ -0,0 +1,35 @@
'user strict';
const { getModel } = require( '../../../Models' );
const usersModel = getModel('users');
const vehiclesModel = getModel('vehicles');
const proposalsModel = getModel('proposals');
const loadsModel = getModel('loads');
/**
* When a vehicle's driver changes, notify all involved parties
* @param {*} userId -> Responsible of the change
* @param {*} vehicleId -> Proposal Affected
*/
async function onVehicleDriverChanged( userId, vehicleId ){
const vehicle = await vehiclesModel.findById( vehicleId );
const driver = await usersModel.findById( vehicle.driver );
const proposal_list = await proposalsModel.find({
vehicle: vehicleId,
is_accepted: true,
is_completed: false
});
/// Update proposals related to this load. Ideally, just one.
// remove vehicle for data safety.
for( const proposal of proposal_list ){
/// Update Load:
/// Add driver and vehicle
await loadsModel.findByIdAndUpdate( proposal.load, {
driver : driver,
vehicle : vehicle
} );
}
}
module.exports = { onVehicleDriverChanged };

View File

@@ -0,0 +1,16 @@
'user strict';
const LoadsEvents = require( './Events/Loads' );
/**
* When the element is modified
* @param {*} userId -> Responsible of the change
* @param {*} elementId -> Element Affected
* @param {*} newData -> New Data Applied
*/
async function onPatchEvent( userId, elementId, newData ){
if( newData.load_status === "Delivered" ){
LoadsEvents.onDelivered( userId, elementId );
}
}
module.exports = { onPatchEvent };

View File

@@ -1,19 +1,11 @@
'user strict';
const { getModel } = require( '../Models' );
const ProposalsEvents = require( './Events/Proposals' );
const vehiclesModel = getModel('vehicles');
const proposalsModel = getModel('proposals');
const loadsModel = getModel('loads');
const usersModel = getModel('users');
const companiesModel = getModel('companies');
const notificationsModel = getModel('notifications');
/**
* When the proposal is created then the load owner should be notified
* @param {*} id
* @param {*} newProposalData
* @param {*} userId -> Responsible of the change
* @param {*} proposalId -> Proposal Affected
* @param {*} newProposalData -> New Data Applied
* @returns
*/
async function onPostEvent( userId, proposalId ,newProposalData ){
@@ -24,15 +16,25 @@ async function onPostEvent( userId, proposalId ,newProposalData ){
/**
* When the proposal is accepted then the load should be updated to have the latest
* shipper, vehicle, etc.
* @param {*} id
* @param {*} newProposalData
* @param {*} userId -> Responsible of the change
* @param {*} proposalId -> Proposal Affected
* @param {*} newProposalData -> New Data Applied
* @returns
*/
async function onPatchEvent( userId, proposalId , newProposalData ){
if( !newProposalData.is_accepted ){
await ProposalsEvents.onProposalRejected( userId, proposalId );
}else{
await ProposalsEvents.onProposalAccepted( userId, proposalId );
/** When proposal is accepted, then the user is assumed to be a shipper, and the data is populated */
if( Object.hasOwn( newProposalData, "is_accepted" ) ){
if( newProposalData.is_accepted ){
await ProposalsEvents.onProposalAccepted( userId, proposalId );
}else{
await ProposalsEvents.onProposalRejected( userId, proposalId );
}
}
/** When vehicle have changed, notify the change */
if( Object.hasOwn( newProposalData, "vehicle" ) ){
await ProposalsEvents.onProposalVehicleChanged( userId, proposalId );
}
}

View File

@@ -0,0 +1,17 @@
'user strict';
const VehiclesEvents = require( './Events/Vehicles' );
/**
* When the element is modified
* @param {*} userId -> Responsible of the change
* @param {*} elementId -> Element Affected
* @param {*} newData -> New Data Applied
*/
async function onPatchEvent( userId, elementId , newData ){
/** When driver have changed, notify the change */
if( newData.driver ){
await VehiclesEvents.onVehicleDriverChanged( userId, elementId );
}
}
module.exports = { onPatchEvent };