feature/IO-3096-GlobalNotifications - Checkpoint, Builders

This commit is contained in:
Dave Richer
2025-02-18 12:57:54 -05:00
parent c214ed1dfb
commit adb15a4748
7 changed files with 149 additions and 138 deletions

View File

@@ -9,50 +9,22 @@ const { client: gqlClient } = require("../graphql-client/graphql-client");
const queries = require("../graphql-client/queries");
const { isEmpty, isFunction } = require("lodash");
const { getMatchingScenarios } = require("./scenarioMapperr");
const emailQueue = require("./queues/emailQueue");
const consoleDir = require("../utils/consoleDir");
const { dispatchEmailsToQueue } = require("./queues/emailQueue");
/**
* Parses an event and determines matching scenarios for notifications.
* Queries job watchers and notification settings before triggering scenario builders.
*
* <p>This function performs the following steps:
* <ol>
* <li>Parse event data to extract necessary details using {@link eventParser}.</li>
* <li>Query job watchers for the given job ID using a GraphQL client.</li>
* <li>Retrieve body shop information from the job.</li>
* <li>Determine matching scenarios based on event data.</li>
* <li>Query notification settings for job watchers.</li>
* <li>Filter scenario watchers based on enabled notification methods.</li>
* <li>Trigger scenario builders for matching scenarios with eligible watchers.</li>
* </ol>
*
* @async
* @function scenarioParser
* @param {Object} req - The request object containing event data.
* Expected properties:
* <pre>
* {
* body: {
* event: { data: { new: Object, old: Object } },
* trigger: Object,
* table: string
* }
* }
* </pre>
* @param {string} jobIdField - The field used to identify the job ID.
* @returns {Promise<void>} A promise that resolves when the scenarios have been processed.
* @throws {Error} Throws an error if required request fields are missing or if body shop data is not found.
*/
const scenarioParser = async (req, jobIdField) => {
const { event, trigger, table } = req.body;
const { logger } = req;
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.
// console.log(`1`);
const eventData = await eventParser({
newData: event.data.new,
oldData: event.data.old,
@@ -62,7 +34,6 @@ const scenarioParser = async (req, jobIdField) => {
});
// Step 2: Query job watchers for the given job ID.
// console.log(`2`);
const watcherData = await gqlClient.request(queries.GET_JOB_WATCHERS, {
jobid: eventData.jobId
});
@@ -79,7 +50,6 @@ const scenarioParser = async (req, jobIdField) => {
}
// Step 3: Retrieve body shop information from the job.
// console.log(`3`);
const bodyShopId = watcherData?.job?.bodyshop?.id;
const bodyShopName = watcherData?.job?.bodyshop?.shopname;
const jobRoNumber = watcherData?.job?.ro_number;
@@ -90,7 +60,6 @@ const scenarioParser = async (req, jobIdField) => {
}
// Step 4: Determine matching scenarios based on event data.
// console.log(`4`);
const matchingScenarios = getMatchingScenarios({
...eventData,
jobWatchers,
@@ -111,7 +80,6 @@ const scenarioParser = async (req, jobIdField) => {
};
// Step 5: Query notification settings for job watchers.
// console.log(`5`);
const associationsData = await gqlClient.request(queries.GET_NOTIFICATION_ASSOCIATIONS, {
emails: jobWatchers.map((x) => x.email),
shopid: bodyShopId
@@ -122,7 +90,6 @@ const scenarioParser = async (req, jobIdField) => {
}
// Step 6: Filter scenario watchers based on enabled notification methods.
// console.log(`6`);
finalScenarioData.matchingScenarios = finalScenarioData.matchingScenarios.map((scenario) => ({
...scenario,
scenarioWatchers: associationsData.associations
@@ -152,8 +119,6 @@ const scenarioParser = async (req, jobIdField) => {
}
// Step 7: Trigger scenario builders for matching scenarios with eligible watchers.
// console.log(`7`);
const scenariosToDispatch = [];
for (const scenario of finalScenarioData.matchingScenarios) {
@@ -177,8 +142,6 @@ const scenarioParser = async (req, jobIdField) => {
}
// Step 8: Filter scenario fields to only include changed fields.
// console.log(`8`);
const filteredScenarioFields =
scenario.fields?.filter((field) => eventData.changedFieldNames.includes(field)) || [];
@@ -208,16 +171,18 @@ const scenarioParser = async (req, jobIdField) => {
}
// Step 9: Dispatch Email Notifications to the Email Notification Queue
// console.log(`8`);
const emailsToDispatch = scenariosToDispatch.map((scenario) => scenario?.email);
dispatchEmailsToQueue({
emailsToDispatch: scenariosToDispatch.map((scenario) => scenario?.email),
logger
}).catch((e) =>
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
const appsToDispatch = scenariosToDispatch.map((scenario) => scenario?.app);
consoleDir({ emailsToDispatch, appsToDispatch });
// TODO: Test Code for Queues
// emailQueue().add("test", { data: "test" });
consoleDir({ appsToDispatch });
};
module.exports = scenarioParser;