From 687d126bd37b416771b4e36c91bc6a8f6b1725c5 Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Thu, 5 Jan 2023 11:49:16 +0800 Subject: [PATCH] Fix the task URLs, send an email if no URLs are parsed for import --- packages/import-handler/src/index.ts | 42 +++++++++++++++++++--------- packages/import-handler/src/task.ts | 11 +++++++- 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/packages/import-handler/src/index.ts b/packages/import-handler/src/index.ts index aa0dcb168..4012827cc 100644 --- a/packages/import-handler/src/index.ts +++ b/packages/import-handler/src/index.ts @@ -8,9 +8,8 @@ import * as path from 'path' import { importMatterHistory } from './matterHistory' import { Stream } from 'node:stream' import { v4 as uuid } from 'uuid' -import { createCloudTask } from './task' +import { CONTENT_FETCH_URL, createCloudTask, EMAIL_USER_URL } from './task' -import axios, { AxiosResponse } from 'axios' import { promisify } from 'util' import * as jwt from 'jsonwebtoken' @@ -48,7 +47,7 @@ const importURL = async ( url: URL, source: string ): Promise => { - return createCloudTask({ + return createCloudTask(CONTENT_FETCH_URL, { userId, source, url: url.toString(), @@ -56,7 +55,7 @@ const importURL = async ( }) } -const importCompletedTask = async (userId: string, urlsEnqueued: number) => { +const createEmailCloudTask = async (userId: string, payload: unknown) => { if (!process.env.JWT_SECRET) { throw 'Envrionment not setup correctly' } @@ -70,14 +69,25 @@ const importCompletedTask = async (userId: string, urlsEnqueued: number) => { Authorization: authToken, } - return createCloudTask( - { - userId, - subject: 'Your Omnivore import has completed processing', - body: `${urlsEnqueued} URLs have been pcoessed and should be available in your library.`, - }, - headers - ) + return createCloudTask(EMAIL_USER_URL, payload, headers) +} + +const sendImportFailedEmail = async (userId: string) => { + return createEmailCloudTask(userId, { + subject: 'Your Omnivore import failed.', + body: `There was an error importing your file. Please ensure you uploaded the correct file type, if you need help, please email feedback@omnivore.app`, + }) +} + +const sendImportCompletedEmail = async ( + userId: string, + urlsEnqueued: number, + urlsFailed: number +) => { + return createEmailCloudTask(userId, { + subject: 'Your Omnivore import has completed processing', + body: `${urlsEnqueued} URLs have been pcoessed and should be available in your library. ${urlsFailed} URLs failed to be parsed.`, + }) } const handlerForFile = (name: string): importHandlerFunc | undefined => { @@ -121,6 +131,7 @@ export const importHandler: EventFunction = async (event, context) => { return } + let countFailed = 0 let countImported = 0 await handler(stream, async (url): Promise => { try { @@ -130,9 +141,14 @@ export const importHandler: EventFunction = async (event, context) => { countImported = countImported + 1 } catch (err) { console.log('error importing url', err) + countFailed = countFailed + 1 } }) - await importCompletedTask(userId, countImported) + if (countImported < 1) { + await sendImportFailedEmail(userId) + } else { + await sendImportCompletedEmail(userId, countImported, countFailed) + } } } diff --git a/packages/import-handler/src/task.ts b/packages/import-handler/src/task.ts index da6c971b3..e63be6bd3 100644 --- a/packages/import-handler/src/task.ts +++ b/packages/import-handler/src/task.ts @@ -3,14 +3,23 @@ import { CloudTasksClient, protos } from '@google-cloud/tasks' const cloudTask = new CloudTasksClient() +export const EMAIL_USER_URL = (() => { + if (!process.env.INTERNAL_SVC_ENDPOINT) { + throw `Environment not configured correctly, no SVC endpoint` + } + return (process.env.INTERNAL_SVC_ENDPOINT ?? '') + '/api/user/email' +})() + +export const CONTENT_FETCH_URL = process.env.CONTENT_FETCH_GCF_URL + export const createCloudTask = async ( + taskHandlerUrl: string | undefined, payload: unknown, requestHeaders?: Record ) => { const queue = 'omnivore-import-queue' const location = process.env.GCP_LOCATION const project = process.env.GCP_PROJECT_ID - const taskHandlerUrl = process.env.CONTENT_FETCH_GCF_URL if (!project || !location || !queue || !taskHandlerUrl) { throw `Environment not configured: ${project}, ${location}, ${queue}, ${taskHandlerUrl}`