feature/IO-3096-GlobalNotifications - Cleanup and Package bumps
This commit is contained in:
@@ -2,6 +2,9 @@
|
||||
* @module scenarioParser
|
||||
* @description
|
||||
* This module exports a function that parses an event and triggers notification scenarios based on the event data.
|
||||
* It integrates with event parsing utilities, GraphQL queries, and notification queues to manage the dispatching
|
||||
* of notifications via email and app channels. The function processes event data, identifies relevant scenarios,
|
||||
* queries user notification preferences, and dispatches notifications accordingly.
|
||||
*/
|
||||
|
||||
const eventParser = require("./eventParser");
|
||||
@@ -9,23 +12,28 @@ const { client: gqlClient } = require("../graphql-client/graphql-client");
|
||||
const queries = require("../graphql-client/queries");
|
||||
const { isEmpty, isFunction } = require("lodash");
|
||||
const { getMatchingScenarios } = require("./scenarioMapperr");
|
||||
const consoleDir = require("../utils/consoleDir");
|
||||
const { dispatchEmailsToQueue } = require("./queues/emailQueue");
|
||||
const { dispatchAppsToQueue } = require("./queues/appQueue");
|
||||
|
||||
/**
|
||||
* Parses an event and determines matching scenarios for notifications.
|
||||
* Queries job watchers and notification settings before triggering scenario builders.
|
||||
*
|
||||
* @param {Object} req - The request object containing event data, trigger, table, and logger.
|
||||
* @param {string} jobIdField - The field name used to extract the job ID from the event data.
|
||||
* @returns {Promise<void>} Resolves when the parsing and notification dispatching process is complete.
|
||||
* @throws {Error} If required request fields (event data, trigger, or table) or body shop data are missing.
|
||||
*/
|
||||
const scenarioParser = async (req, jobIdField) => {
|
||||
const { event, trigger, table } = req.body;
|
||||
const { logger } = req;
|
||||
|
||||
// Validate that required fields are present in the request body
|
||||
if (!event?.data || !trigger || !table) {
|
||||
throw new Error("Missing required request fields: event data, trigger, or table.");
|
||||
}
|
||||
|
||||
// Step 1: Parse event data to extract necessary details.
|
||||
// Step 1: Parse the event data to extract details like job ID and changed fields
|
||||
const eventData = await eventParser({
|
||||
newData: event.data.new,
|
||||
oldData: event.data.old,
|
||||
@@ -34,11 +42,12 @@ const scenarioParser = async (req, jobIdField) => {
|
||||
jobIdField
|
||||
});
|
||||
|
||||
// Step 2: Query job watchers for the given job ID.
|
||||
// Step 2: Query job watchers associated with the job ID using GraphQL
|
||||
const watcherData = await gqlClient.request(queries.GET_JOB_WATCHERS, {
|
||||
jobid: eventData.jobId
|
||||
});
|
||||
|
||||
// Transform watcher data into a simplified format with email and employee details
|
||||
const jobWatchers = watcherData?.job_watchers_aggregate?.nodes?.map((watcher) => ({
|
||||
email: watcher.user_email,
|
||||
firstName: watcher?.user?.employee?.first_name,
|
||||
@@ -46,21 +55,23 @@ const scenarioParser = async (req, jobIdField) => {
|
||||
employeeId: watcher?.user?.employee?.id
|
||||
}));
|
||||
|
||||
// Exit early if no job watchers are found for this job
|
||||
if (isEmpty(jobWatchers)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Step 3: Retrieve body shop information from the job.
|
||||
// Step 3: Extract body shop information from the job data
|
||||
const bodyShopId = watcherData?.job?.bodyshop?.id;
|
||||
const bodyShopName = watcherData?.job?.bodyshop?.shopname;
|
||||
const jobRoNumber = watcherData?.job?.ro_number;
|
||||
const jobClaimNumber = watcherData?.job?.clm_no;
|
||||
|
||||
// Validate that body shop data exists, as it’s required for notifications
|
||||
if (!bodyShopId || !bodyShopName) {
|
||||
throw new Error("No bodyshop data found for this job.");
|
||||
}
|
||||
|
||||
// Step 4: Determine matching scenarios based on event data.
|
||||
// Step 4: Identify scenarios that match the event data and job context
|
||||
const matchingScenarios = getMatchingScenarios({
|
||||
...eventData,
|
||||
jobWatchers,
|
||||
@@ -68,10 +79,12 @@ const scenarioParser = async (req, jobIdField) => {
|
||||
bodyShopName
|
||||
});
|
||||
|
||||
// Exit early if no matching scenarios are identified
|
||||
if (isEmpty(matchingScenarios)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Combine event data with additional context for scenario processing
|
||||
const finalScenarioData = {
|
||||
...eventData,
|
||||
jobWatchers,
|
||||
@@ -80,22 +93,24 @@ const scenarioParser = async (req, jobIdField) => {
|
||||
matchingScenarios
|
||||
};
|
||||
|
||||
// Step 5: Query notification settings for job watchers.
|
||||
// Step 5: Query notification settings for the job watchers
|
||||
const associationsData = await gqlClient.request(queries.GET_NOTIFICATION_ASSOCIATIONS, {
|
||||
emails: jobWatchers.map((x) => x.email),
|
||||
shopid: bodyShopId
|
||||
});
|
||||
|
||||
// Exit early if no notification associations are found
|
||||
if (isEmpty(associationsData?.associations)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Step 6: Filter scenario watchers based on enabled notification methods.
|
||||
// Step 6: Filter scenario watchers based on their enabled notification methods
|
||||
finalScenarioData.matchingScenarios = finalScenarioData.matchingScenarios.map((scenario) => ({
|
||||
...scenario,
|
||||
scenarioWatchers: associationsData.associations
|
||||
.filter((assoc) => {
|
||||
const settings = assoc.notification_settings && assoc.notification_settings[scenario.key];
|
||||
// Include only watchers with at least one enabled notification method (app, email, or FCM)
|
||||
return settings && (settings.app || settings.email || settings.fcm);
|
||||
})
|
||||
.map((assoc) => {
|
||||
@@ -103,6 +118,7 @@ const scenarioParser = async (req, jobIdField) => {
|
||||
const watcherEmail = assoc.user || assoc.useremail;
|
||||
const matchingWatcher = jobWatchers.find((watcher) => watcher.email === watcherEmail);
|
||||
|
||||
// Build watcher object with notification preferences and personal details
|
||||
return {
|
||||
user: watcherEmail,
|
||||
email: settings.email,
|
||||
@@ -115,21 +131,23 @@ const scenarioParser = async (req, jobIdField) => {
|
||||
})
|
||||
}));
|
||||
|
||||
// Exit early if no scenarios have eligible watchers after filtering
|
||||
if (isEmpty(finalScenarioData?.matchingScenarios)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Step 7: Trigger scenario builders for matching scenarios with eligible watchers.
|
||||
// Step 7: Build and collect scenarios to dispatch notifications for
|
||||
const scenariosToDispatch = [];
|
||||
|
||||
for (const scenario of finalScenarioData.matchingScenarios) {
|
||||
// Skip if no watchers or no builder function is defined for the scenario
|
||||
if (isEmpty(scenario.scenarioWatchers) || !isFunction(scenario.builder)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let eligibleWatchers = scenario.scenarioWatchers;
|
||||
|
||||
// Ensure watchers are only notified if they are assigned to the changed field.
|
||||
// Filter watchers to only those assigned to changed fields, if specified
|
||||
if (scenario.matchToUserFields && scenario.matchToUserFields.length > 0) {
|
||||
eligibleWatchers = scenario.scenarioWatchers.filter((watcher) =>
|
||||
scenario.matchToUserFields.some(
|
||||
@@ -138,14 +156,16 @@ const scenarioParser = async (req, jobIdField) => {
|
||||
);
|
||||
}
|
||||
|
||||
// Skip if no watchers remain after filtering
|
||||
if (isEmpty(eligibleWatchers)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Step 8: Filter scenario fields to only include changed fields.
|
||||
// Step 8: Filter scenario fields to include only those that changed
|
||||
const filteredScenarioFields =
|
||||
scenario.fields?.filter((field) => eventData.changedFieldNames.includes(field)) || [];
|
||||
|
||||
// Use the scenario’s builder to construct the notification data
|
||||
scenariosToDispatch.push(
|
||||
scenario.builder({
|
||||
trigger: finalScenarioData.trigger.name,
|
||||
@@ -167,30 +187,33 @@ const scenarioParser = async (req, jobIdField) => {
|
||||
);
|
||||
}
|
||||
|
||||
// Exit early if no scenarios are ready to dispatch
|
||||
if (isEmpty(scenariosToDispatch)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Step 9: Dispatch Email Notifications to the Email Notification Queue
|
||||
// Step 9: Dispatch email notifications to the email queue
|
||||
const emailsToDispatch = scenariosToDispatch.map((scenario) => scenario?.email);
|
||||
if (!isEmpty(emailsToDispatch)) {
|
||||
dispatchEmailsToQueue({
|
||||
emailsToDispatch,
|
||||
logger
|
||||
}).catch((e) =>
|
||||
// Log any errors encountered during email dispatching
|
||||
logger.log("Something went wrong dispatching emails to the Email Notification Queue", "error", "queue", null, {
|
||||
message: e?.message
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
// Step 10: Dispatch App Notifications to the App Notification Queue
|
||||
// Step 10: Dispatch app notifications to the app queue
|
||||
const appsToDispatch = scenariosToDispatch.map((scenario) => scenario?.app);
|
||||
if (!isEmpty(appsToDispatch)) {
|
||||
dispatchAppsToQueue({
|
||||
appsToDispatch,
|
||||
logger
|
||||
}).catch((e) =>
|
||||
// Log any errors encountered during app notification dispatching
|
||||
logger.log("Something went wrong dispatching apps to the App Notification Queue", "error", "queue", null, {
|
||||
message: e?.message
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user