From 48b343a38fc8c83af1cc84c5b5a9d2e54ee71231 Mon Sep 17 00:00:00 2001 From: David Hochbaum Date: Tue, 10 Dec 2024 17:07:07 -0500 Subject: [PATCH] Added resend confirmation email endpoint --- .../src/subscriber/subscriber.controller.ts | 29 ++++++++++++++++++- server/src/subscriber/subscriber.service.ts | 2 +- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/server/src/subscriber/subscriber.controller.ts b/server/src/subscriber/subscriber.controller.ts index 7db4fd12..cd7d6211 100644 --- a/server/src/subscriber/subscriber.controller.ts +++ b/server/src/subscriber/subscriber.controller.ts @@ -1,6 +1,6 @@ import { Controller, Get, Param, Patch, Post, Req, Res } from "@nestjs/common"; import { ConfigService } from "../config/config.service"; -import { SubscriberService } from "./subscriber.service"; +import { SubscriberService, ValidSubscriptionSet } from "./subscriber.service"; import { Request } from "express"; import validateEmail from "../_utils/validate-email"; import * as Sentry from "@sentry/nestjs"; @@ -142,4 +142,31 @@ export class SubscriberController { }); return; } + + @Post("/subscribers/:email/resend-confirmation") + async resendConfirmation(@Param() params, @Res() response) { + const existingUser = await this.subscriberService.findByEmail(params.email); + if (existingUser.code === 404) { + response.status(404).send({ + error: "User not found." + }) + return; + } + + const userId = existingUser['1'].result[params.email].contact.custom_fields[`zap_${this.sendgridEnvironment}_id`]; + var subscriptions = {} + + for (const [key, value] of Object.entries(existingUser['1'].result[params.email].contact.custom_fields)) { + if(((key.includes(this.sendgridEnvironment)) && !(key.endsWith("_confirmed") || key.endsWith("_id")))) { + subscriptions[key.split("_")[2]] = value; + } + } + + // Send the confirmation email + await this.subscriberService.sendConfirmationEmail(params.email, this.sendgridEnvironment, subscriptions, userId); + response.status(201).send({ + message: "Confirmation email sent" + }); + return; + } } \ No newline at end of file diff --git a/server/src/subscriber/subscriber.service.ts b/server/src/subscriber/subscriber.service.ts index 5ed95dad..99493af9 100644 --- a/server/src/subscriber/subscriber.service.ts +++ b/server/src/subscriber/subscriber.service.ts @@ -13,7 +13,7 @@ const validCustomFieldValues = [0, 1] as const; export type CustomFieldValueTuple = typeof validCustomFieldValues; type CustomFieldValue = CustomFieldValueTuple[number]; -type ValidSubscriptionSet = Record; +export type ValidSubscriptionSet = Record; type HttpMethod = 'get' | 'GET' | 'post' | 'POST' | 'put' | 'PUT' | 'patch' | 'PATCH' | 'delete' | 'DELETE';