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: "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-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: "supplement-imported" }, { key: "critical-parts-status-changed" }, { key: "part-marked-back-ordered" } ]; /** * 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 };