fix(loads/proposals): when proposal changes vehicle, active load is removed from vehicle
fix(loads): Populate vehicle.categories + vehicle.notes in find request
This commit is contained in:
@@ -101,12 +101,28 @@ async function onProposalVehicleChanged( userId, proposalId ){
|
||||
const proposal = await proposalsModel.findById( proposalId );
|
||||
const vehicle = await vehiclesModel.findById( proposal.vehicle );
|
||||
|
||||
const vehicle_list = await vehiclesModel.find({
|
||||
active_load: proposal.load
|
||||
});
|
||||
|
||||
/// Update Load:
|
||||
/// Add driver and vehicle
|
||||
await loadsModel.findByIdAndUpdate( proposal.load, {
|
||||
driver : vehicle.driver,
|
||||
vehicle : proposal.vehicle
|
||||
} );
|
||||
|
||||
/// Update vehicles related to this load. Ideally, just one.
|
||||
for( const item of vehicle_list ){
|
||||
await vehiclesModel.findByIdAndUpdate(
|
||||
item.id,
|
||||
{
|
||||
active_load: null,
|
||||
load_shipper: null,
|
||||
status: "Free"
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { onProposalCreate, onProposalRejected, onProposalAccepted, onProposalVehicleChanged };
|
||||
|
||||
Reference in New Issue
Block a user