From e6ed034c0f4aa7799558586ee00ba63eecc3f14e Mon Sep 17 00:00:00 2001 From: Carl Brugger Date: Wed, 1 Nov 2023 16:35:27 -0500 Subject: [PATCH 1/4] fix: record comparison (#292) --- .changeset/cyan-parents-worry.md | 5 ++++ plugins/record-hook/src/RecordHook.ts | 35 ++++++++++++++++++++++----- 2 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 .changeset/cyan-parents-worry.md 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/plugins/record-hook/src/RecordHook.ts b/plugins/record-hook/src/RecordHook.ts index f5c799e40..6ff3c5210 100644 --- a/plugins/record-hook/src/RecordHook.ts +++ b/plugins/record-hook/src/RecordHook.ts @@ -85,12 +85,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') @@ -116,12 +122,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> => { From 22560cf31cf7cf4988cf39e7e0ddcbe206c2b43c Mon Sep 17 00:00:00 2001 From: Flatfile Bot <62193415+flatfiledocker@users.noreply.github.com> Date: Wed, 1 Nov 2023 14:44:26 -0700 Subject: [PATCH 2/4] chore: update versions (#295) Co-authored-by: github-actions[bot] --- .changeset/afraid-melons-sniff.md | 5 ----- .changeset/cyan-parents-worry.md | 5 ----- plugins/record-hook/CHANGELOG.md | 7 +++++++ plugins/record-hook/package.json | 2 +- 4 files changed, 8 insertions(+), 11 deletions(-) delete mode 100644 .changeset/afraid-melons-sniff.md delete mode 100644 .changeset/cyan-parents-worry.md diff --git a/.changeset/afraid-melons-sniff.md b/.changeset/afraid-melons-sniff.md deleted file mode 100644 index f91e23363..000000000 --- a/.changeset/afraid-melons-sniff.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'@flatfile/plugin-record-hook': patch ---- - -Only complete commits when trackChanges is enabled on the Workbook diff --git a/.changeset/cyan-parents-worry.md b/.changeset/cyan-parents-worry.md deleted file mode 100644 index ea0226ff6..000000000 --- a/.changeset/cyan-parents-worry.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'@flatfile/plugin-record-hook': patch ---- - -Fix to compare full Record objects instead of just Record values diff --git a/plugins/record-hook/CHANGELOG.md b/plugins/record-hook/CHANGELOG.md index 02f91781e..6147d640a 100644 --- a/plugins/record-hook/CHANGELOG.md +++ b/plugins/record-hook/CHANGELOG.md @@ -1,5 +1,12 @@ # @flatfile/plugin-record-hook +## 1.1.9 + +### Patch Changes + +- 265412b: Only complete commits when trackChanges is enabled on the Workbook +- e6ed034: Fix to compare full Record objects instead of just Record values + ## 1.1.8 ### Patch Changes diff --git a/plugins/record-hook/package.json b/plugins/record-hook/package.json index 592dbc2e7..33acfa129 100644 --- a/plugins/record-hook/package.json +++ b/plugins/record-hook/package.json @@ -1,6 +1,6 @@ { "name": "@flatfile/plugin-record-hook", - "version": "1.1.8", + "version": "1.1.9", "description": "A plugin for writing custom record-level hooks in Flatfile.", "registryMetadata": { "category": "records" From 2e2a2018c6796a4c6fb1afb469d697f0a4bc4327 Mon Sep 17 00:00:00 2001 From: Carl Brugger Date: Thu, 2 Nov 2023 17:07:38 -0500 Subject: [PATCH 3/4] fix: bug completing commits (#299) * fix: bug completing commits * fix: remove await * prepareXRecords to non-async * feat: switch SDK to API call --- .changeset/funny-clouds-dress.md | 5 ++++ package-lock.json | 10 ++++---- plugins/record-hook/package.json | 2 +- plugins/record-hook/src/RecordHook.ts | 33 +++++++++++++++++---------- 4 files changed, 32 insertions(+), 18 deletions(-) create mode 100644 .changeset/funny-clouds-dress.md diff --git a/.changeset/funny-clouds-dress.md b/.changeset/funny-clouds-dress.md new file mode 100644 index 000000000..c96f9c8e0 --- /dev/null +++ b/.changeset/funny-clouds-dress.md @@ -0,0 +1,5 @@ +--- +'@flatfile/plugin-record-hook': patch +--- + +Bug fix completing commits diff --git a/package-lock.json b/package-lock.json index e42ded02c..286016e5f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -782,9 +782,9 @@ } }, "node_modules/@flatfile/api": { - "version": "1.5.33", - "resolved": "https://registry.npmjs.org/@flatfile/api/-/api-1.5.33.tgz", - "integrity": "sha512-KP7NzP0q7ekXDWGl4yANCPpZmovNTJT2r3oafvlIu5rYvWwe/QDJZkmOsMOax7kIeH5tvEx0wW6Z7xW34JTZPg==", + "version": "1.5.34", + "resolved": "https://registry.npmjs.org/@flatfile/api/-/api-1.5.34.tgz", + "integrity": "sha512-qR7Re4o6N+Vb0Y4v4BkF36s6YghgohJFqSpZZvY2oVCL4nGbMU2S4XFTOfdAdTyPiG2JXA76UVoPfwSi1LiEZw==", "dependencies": { "@flatfile/cross-env-config": "0.0.4", "@types/pako": "2.0.1", @@ -10679,10 +10679,10 @@ }, "plugins/record-hook": { "name": "@flatfile/plugin-record-hook", - "version": "1.1.8", + "version": "1.1.9", "license": "ISC", "dependencies": { - "@flatfile/api": "^1.5.33", + "@flatfile/api": "^1.5.34", "@flatfile/hooks": "^1.3.1", "@flatfile/listener": "^0.3.15", "@flatfile/util-common": "^0.2.1", diff --git a/plugins/record-hook/package.json b/plugins/record-hook/package.json index 33acfa129..43f59bfea 100644 --- a/plugins/record-hook/package.json +++ b/plugins/record-hook/package.json @@ -32,7 +32,7 @@ }, "license": "ISC", "dependencies": { - "@flatfile/api": "^1.5.33", + "@flatfile/api": "^1.5.34", "@flatfile/hooks": "^1.3.1", "@flatfile/listener": "^0.3.15", "@flatfile/util-common": "^0.2.1", diff --git a/plugins/record-hook/src/RecordHook.ts b/plugins/record-hook/src/RecordHook.ts index 6ff3c5210..e767effdb 100644 --- a/plugins/record-hook/src/RecordHook.ts +++ b/plugins/record-hook/src/RecordHook.ts @@ -1,4 +1,4 @@ -import api, { Flatfile } from '@flatfile/api' +import { Flatfile } from '@flatfile/api' import { FlatfileRecord, FlatfileRecords } from '@flatfile/hooks' import { FlatfileEvent } from '@flatfile/listener' import { asyncBatch } from '@flatfile/util-common' @@ -51,13 +51,18 @@ export const BulkRecordHook = async ( ) => any | Promise, options: BulkRecordHookOptions = {} ) => { - const { versionId, workbookId } = event.context - const { data: workbook } = await api.workbooks.get(workbookId) - const trackChanges = workbook.settings?.trackChanges ?? false + const { versionId } = event.context + const { trackChanges } = event.payload + const completeCommit = async () => { if (trackChanges) { try { - await api.commits.complete(versionId) + await event.fetch(`v1/commits/${versionId}/complete`, { + method: 'POST', + }) + if (options.debug) { + console.log('Commit completed successfully') + } } catch (e) { console.log(`Error completing commit: ${e}`) } @@ -65,10 +70,14 @@ export const BulkRecordHook = async ( } const fetchData = async () => { - const data = await event.data - return data.records && data.records.length - ? prepareXRecords(data.records) - : undefined + try { + const data = await event.data + return data.records && data.records.length + ? prepareXRecords(data.records) + : undefined + } catch (e) { + console.log(`Error fetching records: ${e}`) + } } try { @@ -81,7 +90,7 @@ export const BulkRecordHook = async ( if (options.debug) { console.log('No records to process') } - completeCommit() + await completeCommit() return } @@ -101,7 +110,7 @@ export const BulkRecordHook = async ( if (options.debug) { console.log('No records modified') } - completeCommit() + await completeCommit() return } @@ -147,7 +156,7 @@ function deepEqual(obj1, obj2) { return true } -const prepareXRecords = async (records: any): Promise> => { +const prepareXRecords = (records: any): FlatfileRecords => { const clearedMessages: Flatfile.Record_[] = records.map( (record: { values: { [x: string]: { messages: never[] } } }) => { // clear existing cell validation messages From 28900f834ae8f77128f86e483bc80809205853b7 Mon Sep 17 00:00:00 2001 From: Flatfile Bot <62193415+flatfiledocker@users.noreply.github.com> Date: Thu, 2 Nov 2023 15:10:14 -0700 Subject: [PATCH 4/4] chore: update versions (#300) Co-authored-by: github-actions[bot] --- .changeset/funny-clouds-dress.md | 5 ----- plugins/record-hook/CHANGELOG.md | 6 ++++++ plugins/record-hook/package.json | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) delete mode 100644 .changeset/funny-clouds-dress.md diff --git a/.changeset/funny-clouds-dress.md b/.changeset/funny-clouds-dress.md deleted file mode 100644 index c96f9c8e0..000000000 --- a/.changeset/funny-clouds-dress.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'@flatfile/plugin-record-hook': patch ---- - -Bug fix completing commits diff --git a/plugins/record-hook/CHANGELOG.md b/plugins/record-hook/CHANGELOG.md index 6147d640a..6307390be 100644 --- a/plugins/record-hook/CHANGELOG.md +++ b/plugins/record-hook/CHANGELOG.md @@ -1,5 +1,11 @@ # @flatfile/plugin-record-hook +## 1.1.10 + +### Patch Changes + +- 2e2a201: Bug fix completing commits + ## 1.1.9 ### Patch Changes diff --git a/plugins/record-hook/package.json b/plugins/record-hook/package.json index 43f59bfea..d7a1b113f 100644 --- a/plugins/record-hook/package.json +++ b/plugins/record-hook/package.json @@ -1,6 +1,6 @@ { "name": "@flatfile/plugin-record-hook", - "version": "1.1.9", + "version": "1.1.10", "description": "A plugin for writing custom record-level hooks in Flatfile.", "registryMetadata": { "category": "records"