feature/IO-3096-GlobalNotifications - Check-point

This commit is contained in:
Dave Richer
2025-02-11 10:40:57 -05:00
parent 54820fe3c8
commit 2ee582bfa2
9 changed files with 156 additions and 91 deletions

View File

@@ -0,0 +1,23 @@
const path = require("path");
require("dotenv").config({
path: path.resolve(process.cwd(), `.env.${process.env.NODE_ENV || "development"}`)
});
const Queue = require("better-queue");
const logger = require("../../utils/logger");
const notificationsEmailQueue = () =>
new Queue(
(taskIds, cb) => {
logger.log("Processing Notification Emails: ", "silly", null, null);
cb(null);
},
{
batchSize: 50,
batchDelay: 5000,
// The lower this is, the more likely we are to hit the rate limit.
batchDelayTimeout: 1000
}
);
module.exports = { notificationsEmailQueue };

View File

@@ -1,13 +1,12 @@
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 tasksUpdatedCreatedBuilder = require("../scenarioBuilders/tasksUpdatedCreatedBuilder");
const notificationScenarios = [
{ key: "job-assigned-to-me", table: "jobs" },
{ key: "job-assigned-to-me", table: "jobs", fields: ["scheduled_in", "scheduled_completion", "scheduled_delivery"] },
{ key: "bill-posted", table: "bills" },
{ key: "new-note-added", table: "notes", onNew: true },
{

View File

@@ -4,9 +4,10 @@ const queries = require("../../graphql-client/queries");
const { isEmpty, isFunction } = require("lodash");
const { getMatchingScenarios } = require("./scenarioMapperr");
const scenarioParser = async (req) => {
const scenarioParser = async (req, jobIdField) => {
// 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.");
}
@@ -17,19 +18,29 @@ const scenarioParser = async (req) => {
oldData: event.data.old,
trigger,
table,
jobIdField: `req.body.event.new.jobid`
jobIdField
});
// 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;
const jobWatchers = watcherData?.job_watchers_aggregate?.nodes?.map((watcher) => ({
email: watcher.user_email,
firstName: watcher?.user?.employee?.first_name,
lastName: watcher?.user?.employee?.last_name,
employeeId: watcher?.user?.employee?.id
}));
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.");
}
@@ -41,7 +52,10 @@ const scenarioParser = async (req) => {
bodyShopId,
bodyShopName
});
if (isEmpty(matchingScenarios)) return;
if (isEmpty(matchingScenarios)) {
return;
}
// Prepare the final scenario data
const finalScenarioData = {
@@ -55,17 +69,20 @@ const scenarioParser = async (req) => {
// 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,
emails: jobWatchers.map((x) => x.email),
shopid: bodyShopId
});
if (isEmpty(associationsData?.associations)) return;
if (isEmpty(associationsData?.associations)) {
return;
}
// Step 6: For each matching scenario, add a scenarioWatchers property
// that includes only the jobWatchers with at least one notification method enabled.
// Each watcher object is formatted as: { user, email, app, fcm }
finalScenarioData.matchingScenarios.forEach((scenario) => {
scenario.scenarioWatchers = associationsData.associations
finalScenarioData.matchingScenarios = finalScenarioData.matchingScenarios.map((scenario) => ({
...scenario,
scenarioWatchers: associationsData.associations
.filter((assoc) => {
// Retrieve the settings object for this scenario (it now contains app, email, and fcm)
const settings = assoc.notification_settings && assoc.notification_settings[scenario.key];
@@ -74,26 +91,54 @@ const scenarioParser = async (req) => {
})
.map((assoc) => {
const settings = assoc.notification_settings[scenario.key];
// Determine the email from the association—either from assoc.user or assoc.useremail
const watcherEmail = assoc.user || assoc.useremail;
// Find the matching watcher object from jobWatchers using the email address
const matchingWatcher = jobWatchers.find((watcher) => watcher.email === watcherEmail);
return {
// Use assoc.user if available, otherwise fallback to assoc.useremail as the identifier
user: assoc.user || assoc.useremail,
// The email field here is the user's email notification setting (boolean)
// This is the common identifier (email in this case)
user: watcherEmail,
// Notification settings for this scenario
email: settings.email,
app: settings.app,
fcm: settings.fcm
fcm: settings.fcm,
// Additional fields from the watcher lookup
firstName: matchingWatcher ? matchingWatcher.firstName : undefined,
lastName: matchingWatcher ? matchingWatcher.lastName : undefined,
employeeId: matchingWatcher ? matchingWatcher.employeeId : undefined
};
});
});
})
}));
if (isEmpty(finalScenarioData?.matchingScenarios)) {
return;
}
// 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));
for (const scenario of finalScenarioData.matchingScenarios) {
if (isEmpty(scenario.scenarioWatchers) || !isFunction(scenario.builder)) {
continue;
}
});
scenario
.builder({
trigger: finalScenarioData.trigger.name,
bodyShopId: finalScenarioData.bodyShopId,
bodyShopName: finalScenarioData.bodyShopName,
scenarioKey: scenario.key,
scenarioTable: scenario.table,
scenarioFields: scenario.fields,
scenarioBuilder: scenario.builder,
scenarioWatchers: scenario.scenarioWatchers,
jobId: finalScenarioData.jobId,
isNew: finalScenarioData.isNew,
changedFieldNames: finalScenarioData.changedFieldNames,
changedFields: finalScenarioData.changedFields,
data: finalScenarioData.data
})
.catch((error) => console.error(`Error in builder for scenario '${scenario.key}':`, error));
}
};
module.exports = scenarioParser;