Skip to content

Commit

Permalink
Merge branch 'main' into hcd-design-changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tbolt committed Nov 22, 2024
2 parents a7e654f + 5b6ed5d commit b9b0b9d
Show file tree
Hide file tree
Showing 42 changed files with 711 additions and 1,036 deletions.
Binary file modified bun.lockb
Binary file not shown.
159 changes: 0 additions & 159 deletions lib/lambda/appkNewSubmission.ts

This file was deleted.

1 change: 0 additions & 1 deletion lib/lambda/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ export * as checkConsumerLag from "./checkConsumerLag";
export * as cfnNotify from "./cfnNotify";
export * as deleteTriggers from "./deleteTriggers";
export * as sinkChangelog from "./sinkChangelog";
export * as appkNewSubmission from "./appkNewSubmission";
export * as item from "./item";
export * as postAuth from "./postAuth";
21 changes: 18 additions & 3 deletions lib/lambda/processEmails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ export async function processRecord(kafkaRecord: KafkaRecord, config: ProcessEma
}

try {
console.log("Processing record:", JSON.stringify(record, null, 2));
console.log("Config:", JSON.stringify(config, null, 2));
await processAndSendEmails(record, id, config);
} catch (error) {
console.error("Error processing record:", JSON.stringify(error, null, 2));
Expand Down Expand Up @@ -179,6 +181,7 @@ export async function processAndSendEmails(record: any, id: string, config: Proc
allStateUsersEmails,
};

console.log("Template variables:", JSON.stringify(templateVariables, null, 2));
const limit = pLimit(5); // Limit concurrent emails
const sendEmailPromises = templates.map((template) =>
limit(async () => {
Expand All @@ -190,11 +193,21 @@ export async function processAndSendEmails(record: any, id: string, config: Proc
config.applicationEndpointUrl,
config.isDev,
);
await sendEmail(params, config.region);
try {
await sendEmail(params, config.region);
} catch (error) {
console.error("Error sending email:", error);
throw error;
}
}),
);

await Promise.all(sendEmailPromises);
try {
await Promise.all(sendEmailPromises);
} catch (error) {
console.error("Error sending emails:", error);
throw error;
}
}

export function createEmailParams(
Expand All @@ -204,7 +217,7 @@ export function createEmailParams(
isDev: boolean,
): SendEmailCommandInput {
const toAddresses = isDev ? [`State Submitter <${EMAIL_CONFIG.DEV_EMAIL}>`] : filledTemplate.to;
return {
const params = {
Destination: {
ToAddresses: toAddresses,
CcAddresses: filledTemplate.cc,
Expand All @@ -222,6 +235,8 @@ export function createEmailParams(
Source: sourceEmail,
ConfigurationSetName: process.env.configurationSetName,
};
console.log("Email params:", JSON.stringify(params, null, 2));
return params;
}

export async function sendEmail(params: SendEmailCommandInput, region: string): Promise<any> {
Expand Down
46 changes: 46 additions & 0 deletions lib/lambda/submit/submissionPayloads/app-k.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { events } from "shared-types/events";
import {
isAuthorized,
getAuthDetails,
lookupUserAttributes,
} from "libs/api/auth/user";
import { type APIGatewayEvent } from "aws-lambda";
import { itemExists } from "libs/api/package";

export const appK = async (event: APIGatewayEvent) => {
if (!event.body) return;

const parsedResult = events["app-k"].baseSchema.safeParse(
JSON.parse(event.body),
);
if (!parsedResult.success) {
throw parsedResult.error;
}

// This is the backend check for auth
if (!(await isAuthorized(event, parsedResult.data.id.slice(0, 2)))) {
throw "Unauthorized";
}

// This is the backend check for the item already existing
if (await itemExists({ id: parsedResult.data.id })) {
throw "Item Already Exists";
}

const authDetails = getAuthDetails(event);
const userAttr = await lookupUserAttributes(
authDetails.userId,
authDetails.poolId,
);
const submitterEmail = userAttr.email;
const submitterName = `${userAttr.given_name} ${userAttr.family_name}`;

const transformedData = events["app-k"].schema.parse({
...parsedResult.data,
submitterName,
submitterEmail,
timestamp: Date.now(),
});

return transformedData;
};
2 changes: 2 additions & 0 deletions lib/lambda/submit/submissionPayloads/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { withdrawPackage } from "./withdraw-package";
import { withdrawRai } from "./withdraw-rai";
import { toggleWithdrawRai } from "./toggle-withdraw-rai";
import { respondToRai } from "./respond-to-rai";
import { appK } from "./app-k";

export const submissionPayloads = {
"capitated-amendment": capitatedAmendment,
Expand All @@ -27,5 +28,6 @@ export const submissionPayloads = {
"withdraw-rai": withdrawRai,
"toggle-withdraw-rai": toggleWithdrawRai,
"respond-to-rai": respondToRai,
"app-k": appK,
"upload-subsequent-documents": uploadSubsequentDocuments,
};
5 changes: 2 additions & 3 deletions lib/libs/email/content/email-components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,8 @@ const WithdrawRAI = ({
}) => (
<Section>
<Heading as="h2">
The OneMAC Submission Portal received a request to withdraw the Formal RAI Response. You are
receiving this email notification as the Formal RAI for {id} was withdrawn by {submitterName}{" "}
{submitterEmail}.
{`The OneMAC Submission Portal received a request to withdraw the Formal RAI Response. You are
receiving this email notification as the Formal RAI for ${id} was withdrawn by ${submitterName} ${submitterEmail}.`}
</Heading>
</Section>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ export { ChipSpaCMSEmail } from "./ChipSpaCMS";
export { ChipSpaStateEmail } from "./ChipSpaState";
export { AppKCMSEmail } from "./AppKCMS";
export { AppKStateEmail } from "./AppKState";
export { Waiver1915bCMSEmail } from "./Waiver1915bCMS";
export { Waiver1915bStateEmail } from "./Waiver1915bState";
3 changes: 2 additions & 1 deletion lib/libs/email/content/new-submission/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import {
ChipSpaStateEmail,
AppKCMSEmail,
AppKStateEmail,
Waiver1915bCMSEmail,
Waiver1915bStateEmail,
} from "./emailTemplates";
import { render } from "@react-email/render";
import { Waiver1915bCMSEmail, Waiver1915bStateEmail } from "../widthdrawPackage/emailTemplates";

export const newSubmission: AuthoritiesWithUserTypesTemplate = {
[Authority.MED_SPA]: {
Expand Down
41 changes: 34 additions & 7 deletions lib/libs/email/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,48 @@ export type AuthoritiesWithUserTypesTemplate = {
};

export type EmailTemplates = {
"new-medicaid-submission": AuthoritiesWithUserTypesTemplate | UserTypeOnlyTemplate;
"new-chip-submission": AuthoritiesWithUserTypesTemplate | UserTypeOnlyTemplate;
"new-medicaid-submission": AuthoritiesWithUserTypesTemplate;
"new-chip-submission": AuthoritiesWithUserTypesTemplate;
"temp-extension": UserTypeOnlyTemplate;
"withdraw-package": AuthoritiesWithUserTypesTemplate;
"withdraw-rai": AuthoritiesWithUserTypesTemplate;
"contracting-initial": AuthoritiesWithUserTypesTemplate;
"capitated-initial": AuthoritiesWithUserTypesTemplate;
};

export const emailTemplates = {
// Create a type-safe mapping of email templates
const emailTemplates: EmailTemplates = {
"new-medicaid-submission": EmailContent.newSubmission,
"new-chip-submission": EmailContent.newSubmission,
"temp-extension": EmailContent.tempExtention,
"withdraw-package": EmailContent.withdrawPackage,
"withdraw-rai": EmailContent.withdrawRai,
"contracting-initial": EmailContent.newSubmission,
"capitated-initial": EmailContent.newSubmission,
};

function isAuthorityTemplate(obj: any, authority: Authority): obj is AuthoritiesWithUserTypesTemplate {
// Create a type-safe lookup function
export function getEmailTemplate(
action: keyof EmailTemplates,
): AuthoritiesWithUserTypesTemplate | UserTypeOnlyTemplate {
// Handle -state suffix variants
const baseAction = action.replace(/-state$/, "") as keyof EmailTemplates;
return emailTemplates[baseAction];
}

function isAuthorityTemplate(
obj: any,
authority: Authority,
): obj is AuthoritiesWithUserTypesTemplate {
return authority in obj;
}

export async function getEmailTemplates<T>(action: keyof EmailTemplates, authority: Authority): Promise<EmailTemplateFunction<T>[] | null> {
const template = emailTemplates[action];
// Update the getEmailTemplates function to use the new lookup
export async function getEmailTemplates<T>(
action: keyof EmailTemplates,
authority: Authority,
): Promise<EmailTemplateFunction<T>[] | null> {
const template = getEmailTemplate(action);
if (!template) {
console.log("No template found");
return null;
Expand All @@ -48,9 +72,12 @@ export async function getEmailTemplates<T>(action: keyof EmailTemplates, authori
if (isAuthorityTemplate(template, authority)) {
emailTemplatesToSend.push(...Object.values(template[authority] as EmailTemplateFunction<T>));
} else {
emailTemplatesToSend.push(...Object.values(template as EmailTemplateFunction<T>));
emailTemplatesToSend.push(
...Object.values(template as Record<UserType, EmailTemplateFunction<T>>),
);
}

console.log("Email templates to send:", JSON.stringify(emailTemplatesToSend, null, 2));
return emailTemplatesToSend;
}

Expand Down
Loading

0 comments on commit b9b0b9d

Please sign in to comment.