Check if subscription is valid

This commit is contained in:
KMKoushik
2024-05-15 07:44:47 +10:00
parent 64c7613d8c
commit 23750d23a1
2 changed files with 32 additions and 4 deletions

View File

@@ -1,4 +1,7 @@
import { AppSettingsService } from "~/server/service/app-settings-service";
import { parseSesHook } from "~/server/service/ses-hook-parser";
import { SnsNotificationMessage } from "~/types/aws-types";
import { APP_SETTINGS } from "~/utils/constants";
export async function GET(req: Request) {
console.log("GET", req);
@@ -10,6 +13,14 @@ export async function POST(req: Request) {
console.log(data, data.Message);
const isEventValid = await checkEventValidity(data);
console.log("isEventValid: ", isEventValid);
if (!isEventValid) {
return Response.json({ data: "Event is not valid" });
}
if (data.Type === "SubscriptionConfirmation") {
return handleSubscription(data);
}
@@ -19,7 +30,7 @@ export async function POST(req: Request) {
try {
message = JSON.parse(data.Message || "{}");
const status = await parseSesHook(message);
console.log("Error is parsing hook", status);
console.log("Error is parsing hook", !status);
if (!status) {
return Response.json({ data: "Error is parsing hook" });
}
@@ -38,3 +49,17 @@ async function handleSubscription(message: any) {
return Response.json({ data: "Success" });
}
// A simple check to ensure that the event is from the correct topic
async function checkEventValidity(message: SnsNotificationMessage) {
const { TopicArn } = message;
const configuredTopicArn = await AppSettingsService.getSetting(
APP_SETTINGS.SNS_TOPIC_ARN
);
if (TopicArn !== configuredTopicArn) {
return false;
}
return true;
}

View File

@@ -1,8 +1,11 @@
import pgBoss from "pg-boss";
import { env } from "~/env";
import { EmailAttachment, EmailContent } from "~/types";
import { db } from "../db";
import { sendEmailThroughSes, sendEmailWithAttachments } from "../aws/ses";
import { EmailAttachment } from "~/types";
import { db } from "~/server/db";
import {
sendEmailThroughSes,
sendEmailWithAttachments,
} from "~/server/aws/ses";
import { getConfigurationSetName } from "~/utils/ses-utils";
const boss = new pgBoss({