diff --git a/.changeset/stupid-boats-explain.md b/.changeset/stupid-boats-explain.md new file mode 100644 index 000000000..997dce2d4 --- /dev/null +++ b/.changeset/stupid-boats-explain.md @@ -0,0 +1,5 @@ +--- +'@flatfile/plugin-record-hook': patch +--- + +This update handles caching of record messages. diff --git a/.env.defaults b/.env.defaults index 859b766a5..a7489f5e2 100644 --- a/.env.defaults +++ b/.env.defaults @@ -6,4 +6,4 @@ FLATFILE_ENVIRONMENT_ID=us_env_aeCyg5u9 FLATFILE_BEARER_TOKEN="${FLATFILE_API_KEY}" AGENT_INTERNAL_URL="https://platform.flatfile.com/api" -WEBHOOK_SITE_URL="https://webhook.site/077ab8ee-8570-48b0-82d7-f7561ae75dc2" \ No newline at end of file +WEBHOOK_SITE_URL="https://webhook.site/dea90400-e014-479b-9258-410d0998b0cf" \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 9e9bbc4c8..4c366eaff 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13017,7 +13017,7 @@ }, "plugins/json-schema": { "name": "@flatfile/plugin-convert-json-schema", - "version": "0.0.4", + "version": "0.1.0", "license": "ISC", "dependencies": { "@flatfile/api": "^1.5.37", @@ -13215,11 +13215,11 @@ }, "plugins/yaml-schema": { "name": "@flatfile/plugin-convert-yaml-schema", - "version": "0.0.2", + "version": "0.0.3", "license": "ISC", "dependencies": { "@flatfile/api": "^1.5.33", - "@flatfile/plugin-convert-json-schema": "^0.0.4", + "@flatfile/plugin-convert-json-schema": "^0.1.0", "@flatfile/plugin-json-schema": "^0.0.2", "@flatfile/plugin-space-configure": "^0.1.5", "@flatfile/util-fetch-schema": "^0.0.2", diff --git a/plugins/record-hook/src/RecordHook.ts b/plugins/record-hook/src/RecordHook.ts index fbd5f4340..f10d1223f 100644 --- a/plugins/record-hook/src/RecordHook.ts +++ b/plugins/record-hook/src/RecordHook.ts @@ -72,21 +72,19 @@ export const BulkRecordHook = async ( const fetchData = async () => { try { const data = await event.data - return data.records && data.records.length - ? prepareXRecords(data.records) - : undefined + return data.records && data.records.length ? data.records : undefined } catch (e) { console.log(`Error fetching records: ${e}`) } } try { - const batch = await event.cache.init>( - 'records', + const originalRecords = await event.cache.init( + 'originalRecords', fetchData ) - if (!batch || batch.records.length === 0) { + if (!originalRecords || originalRecords.length === 0) { if (options.debug) { console.log('No records to process') } @@ -94,17 +92,21 @@ export const BulkRecordHook = async ( return } - await event.cache.init>('originalRecords', fetchData) + const batch = await event.cache.init>( + 'records', + async () => await prepareXRecords(originalRecords) + ) // Execute client-defined data hooks await asyncBatch(batch.records, handler, options, event) event.afterAll(async () => { - const batch = event.cache.get>('records') + const { records } = event.cache.get>('records') + const batch = new RecordTranslator(records).toXRecords() const originalRecords = - event.cache.get>('originalRecords') - const modifiedRecords = batch.records.filter((record) => - hasRecordChanges(record, originalRecords.records) + event.cache.get('originalRecords') + const modifiedRecords = batch.filter((record) => + hasRecordChanges(record, originalRecords) ) if (!modifiedRecords || modifiedRecords.length === 0) { if (options.debug) { @@ -114,12 +116,8 @@ export const BulkRecordHook = async ( return } - const recordsUpdates = new RecordTranslator( - modifiedRecords - ).toXRecords() - try { - return await event.update(recordsUpdates) + return await event.update(modifiedRecords) } catch (e) { console.log(`Error updating records: ${e}`) } @@ -138,16 +136,7 @@ const hasRecordChanges = (record, originalRecords) => { return JSON.stringify(record) !== JSON.stringify(originalRecord) } -const prepareXRecords = (records: any): FlatfileRecords => { - const clearedMessages: Flatfile.Record_[] = records.map( - (record: { values: { [x: string]: { messages: never[] } } }) => { - // clear existing cell validation messages - Object.keys(record.values).forEach((k) => { - record.values[k].messages = [] - }) - return record - } - ) - const fromX = new RecordTranslator(clearedMessages) +const prepareXRecords = async (records: any): Promise> => { + const fromX = new RecordTranslator(records) return fromX.toFlatFileRecords() }