feature/IO-3096-GlobalNotifications - Checkpoint
This commit is contained in:
@@ -7,12 +7,13 @@
|
|||||||
const tasksUpdatedCreatedBuilder = require("../scenarioBuilders/tasksUpdatedCreatedBuilder");
|
const tasksUpdatedCreatedBuilder = require("../scenarioBuilders/tasksUpdatedCreatedBuilder");
|
||||||
const jobStatusChangeBuilder = require("../scenarioBuilders/jobStatusChangeBuilder");
|
const jobStatusChangeBuilder = require("../scenarioBuilders/jobStatusChangeBuilder");
|
||||||
const jobAssignedToMeBuilder = require("../scenarioBuilders/jobAssignedToMeBuilder");
|
const jobAssignedToMeBuilder = require("../scenarioBuilders/jobAssignedToMeBuilder");
|
||||||
|
|
||||||
const notificationScenarios = [
|
const notificationScenarios = [
|
||||||
{
|
{
|
||||||
key: "job-assigned-to-me",
|
key: "job-assigned-to-me",
|
||||||
table: "jobs",
|
table: "jobs",
|
||||||
fields: ["employee_pre", "employee_body", "employee_csr", "employee_refinish"],
|
fields: ["employee_pre", "employee_body", "employee_csr", "employee_refinish"],
|
||||||
matchEmployee: true,
|
matchToUserFields: ["employee_pre", "employee_body", "employee_csr", "employee_refinish"],
|
||||||
builder: jobAssignedToMeBuilder
|
builder: jobAssignedToMeBuilder
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -3,17 +3,22 @@ const { client: gqlClient } = require("../../graphql-client/graphql-client");
|
|||||||
const queries = require("../../graphql-client/queries");
|
const queries = require("../../graphql-client/queries");
|
||||||
const { isEmpty, isFunction } = require("lodash");
|
const { isEmpty, isFunction } = require("lodash");
|
||||||
const { getMatchingScenarios } = require("./scenarioMapperr");
|
const { getMatchingScenarios } = require("./scenarioMapperr");
|
||||||
const { writeFile } = require("node:fs").promises;
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses an event and determines matching scenarios for notifications.
|
||||||
|
* Queries job watchers and notification settings before triggering scenario builders.
|
||||||
|
*
|
||||||
|
* @param {Object} req - The request object containing event data.
|
||||||
|
* @param {string} jobIdField - The field used to identify the job ID.
|
||||||
|
*/
|
||||||
const scenarioParser = async (req, jobIdField) => {
|
const scenarioParser = async (req, jobIdField) => {
|
||||||
// Destructure required fields from the request body
|
|
||||||
const { event, trigger, table } = req.body;
|
const { event, trigger, table } = req.body;
|
||||||
|
|
||||||
if (!event?.data || !trigger || !table) {
|
if (!event?.data || !trigger || !table) {
|
||||||
throw new Error("Missing required request fields: event data, trigger, or table.");
|
throw new Error("Missing required request fields: event data, trigger, or table.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 1: Parse the changes from the event
|
// Step 1: Parse event data to extract necessary details.
|
||||||
const eventData = await eventParser({
|
const eventData = await eventParser({
|
||||||
newData: event.data.new,
|
newData: event.data.new,
|
||||||
oldData: event.data.old,
|
oldData: event.data.old,
|
||||||
@@ -22,7 +27,7 @@ const scenarioParser = async (req, jobIdField) => {
|
|||||||
jobIdField
|
jobIdField
|
||||||
});
|
});
|
||||||
|
|
||||||
// Step 2: Query jobWatchers for this job
|
// Step 2: Query job watchers for the given job ID.
|
||||||
const watcherData = await gqlClient.request(queries.GET_JOB_WATCHERS, {
|
const watcherData = await gqlClient.request(queries.GET_JOB_WATCHERS, {
|
||||||
jobid: eventData.jobId
|
jobid: eventData.jobId
|
||||||
});
|
});
|
||||||
@@ -38,7 +43,7 @@ const scenarioParser = async (req, jobIdField) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 3: Infer bodyshop information from the job and validate
|
// Step 3: Retrieve body shop information from the job.
|
||||||
const bodyShopId = watcherData?.job?.bodyshop?.id;
|
const bodyShopId = watcherData?.job?.bodyshop?.id;
|
||||||
const bodyShopName = watcherData?.job?.bodyshop?.shopname;
|
const bodyShopName = watcherData?.job?.bodyshop?.shopname;
|
||||||
|
|
||||||
@@ -46,7 +51,7 @@ const scenarioParser = async (req, jobIdField) => {
|
|||||||
throw new Error("No bodyshop data found for this job.");
|
throw new Error("No bodyshop data found for this job.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 4: Get matching scenarios based on eventData and jobWatchers
|
// Step 4: Determine matching scenarios based on event data.
|
||||||
const matchingScenarios = getMatchingScenarios({
|
const matchingScenarios = getMatchingScenarios({
|
||||||
...eventData,
|
...eventData,
|
||||||
jobWatchers,
|
jobWatchers,
|
||||||
@@ -58,7 +63,6 @@ const scenarioParser = async (req, jobIdField) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare the final scenario data
|
|
||||||
const finalScenarioData = {
|
const finalScenarioData = {
|
||||||
...eventData,
|
...eventData,
|
||||||
jobWatchers,
|
jobWatchers,
|
||||||
@@ -67,8 +71,7 @@ const scenarioParser = async (req, jobIdField) => {
|
|||||||
matchingScenarios
|
matchingScenarios
|
||||||
};
|
};
|
||||||
|
|
||||||
// Step 5: Query associations (notification_settings) for each watcher
|
// Step 5: Query notification settings for job watchers.
|
||||||
// Filter by both useremail and shopid
|
|
||||||
const associationsData = await gqlClient.request(queries.GET_NOTIFICATION_ASSOCIATIONS, {
|
const associationsData = await gqlClient.request(queries.GET_NOTIFICATION_ASSOCIATIONS, {
|
||||||
emails: jobWatchers.map((x) => x.email),
|
emails: jobWatchers.map((x) => x.email),
|
||||||
shopid: bodyShopId
|
shopid: bodyShopId
|
||||||
@@ -78,33 +81,24 @@ const scenarioParser = async (req, jobIdField) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 6: For each matching scenario, add a scenarioWatchers property
|
// Step 6: Filter scenario watchers based on enabled notification methods.
|
||||||
// that includes only the jobWatchers with at least one notification method enabled.
|
|
||||||
// Each watcher object is formatted as: { user, email, app, fcm }
|
|
||||||
finalScenarioData.matchingScenarios = finalScenarioData.matchingScenarios.map((scenario) => ({
|
finalScenarioData.matchingScenarios = finalScenarioData.matchingScenarios.map((scenario) => ({
|
||||||
...scenario,
|
...scenario,
|
||||||
scenarioWatchers: associationsData.associations
|
scenarioWatchers: associationsData.associations
|
||||||
.filter((assoc) => {
|
.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];
|
const settings = assoc.notification_settings && assoc.notification_settings[scenario.key];
|
||||||
// Only include this association if at least one notification channel is enabled
|
|
||||||
return settings && (settings.app || settings.email || settings.fcm);
|
return settings && (settings.app || settings.email || settings.fcm);
|
||||||
})
|
})
|
||||||
.map((assoc) => {
|
.map((assoc) => {
|
||||||
const settings = assoc.notification_settings[scenario.key];
|
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;
|
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);
|
const matchingWatcher = jobWatchers.find((watcher) => watcher.email === watcherEmail);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
// This is the common identifier (email in this case)
|
|
||||||
user: watcherEmail,
|
user: watcherEmail,
|
||||||
// Notification settings for this scenario
|
|
||||||
email: settings.email,
|
email: settings.email,
|
||||||
app: settings.app,
|
app: settings.app,
|
||||||
fcm: settings.fcm,
|
fcm: settings.fcm,
|
||||||
// Additional fields from the watcher lookup
|
|
||||||
firstName: matchingWatcher ? matchingWatcher.firstName : undefined,
|
firstName: matchingWatcher ? matchingWatcher.firstName : undefined,
|
||||||
lastName: matchingWatcher ? matchingWatcher.lastName : undefined,
|
lastName: matchingWatcher ? matchingWatcher.lastName : undefined,
|
||||||
employeeId: matchingWatcher ? matchingWatcher.employeeId : undefined
|
employeeId: matchingWatcher ? matchingWatcher.employeeId : undefined
|
||||||
@@ -116,21 +110,40 @@ const scenarioParser = async (req, jobIdField) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 7: Call builder functions for each matching scenario (fire-and-forget)
|
// Step 7: Trigger scenario builders for matching scenarios with eligible watchers.
|
||||||
// Only invoke a builder if its scenario has at least one watcher
|
|
||||||
for (const scenario of finalScenarioData.matchingScenarios) {
|
for (const scenario of finalScenarioData.matchingScenarios) {
|
||||||
if (isEmpty(scenario.scenarioWatchers) || !isFunction(scenario.builder)) {
|
if (isEmpty(scenario.scenarioWatchers) || !isFunction(scenario.builder)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let eligibleWatchers = scenario.scenarioWatchers;
|
||||||
|
|
||||||
|
// Ensure watchers are only notified if they are assigned to the changed field.
|
||||||
|
if (scenario.matchToUserFields && scenario.matchToUserFields.length > 0) {
|
||||||
|
eligibleWatchers = scenario.scenarioWatchers.filter((watcher) =>
|
||||||
|
scenario.matchToUserFields.some(
|
||||||
|
(field) => eventData.changedFieldNames.includes(field) && eventData.data[field]?.includes(watcher.employeeId)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isEmpty(eligibleWatchers)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 8: Filter scenario fields to only include changed fields.
|
||||||
|
const filteredScenarioFields =
|
||||||
|
scenario.fields?.filter((field) => eventData.changedFieldNames.includes(field)) || [];
|
||||||
|
|
||||||
scenario.builder({
|
scenario.builder({
|
||||||
trigger: finalScenarioData.trigger.name,
|
trigger: finalScenarioData.trigger.name,
|
||||||
bodyShopId: finalScenarioData.bodyShopId,
|
bodyShopId: finalScenarioData.bodyShopId,
|
||||||
bodyShopName: finalScenarioData.bodyShopName,
|
bodyShopName: finalScenarioData.bodyShopName,
|
||||||
scenarioKey: scenario.key,
|
scenarioKey: scenario.key,
|
||||||
scenarioTable: scenario.table,
|
scenarioTable: scenario.table,
|
||||||
scenarioFields: scenario.fields,
|
scenarioFields: filteredScenarioFields,
|
||||||
scenarioBuilder: scenario.builder,
|
scenarioBuilder: scenario.builder,
|
||||||
scenarioWatchers: scenario.scenarioWatchers,
|
scenarioWatchers: eligibleWatchers,
|
||||||
jobId: finalScenarioData.jobId,
|
jobId: finalScenarioData.jobId,
|
||||||
isNew: finalScenarioData.isNew,
|
isNew: finalScenarioData.isNew,
|
||||||
changedFieldNames: finalScenarioData.changedFieldNames,
|
changedFieldNames: finalScenarioData.changedFieldNames,
|
||||||
|
|||||||
Reference in New Issue
Block a user