Merged in feature/IO-3166-Global-Notifications-Part-2 (pull request #2190)

Feature/IO-3166 Global Notifications Part 2
This commit is contained in:
Dave Richer
2025-03-12 16:06:34 +00:00
4 changed files with 390 additions and 657 deletions

View File

@@ -1,10 +1,13 @@
/** Notification Scenarios
* @description This file contains the scenarios for job notifications.
* @type {string[]}
*/
const notificationScenarios = [
"job-assigned-to-me",
"bill-posted",
"critical-parts-status-changed",
"part-marked-back-ordered",
"new-note-added",
"supplement-imported",
"schedule-dates-changed",
"tasks-updated-created",
"new-media-added-reassigned",
@@ -14,6 +17,7 @@ const notificationScenarios = [
"job-status-change",
"payment-collected-completed",
"alternate-transport-changed"
// "supplement-imported", // Disabled for now
];
export { notificationScenarios };

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
const {
jobAssignedToMeBuilder,
billPostedHandler,
billPostedBuilder,
newNoteAddedBuilder,
scheduledDatesChangedBuilder,
tasksUpdatedCreatedBuilder,
@@ -30,10 +30,12 @@ const { isFunction } = require("lodash");
* - builder {Function}: A function to handle the scenario.
* - onlyTruthyValues {boolean|Array<string>}: Specifies fields that must have truthy values for the scenario to match.
* - filterCallback {Function}: Optional callback (sync or async) to further filter the scenario based on event data (returns boolean).
* - enabled {boolean}: If true, the scenario is active; if false or omitted, the scenario is skipped.
*/
const notificationScenarios = [
{
key: "job-assigned-to-me",
enabled: true,
table: "jobs",
fields: ["employee_prep", "employee_body", "employee_csr", "employee_refinish"],
matchToUserFields: ["employee_prep", "employee_body", "employee_csr", "employee_refinish"],
@@ -41,24 +43,28 @@ const notificationScenarios = [
},
{
key: "bill-posted",
enabled: true,
table: "bills",
builder: billPostedHandler,
builder: billPostedBuilder,
onNew: true
},
{
key: "new-note-added",
enabled: true,
table: "notes",
builder: newNoteAddedBuilder,
onNew: true
},
{
key: "schedule-dates-changed",
enabled: true,
table: "jobs",
fields: ["scheduled_in", "scheduled_completion", "scheduled_delivery"],
builder: scheduledDatesChangedBuilder
},
{
key: "tasks-updated-created",
enabled: true,
table: "tasks",
fields: ["updated_at"],
// onNew: true,
@@ -66,12 +72,14 @@ const notificationScenarios = [
},
{
key: "job-status-change",
enabled: true,
table: "jobs",
fields: ["status"],
builder: jobStatusChangeBuilder
},
{
key: "job-added-to-production",
enabled: true,
table: "jobs",
fields: ["inproduction"],
onlyTruthyValues: ["inproduction"],
@@ -79,36 +87,42 @@ const notificationScenarios = [
},
{
key: "alternate-transport-changed",
enabled: true,
table: "jobs",
fields: ["alt_transport"],
builder: alternateTransportChangedBuilder
},
{
key: "new-time-ticket-posted",
enabled: true,
table: "timetickets",
builder: newTimeTicketPostedBuilder
},
{
key: "intake-delivery-checklist-completed",
enabled: true,
table: "jobs",
fields: ["intakechecklist", "deliverchecklist"],
builder: intakeDeliveryChecklistCompletedBuilder
},
{
key: "payment-collected-completed",
enabled: true,
table: "payments",
onNew: true,
builder: paymentCollectedCompletedBuilder
},
{
// MAKE SURE YOU ARE NOT ON A LMS ENVIRONMENT
// Only works on a non LMS ENV
key: "new-media-added-reassigned",
enabled: true,
table: "documents",
fields: ["jobid"],
builder: newMediaAddedReassignedBuilder
},
{
key: "critical-parts-status-changed",
enabled: true,
table: "joblines",
fields: ["status"],
onlyTruthyValues: ["status"],
@@ -117,6 +131,7 @@ const notificationScenarios = [
},
{
key: "part-marked-back-ordered",
enabled: true,
table: "joblines",
fields: ["status"],
builder: partMarkedBackOrderedBuilder,
@@ -133,12 +148,11 @@ const notificationScenarios = [
}
}
},
// -------------- Difficult ---------------
// Holding off on this one for now
// Holding off on this one for now, spans multiple tables
{
key: "supplement-imported",
enabled: false,
builder: supplementImportedBuilder
// spans multiple tables,
}
];
@@ -159,6 +173,11 @@ const notificationScenarios = [
const getMatchingScenarios = async (eventData, getBodyshopFromRedis) => {
const matches = [];
for (const scenario of notificationScenarios) {
// Check if the scenario is enabled; skip if not explicitly true
if (scenario.enabled !== true) {
continue;
}
// If eventData has a table, then only scenarios with a table property that matches should be considered.
if (eventData.table) {
if (!scenario.table || eventData.table.name !== scenario.table) {

View File

@@ -35,7 +35,6 @@ const scenarioParser = async (req, jobIdField) => {
} = req;
// Step 1: Validate we know what user committed the action that fired the parser
// console.log("Step 1");
const hasuraUserRole = event?.session_variables?.["x-hasura-role"];
const hasuraUserId = event?.session_variables?.["x-hasura-user-id"];
@@ -52,7 +51,6 @@ const scenarioParser = async (req, jobIdField) => {
}
// Step 2: Extract just the jobId using the provided jobIdField
// console.log("Step 2");
let jobId = null;
if (jobIdField) {
@@ -70,7 +68,6 @@ const scenarioParser = async (req, jobIdField) => {
}
// Step 3: Query job watchers associated with the job ID using GraphQL
// console.log("Step 3");
const watcherData = await gqlClient.request(queries.GET_JOB_WATCHERS, {
jobid: jobId
@@ -96,7 +93,6 @@ const scenarioParser = async (req, jobIdField) => {
}
// Step 5: Perform the full event diff now that we know there are watchers
// console.log("Step 5");
const eventData = await eventParser({
newData: event.data.new,
@@ -107,7 +103,6 @@ const scenarioParser = async (req, jobIdField) => {
});
// Step 6: Extract body shop information from the job data
// console.log("Step 6");
const bodyShopId = watcherData?.job?.bodyshop?.id;
const bodyShopName = watcherData?.job?.bodyshop?.shopname;
@@ -122,7 +117,6 @@ const scenarioParser = async (req, jobIdField) => {
}
// Step 7: Identify scenarios that match the event data and job context
// console.log("Step 7");
const matchingScenarios = await getMatchingScenarios(
{
@@ -155,7 +149,6 @@ const scenarioParser = async (req, jobIdField) => {
};
// Step 8: Query notification settings for the job watchers
// console.log("Step 8");
const associationsData = await gqlClient.request(queries.GET_NOTIFICATION_ASSOCIATIONS, {
emails: jobWatchers.map((x) => x.email),
@@ -173,7 +166,6 @@ const scenarioParser = async (req, jobIdField) => {
}
// Step 9: Filter scenario watchers based on their enabled notification methods
// console.log("Step 9");
finalScenarioData.matchingScenarios = finalScenarioData.matchingScenarios.map((scenario) => ({
...scenario,
@@ -213,7 +205,6 @@ const scenarioParser = async (req, jobIdField) => {
}
// Step 10: Build and collect scenarios to dispatch notifications for
// console.log("Step 10");
const scenariosToDispatch = [];
@@ -240,7 +231,6 @@ const scenarioParser = async (req, jobIdField) => {
}
// Step 11: Filter scenario fields to include only those that changed
// console.log("Step 11");
const filteredScenarioFields =
scenario.fields?.filter((field) => eventData.changedFieldNames.includes(field)) || [];
@@ -274,7 +264,6 @@ const scenarioParser = async (req, jobIdField) => {
}
// Step 12: Dispatch email notifications to the email queue
// console.log("Step 12");
const emailsToDispatch = scenariosToDispatch.map((scenario) => scenario?.email);
if (!isEmpty(emailsToDispatch)) {
@@ -287,7 +276,6 @@ const scenarioParser = async (req, jobIdField) => {
}
// Step 13: Dispatch app notifications to the app queue
// console.log("Step 13");
const appsToDispatch = scenariosToDispatch.map((scenario) => scenario?.app);
if (!isEmpty(appsToDispatch)) {