Skip to content

Commit

Permalink
feat: Add API endpoint for sending Zoom reminder emails with validation
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanKha committed Jan 16, 2025
1 parent c9b8c28 commit 8fa406e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 6 deletions.
61 changes: 61 additions & 0 deletions src/app/api/emails/actions/send-healthy-habits-zoom-link/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { z } from "zod";

import { sendZoomReminderEmail } from "@/server/api/emails/private-mutations";

const sendZoomReminderEmailRequestSchema = z.object({
recipientEmail: z.string().email({ message: "Invalid email" }),
meetingName: z.string(),
meetingLink: z.string(),
});

export type SendZoomReminderEmailRequest = z.infer<
typeof sendZoomReminderEmailRequestSchema
>;

export async function POST(request: Request) {
try {
const json = await request.json();

const apiKeyHeader = request.headers.get("x-api-key");

if (!apiKeyHeader || apiKeyHeader !== process.env.API_KEY) {
return new Response(
JSON.stringify({ success: false, error: "Invalid request." }),
{
status: 401,
headers: { "Content-Type": "application/json" },
},
);
}

const parsedJson = sendZoomReminderEmailRequestSchema.safeParse(json);

if (parsedJson.success === false) {
return new Response(
JSON.stringify({ success: false, error: "Invalid request body." }),
{
status: 400,
headers: { "Content-Type": "application/json" },
},
);
}

const sendZoomReminderEmailRequest = parsedJson.data;

await sendZoomReminderEmail(sendZoomReminderEmailRequest);

return new Response(JSON.stringify({ success: true }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
} catch (error) {
console.error(error);
return new Response(
JSON.stringify({ success: false, error: "Internal server error." }),
{
status: 500,
headers: { "Content-Type": "application/json" },
},
);
}
}
14 changes: 8 additions & 6 deletions src/server/api/emails/private-mutations.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { render } from "@react-email/components";

import { SendZoomReminderEmailRequest } from "@/app/api/emails/actions/send-healthy-habits-zoom-link/route";
import EmailVerificationEmail from "@/components/emails/EmailVerificationEmail";
import PasswordChangedEmail from "@/components/emails/PasswordChangedEmail";
import RejectionEmail from "@/components/emails/RejectionEmail";
Expand Down Expand Up @@ -65,17 +66,18 @@ export async function sendEmailVerificationEmail(
}

export async function sendZoomReminderEmail(
recipientEmail: string,
meetingName: string,
meetingLink: string,
sendZoomReminderEmailRequest: SendZoomReminderEmailRequest,
) {
const html = await render(
<ZoomReminderEmail meetingName={meetingName} meetingLink={meetingLink} />,
<ZoomReminderEmail
meetingName={sendZoomReminderEmailRequest.meetingName}
meetingLink={sendZoomReminderEmailRequest.meetingLink}
/>,
);

await sendEmail(
recipientEmail,
`Reminder: You have an upcoming meeting for ${meetingName}.`,
sendZoomReminderEmailRequest.recipientEmail,
`Reminder: You have an upcoming meeting for: ${sendZoomReminderEmailRequest.meetingName}.`,
html,
);
}

0 comments on commit 8fa406e

Please sign in to comment.