feature/IO-3096-GlobalNotifications - Check-point

This commit is contained in:
Dave Richer
2025-02-10 15:19:41 -05:00
parent ba2d03176f
commit b1ffbe0e12
8 changed files with 262 additions and 70 deletions

View File

@@ -1,4 +1,4 @@
const changeParser = async ({ oldData, newData, trigger, table }) => {
const eventParser = async ({ oldData, newData, trigger, table, jobIdField }) => {
const isNew = !oldData;
let changedFields = {};
let changedFieldNames = [];
@@ -29,6 +29,19 @@ const changeParser = async ({ oldData, newData, trigger, table }) => {
}
}
}
// Extract jobId based on jobIdField
let jobId = null;
if (jobIdField) {
// If the jobIdField is provided as a string like "req.body.event.new.jobid",
// strip the prefix if it exists so we can use the property name.
let keyName = jobIdField;
const prefix = "req.body.event.new.";
if (keyName.startsWith(prefix)) {
keyName = keyName.slice(prefix.length);
}
// Attempt to retrieve the job id from newData first; if not available, try oldData.
jobId = newData[keyName] || (oldData && oldData[keyName]) || null;
}
return {
changedFieldNames,
@@ -36,8 +49,9 @@ const changeParser = async ({ oldData, newData, trigger, table }) => {
isNew,
data: newData,
trigger,
table
table,
jobId
};
};
module.exports = changeParser;
module.exports = eventParser;

View File

@@ -1,52 +1,83 @@
const tasksUpdatedCreatedBuilder = require("../scenarioBuilders/tasksUpdatedCreatedBuilder");
// Key: scenario name
// Table: table name to check for changes
// Fields: fields to check for changes
// OnNew: whether the scenario should be triggered on new data
// Builder: function to handle the scenario
const notificationScenarios = [
{ key: "job-assigned-to-me", table: "jobs" },
{ key: "bill-posted", table: "bills" },
{ key: "critical-parts-status-changed" },
{ key: "part-marked-back-ordered" },
{ key: "new-note-added", table: "notes" },
{ key: "supplement-imported" },
{ key: "schedule-dates-changed", table: "jobs" },
{ key: "new-note-added", table: "notes", onNew: true },
{
key: "schedule-dates-changed",
table: "jobs",
fields: ["scheduled_in", "scheduled_completion", "scheduled_delivery"]
},
{
key: "tasks-updated-created",
table: "tasks",
fields: ["updated_at"],
onNew: false,
// onNew: true,
builder: tasksUpdatedCreatedBuilder
},
{ key: "job-added-to-production", table: "jobs", fields: ["introduction"] },
{ key: "job-status-change", table: "jobs", fields: ["status"] },
{ key: "alternate-transport-changed", table: "jobs", fields: ["alt_transport"] },
{ key: "payment-collected-completed" },
{ key: "new-media-added-reassigned" },
{ key: "new-time-ticket-posted" },
{ key: "intake-delivery-checklist-completed" },
{ key: "job-added-to-production", table: "jobs" },
{ key: "job-status-change", table: "jobs" },
{ key: "payment-collected-completed" },
{ key: "alternate-transport-changed" }
{ key: "supplement-imported" },
{ key: "critical-parts-status-changed" },
{ key: "part-marked-back-ordered" }
];
// Helper function to find a scenario based on multiple criteria
function hasScenarios({ table, keys, onNew }) {
return (
notificationScenarios.find((scenario) => {
// Check if table matches if provided
if (table && scenario.table !== table) return false;
/**
* Returns an array of scenarios that match the given event data.
*
* @param {Object} eventData - The parsed event data.
* Expected properties:
* - table: an object with a `name` property (e.g. { name: "tasks", schema: "public" })
* - changedFieldNames: an array of changed field names (e.g. [ "description", "updated_at" ])
* - isNew: boolean indicating whether the record is new or updated
* - trigger: the trigger information (if needed for extra filtering)
*
* @returns {Array} An array of matching scenario objects.
*/
function getMatchingScenarios(eventData) {
return notificationScenarios.filter((scenario) => {
// 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) {
return false;
}
}
// Check if key matches if provided
if (keys && !keys.some((key) => scenario.key === key)) return false;
// Check the onNew flag.
// Allow onNew to be either a boolean or an array of booleans.
if (Object.prototype.hasOwnProperty.call(scenario, "onNew")) {
if (Array.isArray(scenario.onNew)) {
if (!scenario.onNew.includes(eventData.isNew)) return false;
} else {
if (eventData.isNew !== scenario.onNew) return false;
}
}
// Check if onNew matches if provided
if (onNew !== undefined && scenario.onNew !== onNew) return false;
// If the scenario defines fields, ensure at least one of them is present in changedFieldNames.
if (scenario.fields && scenario.fields.length > 0) {
const hasMatchingField = scenario.fields.some((field) => eventData.changedFieldNames.includes(field));
if (!hasMatchingField) {
return false;
}
}
return true;
}) || null
);
return true;
});
}
// Example usage:
// console.log(hasScenarios({ table: 'jobs', keys: ['job-assigned-to-me'], onNew: false }));
// console.log(hasScenarios({ onNew: true, keys: ['tasks-updated-created'] }));
module.exports = {
notificationScenarios,
hasScenarios
getMatchingScenarios
};

View File

@@ -0,0 +1,83 @@
const eventParser = require("./eventParser");
const { client: gqlClient } = require("../../graphql-client/graphql-client");
const queries = require("../../graphql-client/queries");
const { isEmpty, isFunction } = require("lodash");
const { getMatchingScenarios } = require("./scenarioMapperr");
const scenarioParser = async (req) => {
// Destructure required fields from the request body
const { event, trigger, table } = req.body;
if (!event?.data || !trigger || !table) {
throw new Error("Missing required request fields: event data, trigger, or table.");
}
// Step 1: Parse the changes from the event
const eventData = await eventParser({
newData: event.data.new,
oldData: event.data.old,
trigger,
table,
jobIdField: `req.body.event.new.jobid`
});
// Step 2: Query jobWatchers for this job
const watcherData = await gqlClient.request(queries.GET_JOB_WATCHERS, {
jobid: eventData.jobId
});
const jobWatchers = watcherData?.job_watchers_aggregate?.nodes?.map((watcher) => watcher.user_email);
if (isEmpty(jobWatchers)) return;
// Step 3: Infer bodyshop information from the job and validate
const bodyShopId = watcherData?.job?.bodyshop?.id;
const bodyShopName = watcherData?.job?.bodyshop?.shopname;
if (!bodyShopId || !bodyShopName) {
throw new Error("No bodyshop data found for this job.");
}
// Step 4: Get matching scenarios based on eventData and jobWatchers
const matchingScenarios = getMatchingScenarios({
...eventData,
jobWatchers,
bodyShopId,
bodyShopName
});
if (isEmpty(matchingScenarios)) return;
// Prepare the final scenario data
const finalScenarioData = {
...eventData,
jobWatchers,
bodyShopId,
bodyShopName,
matchingScenarios
};
// Step 5: Query associations (notification_settings) for each watcher
// Filter by both useremail and shopid
const associationsData = await gqlClient.request(queries.GET_NOTIFICATION_ASSOCIATIONS, {
emails: jobWatchers,
shopid: bodyShopId
});
if (isEmpty(associationsData?.associations)) return;
// Step 6: For each matching scenario, add a scenarioWatchers property
// that includes only the jobWatchers with the notification setting enabled
finalScenarioData.matchingScenarios.forEach((scenario) => {
scenario.scenarioWatchers = associationsData.associations
.filter((assoc) => assoc.notification_settings && assoc.notification_settings[scenario.key] === true)
.map((assoc) => assoc.useremail);
});
// Step 7: Call builder functions for each matching scenario (fire-and-forget)
// Only invoke a builder if its scenario has at least one watcher
finalScenarioData.matchingScenarios.forEach((scenario) => {
if (!isEmpty(scenario.scenarioWatchers) && isFunction(scenario.builder)) {
scenario
.builder(finalScenarioData)
.catch((error) => console.error(`Error in builder for scenario '${scenario.key}':`, error));
}
});
};
module.exports = scenarioParser;