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

@@ -34,6 +34,7 @@ export const QUERY_BODYSHOP = gql`
authlevel
useremail
default_prod_list_view
notification_settings
user {
authid
email

View File

@@ -2252,7 +2252,7 @@ exports.UPDATE_PARTS_CRITICAL = `mutation UPDATE_PARTS_CRITICAL ($IdsToMarkCriti
notcritical: update_joblines(where: {id: {_nin: $IdsToMarkCritical}, jobid: {_eq: $jobid}}, _set: {critical: false}) {
affected_rows
}
}`
}`;
exports.ACTIVE_SHOP_BY_USER = `query ACTIVE_SHOP_BY_USER($user: String) {
associations(where: {active: {_eq: true}, useremail: {_eq: $user}}) {
@@ -2618,7 +2618,6 @@ exports.CREATE_CONVERSATION = `mutation CREATE_CONVERSATION($conversation: [conv
}
`;
exports.STATUS_UPDATE = `query STATUS_UPDATE($period: timestamptz!, $today: timestamptz!) {
bodyshops(where: { created_at: { _gte: $period } }) {
shopname
@@ -2689,4 +2688,33 @@ exports.STATUS_UPDATE = `query STATUS_UPDATE($period: timestamptz!, $today: time
}
}
}
`
`;
exports.GET_JOB_WATCHERS = `
query GET_JOB_WATCHERS($jobid: uuid!) {
job_watchers_aggregate(where: { jobid: { _eq: $jobid } }) {
nodes {
user_email
}
}
job: jobs_by_pk(id: $jobid) {
id,
bodyshop {
id
shopname
}
}
}
`;
exports.GET_NOTIFICATION_ASSOCIATIONS = `
query GET_NOTIFICATION_ASSOCIATIONS($emails: [String!]!, $shopid: uuid!) {
associations(where: {
useremail: { _in: $emails },
shopid: { _eq: $shopid }
}) {
useremail
notification_settings
}
}
`;

View File

@@ -1,34 +1,9 @@
const changeParser = require("../utils/changeParser");
const { hasScenarios } = require("../utils/scenarioMapperr");
const scenarioParser = require("../utils/scenarioParser");
const handleTasksChange = async (req, res) => {
try {
// Step 1: Parse the changes
const changes = await changeParser({
newData: req?.body?.event?.data?.new,
oldData: req?.body?.event?.data?.old,
trigger: req?.body?.trigger,
table: req?.body?.table
});
console.dir(changes, { depth: null });
const scenarios = hasScenarios({
table: changes.table.name,
keys: changes.changedFieldNames,
onNew: changes.isNew
});
console.dir(scenarios, { depth: null });
// Step 2: See if any scenarios match the changes
// Step 3: Handle the scenario
} catch (error) {
console.error("Error handling tasks change:", error);
return res.status(500).json({ message: "Error handling tasks change." });
}
// Get Bodyshop from hasura user id,
return res.status(200).json({ message: "Tasks change handled." });
const { logger } = req;
scenarioParser(req).catch((e) => logger.log("notifications-error", "error", "jsr", null, { error: e?.message }));
return res.status(200).json({ message: "Notification Scenario Event Handled." });
};
//
module.exports = handleTasksChange;

View File

@@ -1,3 +1,64 @@
const tasksUpdatedCreatedBuilder = async () => {};
const consoleDir = require("../../utils/consoleDir");
const { sendTaskEmail } = require("../../email/sendemail");
// {
// "changedFieldNames": [
// "description",
// "updated_at"
// ],
// "changedFields": {
// "description": "sadasdasdasdsadssdsaddddsdsd",
// "updated_at": "2025-02-10T19:20:34.195086+00:00"
// },
// "isNew": false,
// "data": {
// "assigned_to": "5e4f78a2-0f23-4e7e-920c-02a4e016b398",
// "billid": null,
// "bodyshopid": "71f8494c-89f0-43e0-8eb2-820b52d723bc",
// "completed": false,
// "completed_at": null,
// "created_at": "2025-02-09T20:02:46.839271+00:00",
// "created_by": "dave@imex.dev",
// "deleted": false,
// "deleted_at": null,
// "description": "sadasdasdasdsadssdsaddddsdsd",
// "due_date": null,
// "id": "ca1c49a9-3c26-46cb-bebd-4b93f02cad2a",
// "jobid": "ec1c26c7-b0ea-493f-9bba-30efc291e0fa",
// "joblineid": "84b5bbf9-ab57-4c77-abb0-8fdd8709c9ff",
// "partsorderid": null,
// "priority": 2,
// "remind_at": null,
// "remind_at_sent": null,
// "title": "sd",
// "updated_at": "2025-02-10T19:20:34.195086+00:00"
// },
// "trigger": {
// "name": "notifications_tasks"
// },
// "table": {
// "name": "tasks",
// "schema": "public"
// },
// "jobId": "ec1c26c7-b0ea-493f-9bba-30efc291e0fa",
// "watchers": [
// "dave@imex.dev"
// ],
// "bodyShopId": "71f8494c-89f0-43e0-8eb2-820b52d723bc",
// "bodyShopName": "Rome Online Collision DEMO"
// }
const tasksUpdatedCreatedBuilder = async (data) => {
consoleDir(data);
// Step 0: Check to see if the users are watching the current scenario
// Step 1: Dispatch Email to all watchers
// sendTaskEmail({
// bcc: data.watchers,
// subject: `Task Updated: ${data.data.title}`,
// text: `Task Updated: ${data.data.title}`
// });
// Step 2: Send notification and basic paramaters to a real time job queue to debounce potential multiple notifications
};
module.exports = tasksUpdatedCreatedBuilder;

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;

View File

@@ -39,10 +39,9 @@ const redisSocketEvents = ({
const registerSocketEvents = (socket) => {
// Uncomment for further testing
// createLogEvent(socket, "debug", `Registering RedisIO Socket Events.`);
getUserSocketMapping(socket.user.email).then((socketIds) => {
console.log(socketIds);
});
// getUserSocketMapping(socket.user.email).then((socketIds) => {
// console.log(socketIds);
// });
// Token Update Events
const registerUpdateEvents = (socket) => {
@@ -62,7 +61,7 @@ const redisSocketEvents = ({
// Update the session data in Redis with the new token info
// await setSessionData(socket.id, "user", user);
// Update the mapping with the user's email
// await addUserSocketMapping(user.email, socket.id);
await addUserSocketMapping(user.email, socket.id);
createLogEvent(socket, "debug", `Token updated successfully for socket ID: ${socket.id}`);
socket.emit("token-updated", { success: true });
} catch (error) {