Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moves the definition of the send function to resolve issues with kafka related imports in the remix build #2590

Merged
merged 3 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {

Check warning on line 10 in app/routes/circulars/circulars.server.ts

View check run for this annotation

Codecov / codecov/patch

app/routes/circulars/circulars.server.ts#L10

Added line #L10 was not covered by tests
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 {
bodyIsValid,
formatAuthor,
formatCircularText,
formatIsValid,
parseEventFromSubject,
subjectIsValid,
Expand All @@ -31,17 +36,18 @@
CircularChangeRequestKeys,
CircularMetadata,
} from './circulars.lib'
import { sendEmail } from '~/lib/email.server'
import { sendEmail, sendEmailBulk } from '~/lib/email.server'

Check warning on line 39 in app/routes/circulars/circulars.server.ts

View check run for this annotation

Codecov / codecov/patch

app/routes/circulars/circulars.server.ts#L39

Added line #L39 was not covered by tests
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'

Check warning on line 49 in app/routes/circulars/circulars.server.ts

View check run for this annotation

Codecov / codecov/patch

app/routes/circulars/circulars.server.ts#L49

Added line #L49 was not covered by tests

const getDynamoDBAutoIncrement = memoizee(
async function () {
const db = await tables()
Expand Down Expand Up @@ -593,3 +599,62 @@
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(

Check warning on line 607 in app/routes/circulars/circulars.server.ts

View check run for this annotation

Codecov / codecov/patch

app/routes/circulars/circulars.server.ts#L603-L607

Added lines #L603 - L607 were not covered by tests
{ client },
{ AttributesToGet: ['email'], TableName }
)
const emails: string[] = []
for await (const page of pages) {
const newEmails = page.Items?.map(({ email }) => email)

Check warning on line 613 in app/routes/circulars/circulars.server.ts

View check run for this annotation

Codecov / codecov/patch

app/routes/circulars/circulars.server.ts#L611-L613

Added lines #L611 - L613 were not covered by tests
if (newEmails) emails.push(...newEmails)
}
return emails

Check warning on line 616 in app/routes/circulars/circulars.server.ts

View check run for this annotation

Codecov / codecov/patch

app/routes/circulars/circulars.server.ts#L616

Added line #L616 was not covered by tests
}

async function getLegacyEmails() {
const db = await tables()
const client = db._doc as unknown as DynamoDBDocument
const TableName = db.name('legacy_users')
const pages = paginateQuery(

Check warning on line 623 in app/routes/circulars/circulars.server.ts

View check run for this annotation

Codecov / codecov/patch

app/routes/circulars/circulars.server.ts#L619-L623

Added lines #L619 - L623 were not covered by tests
{ 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)

Check warning on line 637 in app/routes/circulars/circulars.server.ts

View check run for this annotation

Codecov / codecov/patch

app/routes/circulars/circulars.server.ts#L635-L637

Added lines #L635 - L637 were not covered by tests
if (newEmails) emails.push(...newEmails)
}
return emails

Check warning on line 640 in app/routes/circulars/circulars.server.ts

View check run for this annotation

Codecov / codecov/patch

app/routes/circulars/circulars.server.ts#L640

Added line #L640 was not covered by tests
}

export async function send(circular: Circular) {
const [emails, legacyEmails] = await Promise.all([

Check warning on line 644 in app/routes/circulars/circulars.server.ts

View check run for this annotation

Codecov / codecov/patch

app/routes/circulars/circulars.server.ts#L643-L644

Added lines #L643 - L644 were not covered by tests
getEmails(),
getLegacyEmails(),
])
const to = [...emails, ...legacyEmails]
await sendEmailBulk({

Check warning on line 649 in app/routes/circulars/circulars.server.ts

View check run for this annotation

Codecov / codecov/patch

app/routes/circulars/circulars.server.ts#L648-L649

Added lines #L648 - L649 were not covered by tests
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'

Check warning on line 16 in app/table-streams/circulars/index.ts

View check run for this annotation

Codecov / codecov/patch

app/table-streams/circulars/index.ts#L16

Added line #L16 was not covered by tests

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 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
Loading