feat(loads::events): Adding load state events
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
'user strict';
|
||||
const { getModel } = require( '../../../Models' );
|
||||
const ObserversEvents = require('../Observers');
|
||||
|
||||
const loadsModel = getModel('loads');
|
||||
const vehiclesModel = getModel('vehicles');
|
||||
const proposalsModel = getModel('proposals');
|
||||
const productsModel = getModel('products');
|
||||
const companiesModel = getModel('companies');
|
||||
const usersModel = getModel('users');
|
||||
|
||||
/**
|
||||
* When a load is delivered, notify all involved parties
|
||||
@@ -61,6 +65,72 @@ async function onDelivered( userId, elementId ){
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
const carrier = await companiesModel.findById( load.carrier ) || {};
|
||||
const product = await productsModel.findById( load.product ) || {};
|
||||
const vehicle = await vehiclesModel.findById( load.vehicle ) || {};
|
||||
const driver = await usersModel.findById( load.driver ) || {};
|
||||
await ObserversEvents.onLoadDelivered(
|
||||
ObserversEvents.NOTIFICATION_TYPE.CLIENT,
|
||||
{ load, carrier, product, vehicle, driver }
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = { onDelivered };
|
||||
/**
|
||||
* When a load is delivered, notify all involved parties
|
||||
* @param {*} userId -> Responsible of the change
|
||||
* @param {*} elementId -> Element Affected
|
||||
*/
|
||||
async function onLoading( userId, elementId ){
|
||||
const load = await loadsModel.findById( elementId );
|
||||
|
||||
const carrier = await companiesModel.findById( load.carrier ) || {};
|
||||
const product = await productsModel.findById( load.product ) || {};
|
||||
const vehicle = await vehiclesModel.findById( load.vehicle ) || {};
|
||||
const driver = await usersModel.findById( load.driver ) || {};
|
||||
|
||||
await ObserversEvents.onLoadLoading(
|
||||
ObserversEvents.NOTIFICATION_TYPE.CLIENT,
|
||||
{ load, carrier, product, vehicle, driver }
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* When a load is delivered, notify all involved parties
|
||||
* @param {*} userId -> Responsible of the change
|
||||
* @param {*} elementId -> Element Affected
|
||||
*/
|
||||
async function onTransit( userId, elementId ){
|
||||
const load = await loadsModel.findById( elementId );
|
||||
|
||||
const carrier = await companiesModel.findById( load.carrier ) || {};
|
||||
const product = await productsModel.findById( load.product ) || {};
|
||||
const vehicle = await vehiclesModel.findById( load.vehicle ) || {};
|
||||
const driver = await usersModel.findById( load.driver ) || {};
|
||||
|
||||
await ObserversEvents.onLoadInTransit(
|
||||
ObserversEvents.NOTIFICATION_TYPE.CLIENT,
|
||||
{ load, carrier, product, vehicle, driver }
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* When a load is delivered, notify all involved parties
|
||||
* @param {*} userId -> Responsible of the change
|
||||
* @param {*} elementId -> Element Affected
|
||||
*/
|
||||
async function onDownloading( userId, elementId ){
|
||||
const load = await loadsModel.findById( elementId );
|
||||
|
||||
const carrier = await companiesModel.findById( load.carrier ) || {};
|
||||
const product = await productsModel.findById( load.product ) || {};
|
||||
const vehicle = await vehiclesModel.findById( load.vehicle ) || {};
|
||||
const driver = await usersModel.findById( load.driver ) || {};
|
||||
|
||||
await ObserversEvents.onLoadDownload(
|
||||
ObserversEvents.NOTIFICATION_TYPE.CLIENT,
|
||||
{ load, carrier, product, vehicle, driver }
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = { onDelivered, onLoading, onTransit, onDownloading };
|
||||
|
||||
@@ -37,4 +37,28 @@ async function onProposalVehicleChanged( load, carrier, product, vehicle, driver
|
||||
await sendEmailEvent( EMAIL_EVENTS.CLIENT_PROPOSAL_VEHICLE_CHANGED, load, carrier, product, vehicle, driver );
|
||||
}
|
||||
|
||||
module.exports = { onProposalAccepted, onProposalDriverChanged, onProposalVehicleChanged };
|
||||
async function onLoadLoading(){
|
||||
await sendEmailEvent( EMAIL_EVENTS.CLIENT_LOAD_ON_LOADING, load, carrier, product, vehicle, driver );
|
||||
}
|
||||
|
||||
async function onLoadInTransit(){
|
||||
await sendEmailEvent( EMAIL_EVENTS.CLIENT_LOAD_IN_TRANSIT, load, carrier, product, vehicle, driver );
|
||||
}
|
||||
|
||||
async function onLoadDownload(){
|
||||
await sendEmailEvent( EMAIL_EVENTS.CLIENT_LOAD_ON_DOWNLOAD, load, carrier, product, vehicle, driver );
|
||||
}
|
||||
|
||||
async function onLoadDelivered(){
|
||||
await sendEmailEvent( EMAIL_EVENTS.CLIENT_LOAD_DELIVERED, load, carrier, product, vehicle, driver );
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
onProposalAccepted,
|
||||
onProposalDriverChanged,
|
||||
onProposalVehicleChanged,
|
||||
onLoadLoading,
|
||||
onLoadInTransit,
|
||||
onLoadDownload,
|
||||
onLoadDelivered,
|
||||
};
|
||||
|
||||
@@ -37,4 +37,8 @@ async function onProposalVehicleChanged( warehouse, load, carrier, product, vehi
|
||||
await sendEmailEvent( EMAIL_EVENTS.WAREHOUSE_PROPOSAL_VEHICLE_CHANGED, warehouse, load, carrier, product, vehicle, driver );
|
||||
}
|
||||
|
||||
module.exports = { onProposalAccepted, onProposalDriverChanged, onProposalVehicleChanged };
|
||||
module.exports = {
|
||||
onProposalAccepted,
|
||||
onProposalDriverChanged,
|
||||
onProposalVehicleChanged
|
||||
};
|
||||
|
||||
@@ -56,4 +56,62 @@ async function onProposalVehicleChanged( notification_type, content ){
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { NOTIFICATION_TYPE, onProposalAccepted, onProposalDriverChanged, onProposalVehicleChanged };
|
||||
|
||||
async function onLoadLoading( notification_type, content ){
|
||||
const { load, carrier, product, vehicle, driver } = content;
|
||||
|
||||
switch ( notification_type ){
|
||||
case NOTIFICATION_TYPE.CLIENT:
|
||||
case NOTIFICATION_TYPE.BOTH:
|
||||
return await ClientEvents.onLoadLoading( load, carrier, product, vehicle, driver );
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
async function onLoadInTransit( notification_type, content ){
|
||||
const { load, carrier, product, vehicle, driver } = content;
|
||||
|
||||
switch ( notification_type ){
|
||||
case NOTIFICATION_TYPE.CLIENT:
|
||||
case NOTIFICATION_TYPE.BOTH:
|
||||
return await ClientEvents.onLoadInTransit( load, carrier, product, vehicle, driver );
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
async function onLoadDownload( notification_type, content ){
|
||||
const { load, carrier, product, vehicle, driver } = content;
|
||||
|
||||
switch ( notification_type ){
|
||||
case NOTIFICATION_TYPE.CLIENT:
|
||||
case NOTIFICATION_TYPE.BOTH:
|
||||
return await ClientEvents.onLoadDownload( load, carrier, onLoadOnDownload, vehicle, driver );
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
async function onLoadDelivered( notification_type, content ){
|
||||
const { load, carrier, product, vehicle, driver } = content;
|
||||
|
||||
switch ( notification_type ){
|
||||
case NOTIFICATION_TYPE.CLIENT:
|
||||
case NOTIFICATION_TYPE.BOTH:
|
||||
return await ClientEvents.onLoadDelivered( load, carrier, product, vehicle, driver );
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
NOTIFICATION_TYPE,
|
||||
onProposalAccepted,
|
||||
onProposalDriverChanged,
|
||||
onProposalVehicleChanged,
|
||||
onLoadLoading,
|
||||
onLoadInTransit,
|
||||
onLoadDownload,
|
||||
onLoadDelivered,
|
||||
};
|
||||
|
||||
@@ -4,7 +4,7 @@ const { getModel } = require( '../../../Models' );
|
||||
const onAcceptedEvents = require('./onAccepted');
|
||||
const onVehicleChanged = require('./onVehicleChanged');
|
||||
|
||||
const branchesModel = getModel('branches')
|
||||
const branchesModel = getModel('branches');
|
||||
const vehiclesModel = getModel('vehicles');
|
||||
const proposalsModel = getModel('proposals');
|
||||
const loadsModel = getModel('loads');
|
||||
|
||||
@@ -6,6 +6,9 @@ const usersModel = getModel('users');
|
||||
const vehiclesModel = getModel('vehicles');
|
||||
const proposalsModel = getModel('proposals');
|
||||
const loadsModel = getModel('loads');
|
||||
const productsModel = getModel('products');
|
||||
const companiesModel = getModel('companies');
|
||||
const branchesModel = getModel('branches');
|
||||
|
||||
/**
|
||||
* When a vehicle's driver changes, notify all involved parties
|
||||
|
||||
@@ -8,8 +8,20 @@ const LoadsEvents = require( './Events/Loads' );
|
||||
* @param {*} newData -> New Data Applied
|
||||
*/
|
||||
async function onPatchEvent( userId, elementId, newData ){
|
||||
if( newData.load_status === "Delivered" ){
|
||||
LoadsEvents.onDelivered( userId, elementId );
|
||||
const { load_status } = newData;
|
||||
if( load_status ){
|
||||
switch (load_status){
|
||||
case "Delivered":
|
||||
return await LoadsEvents.onDelivered( userId, elementId );
|
||||
case "Loading":
|
||||
return await LoadsEvents.onLoading( userId, elementId );
|
||||
case "Transit":
|
||||
return await LoadsEvents.onTransit( userId, elementId );
|
||||
case "Downloading":
|
||||
return await LoadsEvents.onDownloading( userId, elementId );
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -108,31 +108,44 @@ async function WarehouseProposalVehicleChanged( receiver, content ){
|
||||
|
||||
async function ClientProposalAccepted( receiver, content ){
|
||||
console.log("ClientProposalAccepted email event not yet implemented");
|
||||
|
||||
console.log(`receiver: ${receiver}`);
|
||||
console.log( content );
|
||||
}
|
||||
|
||||
async function ClientProposalDriverChanged( receiver, content ){
|
||||
console.log("ClientProposalDriverChanged email event not yet implemented");
|
||||
console.log(`receiver: ${receiver}`);
|
||||
console.log( content );
|
||||
}
|
||||
|
||||
async function ClientProposalVehicleChanged( receiver, content ){
|
||||
console.log("ClientProposalVehicleChanged email event not yet implemented");
|
||||
console.log(`receiver: ${receiver}`);
|
||||
console.log( content );
|
||||
}
|
||||
|
||||
async function ClientLoadOnLoading( receiver, content ){
|
||||
console.log("ClientLoadOnLoading email event not yet implemented");
|
||||
console.log(`receiver: ${receiver}`);
|
||||
console.log( content );
|
||||
}
|
||||
|
||||
async function ClientLoadInTransit( receiver, content ){
|
||||
console.log("ClientLoadInTransit email event not yet implemented");
|
||||
console.log(`receiver: ${receiver}`);
|
||||
console.log( content );
|
||||
}
|
||||
|
||||
async function ClientLoadOnDownload( receiver, content ){
|
||||
console.log("ClientLoadOnDownload email event not yet implemented");
|
||||
console.log(`receiver: ${receiver}`);
|
||||
console.log( content );
|
||||
}
|
||||
|
||||
async function ClientLoadDelivered( receiver, content ){
|
||||
console.log("ClientLoadDelivered email event not yet implemented");
|
||||
console.log(`receiver: ${receiver}`);
|
||||
console.log( content );
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
Reference in New Issue
Block a user