Skip to content

Commit

Permalink
Moves the definition of the send function to resolve issues with kafk…
Browse files Browse the repository at this point in the history
…a related imports in the remix build (nasa-gcn#2590)

Fixes nasa-gcn#2588.
  • Loading branch information
dakota002 authored and Vidushi Sharma committed Oct 13, 2024
1 parent 2daa154 commit f51f32d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 69 deletions.
71 changes: 68 additions & 3 deletions app/routes/circulars/circulars.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
*/
import { tables } from '@architect/functions'
import type { DynamoDB } from '@aws-sdk/client-dynamodb'
import { type DynamoDBDocument, paginateScan } from '@aws-sdk/lib-dynamodb'
import {
type DynamoDBDocument,
paginateQuery,
paginateScan,
} from '@aws-sdk/lib-dynamodb'
import { search as getSearch } from '@nasa-gcn/architect-functions-search'
import {
DynamoDBAutoIncrement,
Expand All @@ -21,6 +25,7 @@ import { type User, getUser } from '../_auth/user.server'
import {
bodyIsValid,
formatAuthor,
formatCircularText,
formatIsValid,
parseEventFromSubject,
subjectIsValid,
Expand All @@ -31,17 +36,18 @@ import type {
CircularChangeRequestKeys,
CircularMetadata,
} from './circulars.lib'
import { sendEmail } from '~/lib/email.server'
import { sendEmail, sendEmailBulk } from '~/lib/email.server'
import { feature, origin } from '~/lib/env.server'
import { closeZendeskTicket } from '~/lib/zendesk.server'
import { send } from '~/table-streams/circulars'

// A type with certain keys required.
type Require<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>

export const submitterGroup = 'gcn.nasa.gov/circular-submitter'
export const moderatorGroup = 'gcn.nasa.gov/circular-moderator'

const fromName = 'GCN Circulars'

const getDynamoDBAutoIncrement = memoizee(
async function () {
const db = await tables()
Expand Down Expand Up @@ -593,3 +599,62 @@ function validateCircular({
if (!(format === undefined || formatIsValid(format)))
throw new Response('format is invalid', { status: 400 })
}

async function getEmails() {
const db = await tables()
const client = db._doc as unknown as DynamoDBDocument
const TableName = db.name('circulars_subscriptions')
const pages = paginateScan(
{ client },
{ AttributesToGet: ['email'], TableName }
)
const emails: string[] = []
for await (const page of pages) {
const newEmails = page.Items?.map(({ email }) => email)
if (newEmails) emails.push(...newEmails)
}
return emails
}

async function getLegacyEmails() {
const db = await tables()
const client = db._doc as unknown as DynamoDBDocument
const TableName = db.name('legacy_users')
const pages = paginateQuery(
{ client },
{
IndexName: 'legacyReceivers',
KeyConditionExpression: 'receive = :receive',
ExpressionAttributeValues: {
':receive': 1,
},
ProjectionExpression: 'email',
TableName,
}
)
const emails: string[] = []
for await (const page of pages) {
const newEmails = page.Items?.map(({ email }) => email)
if (newEmails) emails.push(...newEmails)
}
return emails
}

export async function send(circular: Circular) {
const [emails, legacyEmails] = await Promise.all([
getEmails(),
getLegacyEmails(),
])
const to = [...emails, ...legacyEmails]
await sendEmailBulk({
fromName,
to,
subject: circular.subject,
body: `${formatCircularText(
circular
)}\n\n\nView this GCN Circular online at ${origin}/circulars/${
circular.circularId
}.`,
topic: 'circulars',
})
}
67 changes: 1 addition & 66 deletions app/table-streams/circulars/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,19 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
import { tables } from '@architect/functions'
import { paginateQuery, paginateScan } from '@aws-sdk/lib-dynamodb'
import type { DynamoDBDocument } from '@aws-sdk/lib-dynamodb'
import { search as getSearchClient } from '@nasa-gcn/architect-functions-search'
import { errors } from '@opensearch-project/opensearch'
import type { DynamoDBRecord } from 'aws-lambda'

import { unmarshallTrigger } from '../utils'
import { sendEmailBulk } from '~/lib/email.server'
import { origin } from '~/lib/env.server'
import { send as sendKafka } from '~/lib/kafka.server'
import { createTriggerHandler } from '~/lib/lambdaTrigger.server'
import type { Circular } from '~/routes/circulars/circulars.lib'
import { formatCircularText } from '~/routes/circulars/circulars.lib'
import { send } from '~/routes/circulars/circulars.server'

import { $id as circularsJsonSchemaId } from '@nasa-gcn/schema/gcn/circulars.schema.json'

const index = 'circulars'
const fromName = 'GCN Circulars'

async function removeIndex(id: number) {
const client = await getSearchClient()
Expand All @@ -45,65 +39,6 @@ async function putIndex(circular: Circular) {
})
}

async function getEmails() {
const db = await tables()
const client = db._doc as unknown as DynamoDBDocument
const TableName = db.name('circulars_subscriptions')
const pages = paginateScan(
{ client },
{ AttributesToGet: ['email'], TableName }
)
const emails: string[] = []
for await (const page of pages) {
const newEmails = page.Items?.map(({ email }) => email)
if (newEmails) emails.push(...newEmails)
}
return emails
}

async function getLegacyEmails() {
const db = await tables()
const client = db._doc as unknown as DynamoDBDocument
const TableName = db.name('legacy_users')
const pages = paginateQuery(
{ client },
{
IndexName: 'legacyReceivers',
KeyConditionExpression: 'receive = :receive',
ExpressionAttributeValues: {
':receive': 1,
},
ProjectionExpression: 'email',
TableName,
}
)
const emails: string[] = []
for await (const page of pages) {
const newEmails = page.Items?.map(({ email }) => email)
if (newEmails) emails.push(...newEmails)
}
return emails
}

export async function send(circular: Circular) {
const [emails, legacyEmails] = await Promise.all([
getEmails(),
getLegacyEmails(),
])
const to = [...emails, ...legacyEmails]
await sendEmailBulk({
fromName,
to,
subject: circular.subject,
body: `${formatCircularText(
circular
)}\n\n\nView this GCN Circular online at ${origin}/circulars/${
circular.circularId
}.`,
topic: 'circulars',
})
}

export const handler = createTriggerHandler(
async ({ eventName, dynamodb }: DynamoDBRecord) => {
const id = unmarshallTrigger(dynamodb!.Keys).circularId as number
Expand Down

0 comments on commit f51f32d

Please sign in to comment.