feature/IO-3096-GlobalNotifications - Code Review Part 2
This commit is contained in:
@@ -3,15 +3,14 @@
|
||||
*
|
||||
* This function analyzes the differences between previous (`oldData`) and current (`newData`)
|
||||
* data states to identify changed fields. It determines if the event is a new entry or an update
|
||||
* and optionally extracts a `jobId` based on a specified field. The result includes details
|
||||
* about changed fields, the event type, and associated metadata.
|
||||
* and returns details about changed fields, the event type, and associated metadata.
|
||||
*
|
||||
* @param {Object} options - Configuration options for parsing the event.
|
||||
* @param {Object} [options.oldData] - The previous state of the data (undefined for new entries).
|
||||
* @param {Object} options.newData - The current state of the data.
|
||||
* @param {string} options.trigger - The type of event trigger (e.g., 'INSERT', 'UPDATE').
|
||||
* @param {string} options.table - The name of the table associated with the event.
|
||||
* @param {string} [options.jobIdField] - The field name used to extract the jobId (optional).
|
||||
* @param {string} [options.jobId] - The job ID, if already extracted by the caller (optional).
|
||||
* @returns {Object} An object containing the parsed event details:
|
||||
* - {Array<string>} changedFieldNames - List of field names that have changed.
|
||||
* - {Object} changedFields - Map of changed fields with their old and new values.
|
||||
@@ -19,9 +18,9 @@
|
||||
* - {Object} data - The current data state (`newData`).
|
||||
* - {string} trigger - The event trigger type.
|
||||
* - {string} table - The table name.
|
||||
* - {string|null} jobId - The extracted jobId or null if not applicable.
|
||||
* - {string|null} jobId - The provided jobId or null if not provided.
|
||||
*/
|
||||
const eventParser = async ({ oldData, newData, trigger, table, jobIdField }) => {
|
||||
const eventParser = async ({ oldData, newData, trigger, table, jobId = null }) => {
|
||||
const isNew = !oldData; // True if no old data exists, indicating a new entry
|
||||
let changedFields = {};
|
||||
let changedFieldNames = [];
|
||||
@@ -61,19 +60,6 @@ const eventParser = async ({ oldData, newData, trigger, table, jobIdField }) =>
|
||||
}
|
||||
}
|
||||
|
||||
// Extract jobId if jobIdField is provided
|
||||
let jobId = null;
|
||||
if (jobIdField) {
|
||||
let keyName = jobIdField;
|
||||
const prefix = "req.body.event.new.";
|
||||
// Strip prefix if present to isolate the actual field name
|
||||
if (keyName.startsWith(prefix)) {
|
||||
keyName = keyName.slice(prefix.length);
|
||||
}
|
||||
// Look for jobId in newData first, then fallback to oldData if necessary
|
||||
jobId = newData[keyName] || (oldData && oldData[keyName]) || null;
|
||||
}
|
||||
|
||||
return {
|
||||
changedFieldNames, // Array of fields that changed
|
||||
changedFields, // Object with old/new values for changed fields
|
||||
@@ -81,7 +67,7 @@ const eventParser = async ({ oldData, newData, trigger, table, jobIdField }) =>
|
||||
data: newData, // Current data state
|
||||
trigger, // Event trigger (e.g., 'INSERT', 'UPDATE')
|
||||
table, // Associated table name
|
||||
jobId // Extracted jobId or null
|
||||
jobId // Provided jobId or null
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user