feat(Proposals): Add event to update proposal and loads on acceptance

This commit is contained in:
Josepablo C
2024-04-06 16:39:56 -06:00
parent abe6b2ceff
commit 62a5e8419a
2 changed files with 30 additions and 3 deletions

View File

@@ -1,13 +1,40 @@
'user strict';
const { getModel } = require( '../Models' );
const vehiclesModel = require('../Models/vehicles.model');
const proposalsModel = getModel('proposals');
const loadsModel = getModel('loads');
const usersModel = getModel('users');
const companiesModel = getModel('companies');
/**
* When the proposal is accepted then the load should be updated to have the
* @param {*} id
* @param {*} newProposalData
* @returns
*/
async function onPatchEvent( id , newProposalData ){
console.log( newProposalData );
if( !newProposalData.is_accepted ){
return;
}
const proposal = await proposalsModel.findById( id );
const shipper_user = await usersModel.findById( proposal.accepted_by );
const shipper = await companiesModel.findById( shipper_user.company );
const vehicle = await vehiclesModel.findById( proposal.vehicle );
/// Update Proposal:
/// Adding shipper to proposal
await proposalsModel.findByIdAndUpdate( id , {
shipper : shipper.id
} );
/// Update Load:
/// Add carrier, driver and vehicle
await loadsModel.findByIdAndUpdate( proposal.load, {
carrier : proposal.carrier,
driver : vehicle.driver,
vehicle : proposal.vehicle,
} );
}
module.exports = { onPatchEvent };

View File

@@ -3,8 +3,8 @@ const { Schema } = mongoose;
const schema = new Schema({
load: { type: Schema.Types.ObjectId, ref: 'loads' },
shipper: { type: Schema.Types.ObjectId, ref: 'companies' }, // how offers load
carrier: { type: Schema.Types.ObjectId, ref: 'companies' }, // how transport the load
shipper: { type: Schema.Types.ObjectId, ref: 'companies' }, // who offers load
carrier: { type: Schema.Types.ObjectId, ref: 'companies' }, // who transport the load
vehicle: { type: Schema.Types.ObjectId, ref: 'vehicles' },
bidder: { type: Schema.Types.ObjectId, ref: 'users' },