-
Notifications
You must be signed in to change notification settings - Fork 3
/
workbook_submit.ts
77 lines (70 loc) · 2.26 KB
/
workbook_submit.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import api from '@flatfile/api'
import { FlatfileEvent, FlatfileListener } from '@flatfile/listener'
import {
RejectionResponse,
responseRejectionHandler,
} from '@flatfile/util-response-rejection'
export default function flatfileEventListener(listener: FlatfileListener) {
listener.on(
'job:ready',
{ job: 'workbook:submitActionFg' },
async (event: FlatfileEvent) => {
const {jobId, workbookId} = event.context
const { data: workbook } = await api.workbooks.get(workbookId)
const { data: workbookSheets } = await api.sheets.list({ workbookId })
const sheets = []
for (const [_, element] of workbookSheets.entries()) {
const { data: records } = await api.records.get(element.id)
sheets.push({
...element,
...records,
})
}
try {
await api.jobs.ack(jobId, {
info: 'Starting job to submit action to webhook.site',
progress: 10,
})
// TODO: place your webhook url here
const webhookReceiver = 'https://webhook.site/...'
const response = await fetch(webhookReceiver, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
workbook: {
...workbook,
sheets,
},
}),
})
if (response.status === 200) {
const responseData = await response.json()
const rejections: RejectionResponse = responseData.rejections
if (rejections) {
const outcome = await responseRejectionHandler(rejections)
await api.jobs.complete(jobId, outcome)
}
await api.jobs.complete(jobId, {
outcome: {
message: `Data was successfully submitted to webhook.site. Go check it out at ${webhookReceiver}.`,
},
})
} else {
throw new Error('Failed to submit data to webhook.site')
}
return
} catch (error) {
console.error(error)
await api.jobs.fail(jobId, {
outcome: {
message:
"This job failed probably because it couldn't find the webhook.site URL.",
},
})
return
}
}
)
}