Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove sheetId filtering #221

Merged
merged 2 commits into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/warm-cherries-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@flatfile/plugin-autocast': patch
---

Fix sheet filtering options to require sheetSlug.
84 changes: 41 additions & 43 deletions plugins/autocast/src/autocast.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,59 @@ import { FlatfileRecord, TPrimitive } from '@flatfile/hooks'
import { logInfo } from '@flatfile/util-common'

export function autocast(
sheetFilter: { sheetSlug?: string; sheetId?: string },
sheetSlug: string,
fieldFilters?: string[],
options?: {
chunkSize?: number
parallel?: number
}
) {
if (!sheetFilter.sheetSlug && !sheetFilter.sheetId) {
throw new Error('You must provide either a sheetSlug or sheetId')
}
if (sheetFilter.sheetSlug && sheetFilter.sheetId) {
throw new Error('You must provide either a sheetSlug or sheetId, not both')
}
return async (listener: FlatfileListener) => {
listener.on('commit:created', sheetFilter, async (event: FlatfileEvent) => {
return await BulkRecordHook(
event,
async (records: FlatfileRecord[]) => {
const sheetId = event.context.sheetId
const sheet = await api.sheets.get(sheetId)
if (!sheet) {
logInfo('@flatfile/plugin-autocast', 'Failed to fetch sheet')
return
}
listener.on(
'commit:created',
{ sheetSlug },
async (event: FlatfileEvent) => {
return await BulkRecordHook(
event,
async (records: FlatfileRecord[]) => {
const sheetId = event.context.sheetId
const sheet = await api.sheets.get(sheetId)
if (!sheet) {
logInfo('@flatfile/plugin-autocast', 'Failed to fetch sheet')
return
}

const castableFields = sheet.data.config.fields.filter((field) =>
fieldFilters
? fieldFilters.includes(field.key)
: field.type !== 'string'
)
records.forEach((record) => {
castableFields.forEach((field) => {
const originalValue = record.get(field.key)
const caster = CASTING_FUNCTIONS[field.type]
const castableFields = sheet.data.config.fields.filter((field) =>
fieldFilters
? fieldFilters.includes(field.key)
: field.type !== 'string'
)
records.forEach((record) => {
castableFields.forEach((field) => {
const originalValue = record.get(field.key)
const caster = CASTING_FUNCTIONS[field.type]

if (
originalValue &&
caster &&
typeof originalValue !== field.type
) {
record.computeIfPresent(field.key, caster)
if (
originalValue &&
caster &&
typeof originalValue !== field.type
) {
record.computeIfPresent(field.key, caster)

if (originalValue === record.get(field.key)) {
record.addError(
field.key,
`Failed to cast '${originalValue}' to '${field.type}'`
)
if (originalValue === record.get(field.key)) {
record.addError(
field.key,
`Failed to cast '${originalValue}' to '${field.type}'`
)
}
}
}
})
})
})
},
options
)
})
},
options
)
}
)
}
}

Expand Down