78 lines
3.0 KiB
JavaScript
78 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:
|
|
* - {@link changedFieldNames}: An array of field names that have changed.
|
|
* - {@link changedFields}: An object mapping changed field names to their new values (or `null` if the field was removed).
|
|
* - {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 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);
|
|
}
|
|
}
|
|
}
|
|
// Extract jobId based on jobIdField
|
|
let jobId = null;
|
|
if (jobIdField) {
|
|
// If the jobIdField is provided as a string like "req.body.event.new.jobid",
|
|
// strip the prefix if it exists so we can use the property name.
|
|
let keyName = jobIdField;
|
|
const prefix = "req.body.event.new.";
|
|
if (keyName.startsWith(prefix)) {
|
|
keyName = keyName.slice(prefix.length);
|
|
}
|
|
// Attempt to retrieve the job id from newData first; if not available, try oldData.
|
|
jobId = newData[keyName] || (oldData && oldData[keyName]) || null;
|
|
}
|
|
|
|
return {
|
|
changedFieldNames,
|
|
changedFields,
|
|
isNew,
|
|
data: newData,
|
|
trigger,
|
|
table,
|
|
jobId
|
|
};
|
|
};
|
|
|
|
module.exports = eventParser;
|