Skip to content

Commit

Permalink
Merge pull request #95 from ReflectionsProjections/dev/alex/ses
Browse files Browse the repository at this point in the history
  • Loading branch information
aletya authored Jul 19, 2024
2 parents 17e34ea + 9a4077a commit 971dc28
Show file tree
Hide file tree
Showing 5 changed files with 817 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
},
"dependencies": {
"@aws-sdk/client-s3": "^3.496.0",
"@aws-sdk/client-ses": "^3.609.0",
"@aws-sdk/s3-presigned-post": "^3.499.0",
"@aws-sdk/s3-request-presigner": "^3.496.0",
"@paralleldrive/cuid2": "^2.2.2",
Expand Down
7 changes: 7 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { StatusCodes } from "http-status-codes";
import { Config } from "./config";
import { rateLimiter } from "./middleware/rateLimiter";
import { isTest } from "./utilities";
import AWS from "aws-sdk";

import databaseMiddleware from "./middleware/database-middleware";
import customCors from "./middleware/cors-middleware";
Expand All @@ -21,6 +22,12 @@ import statsRouter from "./services/stats/stats-router";
import subscriptionRouter from "./services/subscription/subscription-router";
import speakersRouter from "./services/speakers/speakers-router";

AWS.config.update({
region: Config.S3_REGION,
accessKeyId: Config.S3_ACCESS_KEY,
secretAccessKey: Config.S3_SECRET_KEY,
});

const app = express();

// to prevent server-side caching/returning status code 200
Expand Down
8 changes: 8 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import dotenv from "dotenv";
import { z } from "zod";
import { getEnv } from "./utilities";

import AWS from "aws-sdk";

dotenv.config();

export const Environment = z.enum(["PRODUCTION", "DEVELOPMENT", "TESTING"]);
Expand Down Expand Up @@ -79,6 +81,8 @@ export const Config = {
// QR Scanning
QR_HASH_ITERATIONS: 10000,
QR_HASH_SECRET: getEnv("QR_HASH_SECRET"),

OUTGOING_EMAIL_ADDRESSES: z.enum(["[email protected]"]),
};

export const DeviceRedirects: Record<string, string> = {
Expand All @@ -87,4 +91,8 @@ export const DeviceRedirects: Record<string, string> = {
mobile: "exp://192.168.86.24:8081/--/Main",
};

export const ses = new AWS.SES({
region: Config.S3_REGION,
});

export default Config;
40 changes: 40 additions & 0 deletions src/services/ses/ses-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { ses, Config } from "../../config";

export function sendManyEmails(
emailIds: string[],
subject: string,
emailBody: string
): Promise<AWS.SES.SendEmailResponse>[] {
const emailPromises: Promise<AWS.SES.SendEmailResponse>[] = [];
for (let i = 0; i < emailIds.length; i++) {
emailPromises.push(sendEmail(emailIds[i], subject, emailBody));
}
return emailPromises;
}

export function sendEmail(
emailId: string,
subject: string,
emailBody: string
): Promise<AWS.SES.SendEmailResponse> {
return ses
.sendEmail({
Destination: {
ToAddresses: [emailId],
},
Message: {
Body: {
Text: {
Data: emailBody,
},
},
Subject: {
Data: subject,
},
},
Source: Config.OUTGOING_EMAIL_ADDRESSES.Enum[
"[email protected]"
],
})
.promise();
}
Loading

0 comments on commit 971dc28

Please sign in to comment.