feature/IO-3096-GlobalNotifications - Check-point
This commit is contained in:
43
server/notifications/utils/changeParser.js
Normal file
43
server/notifications/utils/changeParser.js
Normal file
@@ -0,0 +1,43 @@
|
||||
const changeParser = async ({ oldData, newData, trigger, table }) => {
|
||||
const isNew = !oldData;
|
||||
let changedFields = {};
|
||||
let changedFieldNames = [];
|
||||
|
||||
if (isNew) {
|
||||
// If there's no old data, every field in newData is considered changed (new)
|
||||
changedFields = { ...newData };
|
||||
changedFieldNames = Object.keys(newData);
|
||||
} else {
|
||||
// Compare oldData with newData for changes
|
||||
for (const key in newData) {
|
||||
if (Object.prototype.hasOwnProperty.call(newData, key)) {
|
||||
// Check if the key exists in oldData and if values differ
|
||||
if (
|
||||
!Object.prototype.hasOwnProperty.call(oldData, key) ||
|
||||
JSON.stringify(oldData[key]) !== JSON.stringify(newData[key])
|
||||
) {
|
||||
changedFields[key] = newData[key];
|
||||
changedFieldNames.push(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Check for fields that were removed
|
||||
for (const key in oldData) {
|
||||
if (Object.prototype.hasOwnProperty.call(oldData, key) && !Object.prototype.hasOwnProperty.call(newData, key)) {
|
||||
changedFields[key] = null; // Indicate field was removed
|
||||
changedFieldNames.push(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
changedFieldNames,
|
||||
changedFields,
|
||||
isNew,
|
||||
data: newData,
|
||||
trigger,
|
||||
table
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = changeParser;
|
||||
52
server/notifications/utils/scenarioMapperr.js
Normal file
52
server/notifications/utils/scenarioMapperr.js
Normal file
@@ -0,0 +1,52 @@
|
||||
const tasksUpdatedCreatedBuilder = require("../scenarioBuilders/tasksUpdatedCreatedBuilder");
|
||||
|
||||
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: "tasks-updated-created",
|
||||
table: "tasks",
|
||||
fields: ["updated_at"],
|
||||
onNew: false,
|
||||
builder: tasksUpdatedCreatedBuilder
|
||||
},
|
||||
{ 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" }
|
||||
];
|
||||
|
||||
// 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;
|
||||
|
||||
// Check if key matches if provided
|
||||
if (keys && !keys.some((key) => scenario.key === key)) return false;
|
||||
|
||||
// Check if onNew matches if provided
|
||||
if (onNew !== undefined && scenario.onNew !== onNew) return false;
|
||||
|
||||
return true;
|
||||
}) || null
|
||||
);
|
||||
}
|
||||
|
||||
// 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
|
||||
};
|
||||
Reference in New Issue
Block a user