84 lines
3.0 KiB
JavaScript
84 lines
3.0 KiB
JavaScript
/**
|
||
* Parses an event by comparing old and new data to determine which fields have changed.
|
||
*
|
||
* @async
|
||
* @function eventParser
|
||
* @param {Object} params - The parameters for parsing the event.
|
||
* @param {Object} params.oldData - The previous state of the data. If not provided, the data is considered new.
|
||
* @param {Object} params.newData - The new state of the data.
|
||
* @param {string} params.trigger - The trigger that caused the event.
|
||
* @param {string} params.table - The name of the table where the event occurred.
|
||
* @param {string} [params.jobIdField] - The field name or key path (e.g., "req.body.event.new.jobid") used to extract the job ID.
|
||
* @returns {Promise<Object>} An object containing:
|
||
* - {string[]} changedFieldNames - An array of field names that have changed.
|
||
* - {Object} changedFields - An object mapping changed field names to an object with `old` and `new` values.
|
||
* - {boolean} isNew - Indicates if the event is for new data (i.e., no oldData exists).
|
||
* - {Object} data - The new data.
|
||
* - {string} trigger - The event trigger.
|
||
* - {string} table - The table name.
|
||
* - {string|null} jobId - The extracted job ID, if available.
|
||
*/
|
||
const eventParser = async ({ oldData, newData, trigger, table, jobIdField }) => {
|
||
const isNew = !oldData;
|
||
let changedFields = {};
|
||
let changedFieldNames = [];
|
||
|
||
if (isNew) {
|
||
// If there's no old data, every field in newData is considered new
|
||
changedFields = Object.fromEntries(
|
||
Object.entries(newData).map(([key, value]) => [key, { old: undefined, new: value }])
|
||
);
|
||
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] = {
|
||
old: oldData[key], // Could be undefined if key didn’t exist in oldData
|
||
new: 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] = {
|
||
old: oldData[key],
|
||
new: null // Indicate field was removed
|
||
};
|
||
changedFieldNames.push(key);
|
||
}
|
||
}
|
||
}
|
||
|
||
// Extract jobId based on jobIdField
|
||
let jobId = null;
|
||
if (jobIdField) {
|
||
let keyName = jobIdField;
|
||
const prefix = "req.body.event.new.";
|
||
if (keyName.startsWith(prefix)) {
|
||
keyName = keyName.slice(prefix.length);
|
||
}
|
||
jobId = newData[keyName] || (oldData && oldData[keyName]) || null;
|
||
}
|
||
|
||
return {
|
||
changedFieldNames,
|
||
changedFields,
|
||
isNew,
|
||
data: newData,
|
||
trigger,
|
||
table,
|
||
jobId
|
||
};
|
||
};
|
||
|
||
module.exports = eventParser;
|