Change order of ctx arguments

This commit is contained in:
Jackson Harper 2023-01-11 22:55:04 +08:00
parent 04375d6856
commit 0bc87deba0
5 changed files with 14 additions and 21 deletions

View file

@ -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) {

View file

@ -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<void>
type importHandlerFunc = (ctx: ImportContext, stream: Stream) => Promise<void>
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)
}
}
}

View file

@ -21,8 +21,8 @@ import { ImportContext } from '.'
export type UrlHandler = (url: URL) => Promise<void>
export const importMatterHistoryCsv = async (
stream: Stream,
ctx: ImportContext
ctx: ImportContext,
stream: Stream
): Promise<void> => {
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<void> => {
const archiveDir = await unarchive(stream)

View file

@ -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([

View file

@ -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([