diff --git a/plugins/webhook-egress/src/webhook.egress.spec.ts b/plugins/webhook-egress/src/webhook.egress.spec.ts index 732b38384..5d580baf0 100644 --- a/plugins/webhook-egress/src/webhook.egress.spec.ts +++ b/plugins/webhook-egress/src/webhook.egress.spec.ts @@ -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', @@ -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.` ) }) @@ -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 () => { @@ -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') }) }) }) diff --git a/plugins/webhook-egress/src/webhook.egress.ts b/plugins/webhook-egress/src/webhook.egress.ts index a0cf66772..ce5d8a1b1 100644 --- a/plugins/webhook-egress/src/webhook.egress.ts +++ b/plugins/webhook-egress/src/webhook.egress.ts @@ -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) => { @@ -17,11 +16,14 @@ 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 = [] + for (const [_, sheet] of workbookSheets.entries()) { + const { data: records } = await api.records.get(sheet.id) sheets.push({ - ...element, + ...sheet, ...records, }) } @@ -29,8 +31,7 @@ export function webhookEgress(job: string, webhookUrl?: string) { 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', @@ -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: { diff --git a/plugins/webhook-event-forwarder/src/forward.webhook.ts b/plugins/webhook-event-forwarder/src/forward.webhook.ts index d6a7e8b92..639977cc9 100644 --- a/plugins/webhook-event-forwarder/src/forward.webhook.ts +++ b/plugins/webhook-event-forwarder/src/forward.webhook.ts @@ -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, options?: { debug?: boolean @@ -10,13 +10,7 @@ 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', @@ -24,7 +18,9 @@ export function webhookEventForward( 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') diff --git a/utils/response-rejection/src/index.ts b/utils/response-rejection/src/index.ts index 59efa4d5e..14a8505b0 100644 --- a/utils/response-rejection/src/index.ts +++ b/utils/response-rejection/src/index.ts @@ -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 { @@ -21,17 +22,17 @@ export interface RecordRejections { export async function responseRejectionHandler( responseRejection: RejectionResponse ): Promise { - 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 { @@ -39,8 +40,8 @@ export async function responseRejectionHandler( buttonText: 'Close', heading: totalRejectedRecords > 0 ? 'Rejected Records' : 'Success!', acknowledge: true, - ...(next && !responseRejection.deleteSubmitted && { next }), - message, + ...(next && !deleteSubmitted && { next }), + message: message ?? getMessage(totalRejectedRecords), }, } }