IO-3166-Global-Notifications-Part-2: Add Enabled key to scenario map (backend), filter out scenarios not enabled.

This commit is contained in:
Dave Richer
2025-03-12 11:46:09 -04:00
parent 8ae3b28cb6
commit 9ef8440e64

View File

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