From 97981f9a7b03dac7730b68b13daa1844f0f8f751 Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Tue, 3 Jan 2023 20:55:53 +0800 Subject: [PATCH] Updates to handle new import file format --- packages/import-handler/src/index.ts | 37 +++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/packages/import-handler/src/index.ts b/packages/import-handler/src/index.ts index b9b536385..65ff113ed 100644 --- a/packages/import-handler/src/index.ts +++ b/packages/import-handler/src/index.ts @@ -4,8 +4,10 @@ import { } from '@google-cloud/functions-framework/build/src/functions' import { Storage } from '@google-cloud/storage' import { PubSub } from '@google-cloud/pubsub' -import { importCsv } from './csv' +import { importCsv, UrlHandler } from './csv' import * as path from 'path' +import { importMatterHistory } from './matterHistory' +import { Stream } from 'node:stream' const pubsub = new PubSub() const storage = new Storage() @@ -17,6 +19,11 @@ interface StorageEventData { contentType: string } +type importHandlerFunc = ( + stream: Stream, + handler: UrlHandler +) => Promise + const shouldHandle = (data: StorageEventData, ctx: CloudFunctionsContext) => { console.log('deciding to handle', ctx, data) if (ctx.eventType !== 'google.storage.object.finalize') { @@ -45,6 +52,17 @@ const importURL = async ( }) } +const handlerForFile = (name: string): importHandlerFunc | undefined => { + const fileName = path.parse(name).name + if (fileName.startsWith('MATTER')) { + return importMatterHistory + } else if (fileName.startsWith('URL_LIST')) { + return importCsv + } + + return undefined +} + export const importHandler: EventFunction = async (event, context) => { const data = event as StorageEventData const ctx = context as CloudFunctionsContext @@ -57,10 +75,21 @@ export const importHandler: EventFunction = async (event, context) => { .file(data.name) .createReadStream() - await importCsv(stream, async (url): Promise => { + const handler = handlerForFile(data.name) + if (!handler) { + console.log('no handler for file:', data.name) + return + } + + await handler(stream, async (url): Promise => { try { - // Imports are stored in the format imports//.extension - const userId = path.parse(data.name).name + // Imports are stored in the format imports//-.csv + const group = path.parse(data.name).name.match(/(?<=-).*/gi) + if (!group || group.length < 1) { + console.log('could not match file pattern: ', data.name) + return + } + const userId = [...group][0] const result = await importURL(userId, url, 'csv-importer') console.log('import url result', result) } catch (err) {