From 9a4077aabc27793295eba90b806451fc17a71704 Mon Sep 17 00:00:00 2001 From: Alex Yang <32620988+DatProJack@users.noreply.github.com> Date: Mon, 15 Jul 2024 23:27:14 -0400 Subject: [PATCH] ses genocide --- src/app.ts | 2 -- src/config.ts | 2 +- src/services/ses/ses-formats.ts | 16 ---------- src/services/ses/ses-router.ts | 52 --------------------------------- src/services/ses/ses-schema.ts | 10 ------- src/services/ses/ses-utils.ts | 16 +++++++++- 6 files changed, 16 insertions(+), 82 deletions(-) delete mode 100644 src/services/ses/ses-formats.ts delete mode 100644 src/services/ses/ses-router.ts delete mode 100644 src/services/ses/ses-schema.ts diff --git a/src/app.ts b/src/app.ts index 763ded8..d59b431 100644 --- a/src/app.ts +++ b/src/app.ts @@ -17,7 +17,6 @@ import eventsRouter from "./services/events/events-router"; import notificationsRouter from "./services/notifications/notifications-router"; import registrationRouter from "./services/registration/registration-router"; import s3Router from "./services/s3/s3-router"; -import sesRouter from "./services/ses/ses-router"; import statsRouter from "./services/stats/stats-router"; import sponsorRouter from "./services/sponsor/sponsor-router"; import subscriptionRouter from "./services/subscription/subscription-router"; @@ -50,7 +49,6 @@ app.use("/events", databaseMiddleware, eventsRouter); app.use("/notifications", databaseMiddleware, notificationsRouter); app.use("/registration", databaseMiddleware, registrationRouter); app.use("/s3", databaseMiddleware, s3Router); -app.use("/ses", sesRouter); app.use("/stats", databaseMiddleware, statsRouter); app.use("/sponsor", databaseMiddleware, sponsorRouter); app.use("/subscription", databaseMiddleware, subscriptionRouter); diff --git a/src/config.ts b/src/config.ts index b733d24..d2e6087 100644 --- a/src/config.ts +++ b/src/config.ts @@ -82,7 +82,7 @@ export const Config = { QR_HASH_ITERATIONS: 10000, QR_HASH_SECRET: getEnv("QR_HASH_SECRET"), - OUTGOING_EMAIL_ADDRESSES: "no-reply@reflectionsprojections.org", + OUTGOING_EMAIL_ADDRESSES: z.enum(["no-reply@reflectionsprojections.org"]), }; export const DeviceRedirects: Record = { diff --git a/src/services/ses/ses-formats.ts b/src/services/ses/ses-formats.ts deleted file mode 100644 index e27f6d8..0000000 --- a/src/services/ses/ses-formats.ts +++ /dev/null @@ -1,16 +0,0 @@ -export interface sendEmailParams { - Destination: { - ToAddresses: string[]; - }; - Message: { - Body: { - Text: { - Data: string; - }; - }; - Subject: { - Data: string; - }; - }; - Source: string; -} diff --git a/src/services/ses/ses-router.ts b/src/services/ses/ses-router.ts deleted file mode 100644 index 6aac074..0000000 --- a/src/services/ses/ses-router.ts +++ /dev/null @@ -1,52 +0,0 @@ -import { Request, Response, Router } from "express"; -import RoleChecker from "../../middleware/role-checker"; -import { StatusCodes } from "http-status-codes"; -import { Role } from "../auth/auth-models"; -import { sendEmail } from "./ses-utils"; - -const sesRouter: Router = Router(); - -//Takes in a list of emails and sends a test email to each of them -//Will change this to take in a template_id later -//The template_id and substitutions will all be done locally -sesRouter.post( - "/email", - RoleChecker([Role.enum.STAFF], true), - async (req: Request, res: Response) => { - if (!req.body.emailList) { - return res - .status(StatusCodes.BAD_REQUEST) - .json({ error: "Invalid Params" }); - } - - const emailList: string[] = req.body.emailList; - const emailPromises: Promise[] = []; - for (let i = 0; i < emailList.length; i++) { - emailPromises.push( - sendEmail(emailList[i], "Hello from SES!", `Test Email ${i}`) - .then(() => {}) - .catch((err) => { - console.error( - `Error sending email ${i + 1}:`, - err.message - ); - }) - ); - } - - Promise.all(emailPromises) - .then(() => { - return res - .status(StatusCodes.OK) - .send("All emails sent successfully"); - }) - .catch((err) => { - console.error("Error sending emails:", err.message); - return res - .status(StatusCodes.INTERNAL_SERVER_ERROR) - .send("Internal Server Error"); - }); - } -); - -export default sesRouter; diff --git a/src/services/ses/ses-schema.ts b/src/services/ses/ses-schema.ts deleted file mode 100644 index 9be550c..0000000 --- a/src/services/ses/ses-schema.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { z } from "zod"; - -// Zod schema for email requests -const NotificationsValidator = z.object({ - recipients: z.string(), - template: z.string(), - //should be able to work out subs from template -}); - -export { NotificationsValidator }; diff --git a/src/services/ses/ses-utils.ts b/src/services/ses/ses-utils.ts index a5103ac..bb6bc78 100644 --- a/src/services/ses/ses-utils.ts +++ b/src/services/ses/ses-utils.ts @@ -1,5 +1,17 @@ import { ses, Config } from "../../config"; +export function sendManyEmails( + emailIds: string[], + subject: string, + emailBody: string +): Promise[] { + const emailPromises: Promise[] = []; + for (let i = 0; i < emailIds.length; i++) { + emailPromises.push(sendEmail(emailIds[i], subject, emailBody)); + } + return emailPromises; +} + export function sendEmail( emailId: string, subject: string, @@ -20,7 +32,9 @@ export function sendEmail( Data: subject, }, }, - Source: Config.OUTGOING_EMAIL_ADDRESSES, + Source: Config.OUTGOING_EMAIL_ADDRESSES.Enum[ + "no-reply@reflectionsprojections.org" + ], }) .promise(); }