diff --git a/.changeset/cyan-parents-worry.md b/.changeset/cyan-parents-worry.md new file mode 100644 index 000000000..ea0226ff6 --- /dev/null +++ b/.changeset/cyan-parents-worry.md @@ -0,0 +1,5 @@ +--- +'@flatfile/plugin-record-hook': patch +--- + +Fix to compare full Record objects instead of just Record values diff --git a/package-lock.json b/package-lock.json index 11274068c..e42ded02c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10679,7 +10679,7 @@ }, "plugins/record-hook": { "name": "@flatfile/plugin-record-hook", - "version": "1.1.7", + "version": "1.1.8", "license": "ISC", "dependencies": { "@flatfile/api": "^1.5.33", diff --git a/plugins/record-hook/src/RecordHook.ts b/plugins/record-hook/src/RecordHook.ts index 15889cac0..dee19d121 100644 --- a/plugins/record-hook/src/RecordHook.ts +++ b/plugins/record-hook/src/RecordHook.ts @@ -74,12 +74,18 @@ export const BulkRecordHook = async ( return } + await event.cache.init>('originalRecords', fetchData) + // Execute client-defined data hooks await asyncBatch(batch.records, handler, options, event) event.afterAll(async () => { const batch = event.cache.get>('records') - const modifiedRecords = batch.records.filter(hasRecordChanges) + const originalRecords = + event.cache.get>('originalRecords') + const modifiedRecords = batch.records.filter((record) => + hasRecordChanges(record, originalRecords.records) + ) if (!modifiedRecords || modifiedRecords.length === 0) { if (options.debug) { console.log('No records modified') @@ -105,12 +111,29 @@ export const BulkRecordHook = async ( return handler } -const hasRecordChanges = (record: FlatfileRecord) => { - const messageCount = record.toJSON().info.length - return ( - JSON.stringify(record.originalValue) !== JSON.stringify(record.value) || - messageCount > 0 +const hasRecordChanges = ( + record: FlatfileRecord, + originalRecords: FlatfileRecord[] +) => { + const originalRecord = originalRecords.find( + (original) => original.rowId === record.rowId ) + return !deepEqual(originalRecord, record) +} + +function deepEqual(obj1, obj2) { + if (obj1 === obj2) return true + + const keysA = Object.keys(obj1) + const keysB = Object.keys(obj2) + + if (keysA.length !== keysB.length) return false + + for (let key of keysA) { + if (!keysB.includes(key) || !deepEqual(obj1[key], obj2[key])) return false + } + + return true } const prepareXRecords = async (records: any): Promise> => {