feature/IO-3096-GlobalNotifications - Check-point
This commit is contained in:
@@ -1,52 +1,83 @@
|
||||
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: "critical-parts-status-changed" },
|
||||
{ key: "part-marked-back-ordered" },
|
||||
{ key: "new-note-added", table: "notes" },
|
||||
{ key: "supplement-imported" },
|
||||
{ key: "schedule-dates-changed", table: "jobs" },
|
||||
{ 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: false,
|
||||
// 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: "job-added-to-production", table: "jobs" },
|
||||
{ key: "job-status-change", table: "jobs" },
|
||||
{ key: "payment-collected-completed" },
|
||||
{ key: "alternate-transport-changed" }
|
||||
{ key: "supplement-imported" },
|
||||
{ key: "critical-parts-status-changed" },
|
||||
{ key: "part-marked-back-ordered" }
|
||||
];
|
||||
|
||||
// Helper function to find a scenario based on multiple criteria
|
||||
function hasScenarios({ table, keys, onNew }) {
|
||||
return (
|
||||
notificationScenarios.find((scenario) => {
|
||||
// Check if table matches if provided
|
||||
if (table && scenario.table !== table) return false;
|
||||
/**
|
||||
* 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 if key matches if provided
|
||||
if (keys && !keys.some((key) => scenario.key === key)) 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;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if onNew matches if provided
|
||||
if (onNew !== undefined && scenario.onNew !== 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;
|
||||
}) || null
|
||||
);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
// Example usage:
|
||||
// console.log(hasScenarios({ table: 'jobs', keys: ['job-assigned-to-me'], onNew: false }));
|
||||
// console.log(hasScenarios({ onNew: true, keys: ['tasks-updated-created'] }));
|
||||
|
||||
module.exports = {
|
||||
notificationScenarios,
|
||||
hasScenarios
|
||||
getMatchingScenarios
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user