diff --git a/packages/import-handler/src/csv.ts b/packages/import-handler/src/csv.ts index 80f15f72f..c505cc39a 100644 --- a/packages/import-handler/src/csv.ts +++ b/packages/import-handler/src/csv.ts @@ -7,7 +7,7 @@ import { parse } from '@fast-csv/parse' import { Stream } from 'stream' import { ImportContext } from '.' -export const importCsv = async (stream: Stream, ctx: ImportContext) => { +export const importCsv = async (ctx: ImportContext, stream: Stream) => { const parser = parse() stream.pipe(parser) for await (const row of parser) { diff --git a/packages/import-handler/src/index.ts b/packages/import-handler/src/index.ts index a86048f70..dbce23fbc 100644 --- a/packages/import-handler/src/index.ts +++ b/packages/import-handler/src/index.ts @@ -1,7 +1,3 @@ -import { - EventFunction, - CloudFunctionsContext, -} from '@google-cloud/functions-framework/build/src/functions' import { Storage } from '@google-cloud/storage' import { importCsv } from './csv' import * as path from 'path' @@ -44,10 +40,7 @@ export type ImportContext = { contentHandler: ContentHandler } -type importHandlerFunc = ( - stream: Stream, - handler: ImportContext -) => Promise +type importHandlerFunc = (ctx: ImportContext, stream: Stream) => Promise interface StorageEvent { name: string @@ -188,20 +181,20 @@ const handleEvent = async (data: StorageEvent) => { return } - const countFailed = 0 - const countImported = 0 - await handler(stream, { + const ctx = { userId, countImported: 0, countFailed: 0, urlHandler, contentHandler, - }) + } - if (countImported <= 1) { + await handler(ctx, stream) + + if (ctx.countImported <= 1) { await sendImportFailedEmail(userId) } else { - await sendImportCompletedEmail(userId, countImported, countFailed) + await sendImportCompletedEmail(userId, ctx.countImported, ctx.countFailed) } } } diff --git a/packages/import-handler/src/matterHistory.ts b/packages/import-handler/src/matterHistory.ts index f0b314183..7d339d624 100644 --- a/packages/import-handler/src/matterHistory.ts +++ b/packages/import-handler/src/matterHistory.ts @@ -21,8 +21,8 @@ import { ImportContext } from '.' export type UrlHandler = (url: URL) => Promise export const importMatterHistoryCsv = async ( - stream: Stream, - ctx: ImportContext + ctx: ImportContext, + stream: Stream ): Promise => { const parser = parse({ headers: true, @@ -202,8 +202,8 @@ const handleMatterHistoryRow = async ( } export const importMatterArchive = async ( - stream: Stream, - ctx: ImportContext + ctx: ImportContext, + stream: Stream ): Promise => { const archiveDir = await unarchive(stream) diff --git a/packages/import-handler/test/csv/csv.test.ts b/packages/import-handler/test/csv/csv.test.ts index 7dfff7ea8..0f695d69e 100644 --- a/packages/import-handler/test/csv/csv.test.ts +++ b/packages/import-handler/test/csv/csv.test.ts @@ -19,7 +19,7 @@ describe('Load a simple CSV file', () => { return Promise.resolve() } - await importCsv(stream, stub) + await importCsv(stub, stream) expect(stub.countFailed).to.equal(0) expect(stub.countImported).to.equal(2) expect(urls).to.eql([ diff --git a/packages/import-handler/test/matter/matter_importer.test.ts b/packages/import-handler/test/matter/matter_importer.test.ts index e160c7be3..75b2a9cc8 100644 --- a/packages/import-handler/test/matter/matter_importer.test.ts +++ b/packages/import-handler/test/matter/matter_importer.test.ts @@ -23,7 +23,7 @@ describe('Load a simple _matter_history file', () => { return Promise.resolve() } - await importMatterHistoryCsv(stream, stub) + await importMatterHistoryCsv(stub, stream) expect(stub.countFailed).to.equal(0) expect(stub.countImported).to.equal(1) expect(urls).to.eql([