Skip to content

Commit

Permalink
Merge branch 'main' into feat/json-schema-implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
carlbrugger committed Nov 3, 2023
2 parents e577938 + 28900f8 commit 0c4165a
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 30 deletions.
5 changes: 0 additions & 5 deletions .changeset/afraid-melons-sniff.md

This file was deleted.

10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions plugins/record-hook/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# @flatfile/plugin-record-hook

## 1.1.10

### Patch Changes

- 2e2a201: Bug fix completing commits

## 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
Expand Down
4 changes: 2 additions & 2 deletions plugins/record-hook/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@flatfile/plugin-record-hook",
"version": "1.1.8",
"version": "1.1.10",
"description": "A plugin for writing custom record-level hooks in Flatfile.",
"registryMetadata": {
"category": "records"
Expand Down Expand Up @@ -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",
Expand Down
68 changes: 50 additions & 18 deletions plugins/record-hook/src/RecordHook.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -51,24 +51,33 @@ export const BulkRecordHook = async (
) => any | Promise<any>,
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}`)
}
}
}

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 {
Expand All @@ -81,21 +90,27 @@ export const BulkRecordHook = async (
if (options.debug) {
console.log('No records to process')
}
completeCommit()
await completeCommit()
return
}

await event.cache.init<FlatfileRecords<any>>('originalRecords', fetchData)

// Execute client-defined data hooks
await asyncBatch(batch.records, handler, options, event)

event.afterAll(async () => {
const batch = event.cache.get<FlatfileRecords<any>>('records')
const modifiedRecords = batch.records.filter(hasRecordChanges)
const originalRecords =
event.cache.get<FlatfileRecords<any>>('originalRecords')
const modifiedRecords = batch.records.filter((record) =>
hasRecordChanges(record, originalRecords.records)
)
if (!modifiedRecords || modifiedRecords.length === 0) {
if (options.debug) {
console.log('No records modified')
}
completeCommit()
await completeCommit()
return
}

Expand All @@ -116,15 +131,32 @@ 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<FlatfileRecords<any>> => {
const prepareXRecords = (records: any): FlatfileRecords<any> => {
const clearedMessages: Flatfile.Record_[] = records.map(
(record: { values: { [x: string]: { messages: never[] } } }) => {
// clear existing cell validation messages
Expand Down

0 comments on commit 0c4165a

Please sign in to comment.