Skip to content

Commit

Permalink
feat: minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
carlbrugger committed Apr 3, 2024
1 parent a649fc0 commit bc68229
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 40 deletions.
14 changes: 7 additions & 7 deletions plugins/webhook-egress/src/webhook.egress.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('webhookEgress() e2e', () => {
'notes',
])
workbookId = workbook.id
sheetId = workbook.sheets[0].id
sheetId = workbook.sheets![0].id
await createRecords(sheetId, [
{
name: 'John Doe',
Expand Down Expand Up @@ -74,8 +74,8 @@ describe('webhookEgress() e2e', () => {
await listener.waitFor('job:ready', 1, 'workbook:egressTestSuccess')

const response = await api.jobs.get(successfulJobId)
expect(response.data.outcome.message).toEqual(
`Data was successfully submitted to the provided webhook. Go check it out at example.com.`
expect(response.data.outcome!.message).toEqual(
`Data was successfully submitted to example.com.`
)
})

Expand Down Expand Up @@ -155,10 +155,10 @@ describe('webhookEgress() e2e', () => {
await listener.waitFor('job:ready', 1, 'workbook:egressTestSuccess')

const response = await api.jobs.get(successfulJobId)
expect(response.data.outcome.message).toEqual(
expect(response.data.outcome!.message).toEqual(
'The data has been successfully submitted without any rejections. This task is now complete.'
)
expect(response.data.outcome.heading).toEqual('Success!')
expect(response.data.outcome!.heading).toEqual('Success!')
})

it('returns rejections', async () => {
Expand Down Expand Up @@ -206,10 +206,10 @@ describe('webhookEgress() e2e', () => {
await listener.waitFor('job:ready', 1, 'workbook:egressTestSuccess')

const response = await api.jobs.get(successfulJobId)
expect(response.data.outcome.message).toEqual(
expect(response.data.outcome!.message).toEqual(
'During the data submission process, 1 records were rejected. Please review and correct these records before resubmitting.'
)
expect(response.data.outcome.heading).toEqual('Rejected Records')
expect(response.data.outcome!.heading).toEqual('Rejected Records')
})
})
})
29 changes: 15 additions & 14 deletions plugins/webhook-egress/src/webhook.egress.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import type { Flatfile } from '@flatfile/api'
import api from '@flatfile/api'
import { FlatfileListener } from '@flatfile/listener'
import type { FlatfileListener } from '@flatfile/listener'
import { jobHandler } from '@flatfile/plugin-job-handler'
import { logError } from '@flatfile/util-common'
import {
RejectionResponse,
responseRejectionHandler,
} from '@flatfile/util-response-rejection'
import type { RejectionResponse } from '@flatfile/util-response-rejection'
import { responseRejectionHandler } from '@flatfile/util-response-rejection'

export function webhookEgress(job: string, webhookUrl?: string) {
export function webhookEgress(job: string, url: string) {
return function (listener: FlatfileListener) {
listener.use(
jobHandler(job, async (event, tick) => {
Expand All @@ -17,20 +16,22 @@ export function webhookEgress(job: string, webhookUrl?: string) {

await tick(30, 'Getting workbook data')

const sheets = []
for (const [_, element] of workbookSheets.entries()) {
const { data: records } = await api.records.get(element.id)
interface EnhancedSheet extends Flatfile.Sheet {
records: Flatfile.RecordsWithLinks
}
const sheets: Array<EnhancedSheet> = []
for (const [_, sheet] of workbookSheets.entries()) {
const { data: records } = await api.records.get(sheet.id)
sheets.push({
...element,
...sheet,
...records,
})
}

await tick(60, 'Posting data to webhook')

try {
const webhookReceiver = webhookUrl || process.env.WEBHOOK_SITE_URL
const response = await fetch(webhookReceiver, {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand All @@ -53,13 +54,13 @@ export function webhookEgress(job: string, webhookUrl?: string) {

return {
outcome: {
message: `Data was successfully submitted to the provided webhook. Go check it out at ${webhookReceiver}.`,
message: `Data was successfully submitted to ${url}.`,
},
}
} else {
logError(
'@flatfile/plugin-webhook-egress',
`Failed to submit data to ${webhookReceiver}. Status: ${response.status} ${response.statusText}`
`Failed to submit data to ${url}. Status: ${response.status} ${response.statusText}`
)
return {
outcome: {
Expand Down
16 changes: 6 additions & 10 deletions plugins/webhook-event-forwarder/src/forward.webhook.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FlatfileEvent, FlatfileListener } from '@flatfile/listener'
import type { FlatfileEvent, FlatfileListener } from '@flatfile/listener'

export function webhookEventForward(
url?: string,
url: string,
callback?: (data: any, event: FlatfileEvent) => Promise<any> | any,
options?: {
debug?: boolean
Expand All @@ -10,21 +10,17 @@ export function webhookEventForward(
return async (listener: FlatfileListener) => {
return listener.on('**', async (event) => {
try {
const apiUrl = url || process.env.WEBHOOK_SITE_URL

if (typeof apiUrl === 'undefined') {
throw new Error('No url provided')
}

const response = await fetch(apiUrl, {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(event),
})

if (response.status !== 200) throw new Error('Error forwarding webhook')
if (response.status !== 200) {
throw new Error('Error forwarding webhook')
}

const contentType = response.headers.get('content-type')
const isJson = contentType && contentType.includes('application/json')
Expand Down
19 changes: 10 additions & 9 deletions utils/response-rejection/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import api, { Flatfile } from '@flatfile/api'
import type { Flatfile } from '@flatfile/api'
import api from '@flatfile/api'
import { processRecords } from '@flatfile/util-common'

export interface RejectionResponse {
Expand All @@ -21,26 +22,26 @@ export interface RecordRejections {
export async function responseRejectionHandler(
responseRejection: RejectionResponse
): Promise<Flatfile.JobCompleteDetails> {
let totalRejectedRecords = 0
const { deleteSubmitted, message, sheets } = responseRejection

for (const sheet of responseRejection.sheets || []) {
const count = await updateSheet(sheet, responseRejection.deleteSubmitted)
let totalRejectedRecords = 0
for (const sheet of sheets || []) {
const count = await updateSheet(sheet, deleteSubmitted ?? false)
totalRejectedRecords += count
}

const message = responseRejection.message ?? getMessage(totalRejectedRecords)
let next
if (!responseRejection.deleteSubmitted && totalRejectedRecords > 0) {
next = getNext(totalRejectedRecords, responseRejection.sheets[0].sheetId)
if (!deleteSubmitted && totalRejectedRecords > 0) {
next = getNext(totalRejectedRecords, sheets[0].sheetId)
}

return {
outcome: {
buttonText: 'Close',
heading: totalRejectedRecords > 0 ? 'Rejected Records' : 'Success!',
acknowledge: true,
...(next && !responseRejection.deleteSubmitted && { next }),
message,
...(next && !deleteSubmitted && { next }),
message: message ?? getMessage(totalRejectedRecords),
},
}
}
Expand Down

0 comments on commit bc68229

Please sign in to comment.