diff --git a/packages/inbound-email-handler/src/index.ts b/packages/inbound-email-handler/src/index.ts index 055a18778..6f4caa502 100644 --- a/packages/inbound-email-handler/src/index.ts +++ b/packages/inbound-email-handler/src/index.ts @@ -75,8 +75,8 @@ export const inboundEmailHandler = Sentry.GCPFunction.wrapHttpFunction( // check if it is a forwarding confirmation email or newsletter const newsletterHandler = getNewsletterHandler(rawUrl, from) - if (newsletterHandler) { - try { + try { + if (newsletterHandler) { console.log('handleNewsletter', from, recipientAddress) await newsletterHandler.handleNewsletter( recipientAddress, @@ -85,13 +85,22 @@ export const inboundEmailHandler = Sentry.GCPFunction.wrapHttpFunction( subject, from ) - } catch (error) { - console.log( - 'error handling newsletter, will forward.', - from, - recipientAddress, - subject - ) + } else { + console.log('non-newsletter email from:', from, recipientAddress) + + if (isConfirmationEmail(from)) { + console.log('handleConfirmation', from) + await handleConfirmation(recipientAddress, subject) + } else if (pdfAttachment) { + console.log('handle PDF attachment', from, recipientAddress) + await handlePdfAttachment( + recipientAddress, + pdfAttachmentName, + pdfAttachment, + subject + ) + } + // queue non-newsletter emails await pubsub.topic(NON_NEWSLETTER_EMAIL_TOPIC).publishMessage({ json: { @@ -103,23 +112,14 @@ export const inboundEmailHandler = Sentry.GCPFunction.wrapHttpFunction( }, }) } - } else { - console.log('non-newsletter email from:', from, recipientAddress) - - if (isConfirmationEmail(from)) { - console.log('handleConfirmation', from, recipientAddress) - await handleConfirmation(recipientAddress, subject) - } else if (pdfAttachment) { - console.log('handle PDF attachment', from, recipientAddress) - await handlePdfAttachment( - recipientAddress, - pdfAttachmentName, - pdfAttachment, - subject - ) - } - - // queue non-newsletter emails + } catch (error) { + console.log( + 'error handling emails, will forward.', + from, + recipientAddress, + subject + ) + // queue error emails await pubsub.topic(NON_NEWSLETTER_EMAIL_TOPIC).publishMessage({ json: { from: from, diff --git a/packages/inbound-email-handler/src/newsletter.ts b/packages/inbound-email-handler/src/newsletter.ts index 98b8d84e9..858bdcf97 100644 --- a/packages/inbound-email-handler/src/newsletter.ts +++ b/packages/inbound-email-handler/src/newsletter.ts @@ -7,7 +7,7 @@ const EMAIL_CONFIRMATION_CODE_RECEIVED_TOPIC = 'emailConfirmationCodeReceived' const EMAIL_FORWARDING_SENDER_ADDRESSES = [ 'Gmail Team ', ] -const CONFIRMATION_CODE_PATTERN = /^\\(#\\d+\\)/ +const CONFIRMATION_CODE_PATTERN = /^\(#\d+\)/ export class NewsletterHandler { protected senderRegex = /NEWSLETTER_SENDER_REGEX/ @@ -70,16 +70,9 @@ export class NewsletterHandler { } export const handleConfirmation = async (email: string, subject: string) => { - console.log('confirmation email') - - let confirmationCode = '' - const matches = subject.match(CONFIRMATION_CODE_PATTERN) - if (matches) { - // get the number code only - // e.g. (#123456) => 123456 - confirmationCode = matches[0].slice(2, -1) - } + console.log('confirmation email', email, subject) + const confirmationCode = getConfirmationCode(subject) if (!email || !confirmationCode) { console.log( 'confirmation email error, user email:', @@ -94,6 +87,20 @@ export const handleConfirmation = async (email: string, subject: string) => { return publishMessage(EMAIL_CONFIRMATION_CODE_RECEIVED_TOPIC, message) } +export const getConfirmationCode = (subject: string): string | undefined => { + const matches = subject.match(CONFIRMATION_CODE_PATTERN) + if (matches) { + // get the number code only + // e.g. (#123456) => 123456 + return matches[0].slice(2, -1) + } + return undefined +} + +export const isConfirmationEmail = (from: string): boolean => { + return EMAIL_FORWARDING_SENDER_ADDRESSES.includes(from) +} + const publishMessage = async ( topic: string, message: Record @@ -106,7 +113,3 @@ const publishMessage = async ( return undefined }) } - -export const isConfirmationEmail = (from: string): boolean => { - return EMAIL_FORWARDING_SENDER_ADDRESSES.includes(from) -} diff --git a/packages/inbound-email-handler/test/newsletter.test.ts b/packages/inbound-email-handler/test/newsletter.test.ts index bda334237..46f427421 100644 --- a/packages/inbound-email-handler/test/newsletter.test.ts +++ b/packages/inbound-email-handler/test/newsletter.test.ts @@ -1,5 +1,9 @@ import { expect } from 'chai' -import { isConfirmationEmail, NewsletterHandler } from '../src/newsletter' +import { + getConfirmationCode, + isConfirmationEmail, + NewsletterHandler, +} from '../src/newsletter' import { SubstackHandler } from '../src/substack-handler' import { AxiosHandler } from '../src/axios-handler' import { BloombergHandler } from '../src/bloomberg-handler' @@ -14,6 +18,15 @@ describe('Confirmation email test', () => { expect(isConfirmationEmail(from)).to.be.true }) }) + + describe('#getConfirmationCode()', () => { + it('returns the confirmation code from the email', () => { + const code = '593781109' + const subject = `(#${code}) Gmail Forwarding Confirmation - Receive Mail from sam@omnivore.com` + + expect(getConfirmationCode(subject)).to.equal(code) + }) + }) }) describe('Newsletter email test', () => {