104 lines
3.5 KiB
JavaScript
104 lines
3.5 KiB
JavaScript
// 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 jobStatusChangeBuilder = require("../scenarioBuilders/jobStatusChangeBuilder");
|
|
const jobAssignedToMeBuilder = require("../scenarioBuilders/jobAssignedToMeBuilder");
|
|
const notificationScenarios = [
|
|
{
|
|
key: "job-assigned-to-me",
|
|
table: "jobs",
|
|
fields: ["employee_pre", "employee_body", "employee_csr", "employee_refinish"],
|
|
matchEmployee: true,
|
|
builder: jobAssignedToMeBuilder
|
|
},
|
|
{
|
|
key: "bill-posted",
|
|
table: "bills"
|
|
},
|
|
{
|
|
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: true,
|
|
builder: tasksUpdatedCreatedBuilder
|
|
},
|
|
{
|
|
key: "job-status-change",
|
|
table: "jobs",
|
|
fields: ["status"],
|
|
builder: jobStatusChangeBuilder
|
|
},
|
|
{ key: "job-added-to-production", table: "jobs", fields: ["introduction"] },
|
|
{ key: "alternate-transport-changed", table: "jobs", fields: ["alt_transport"] },
|
|
{ key: "payment-collected-completed", table: "payments", onNew: true },
|
|
// MAKE SURE YOU ARE NOT ON A LMS ENVIRONMENT
|
|
{ key: "new-media-added-reassigned", table: "documents" },
|
|
{ key: "new-time-ticket-posted", table: "timetickets" },
|
|
{ key: "intake-delivery-checklist-completed", table: "jobs", fields: ["intakechecklist"] },
|
|
{ key: "supplement-imported" },
|
|
{ key: "critical-parts-status-changed", table: "joblines" },
|
|
{ key: "part-marked-back-ordered", table: "joblines" }
|
|
];
|
|
|
|
/**
|
|
* 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 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;
|
|
}
|
|
}
|
|
|
|
// 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;
|
|
});
|
|
}
|
|
|
|
module.exports = {
|
|
notificationScenarios,
|
|
getMatchingScenarios
|
|
};
|