Skip to content

Commit

Permalink
feat: added delete email (#147)
Browse files Browse the repository at this point in the history
### Short description of changes:
  • Loading branch information
elliotsaha committed Mar 20, 2024
2 parents f5301e7 + dc9e02f commit 0026d85
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 22 deletions.
22 changes: 11 additions & 11 deletions src/app/api/contact/route.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import {NextRequest} from 'next/server';
import {z} from 'zod';
import {connectToDatabase} from '@lib';
import {logger, sendMail} from '@lib';
import {ServerResponse} from '@helpers';
import ContactEmail from '@emails/ContactEmail';
import { NextRequest } from "next/server";
import { z } from "zod";
import { connectToDatabase } from "@lib";
import { logger, sendMail } from "@lib";
import { ServerResponse } from "@helpers";
import ContactEmail from "@emails/ContactEmail";

const ContactEmailSchema = z.object({
name: z.string({required_error: 'Username is required'}),
name: z.string({ required_error: "Username is required" }),
email_address: z
.string({required_error: 'Email Address is required'})
.string({ required_error: "Email Address is required" })
.email(),
message: z.string({required_error: 'Message is required'}),
message: z.string({ required_error: "Message is required" }),
});

export const POST = async (request: NextRequest) => {
await connectToDatabase();

const body = await request.json();

const {name, email_address, message} = structuredClone(body);
const { name, email_address, message } = structuredClone(body);
const validation = ContactEmailSchema.safeParse({
name,
email_address,
Expand All @@ -38,7 +38,7 @@ export const POST = async (request: NextRequest) => {
}),
});

return ServerResponse.success('Successfully sent message.');
return ServerResponse.success("Successfully sent message.");
} catch (e) {
logger.error(e);

Expand Down
48 changes: 48 additions & 0 deletions src/app/api/emails/DeleteEventEmail.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {
Body,
Container,
Head,
Heading,
Hr,
Html,
Preview,
Tailwind,
Text,
Img,
} from "@react-email/components";
import * as React from "react";

interface DeleteEmailProps {
event_name: string;
}

export const DeleteEmail = ({ event_name }: DeleteEmailProps) => {
return (
<Html>
<Head />
<Preview>Cancellation of {event_name}</Preview>
<Tailwind>
<Body className="mx-auto my-auto font-sans bg-white">
<Container className="border border-solid border-[#eaeaea] rounded my-[40px] mx-auto p-[20px] w-[465px]">
<Img
src={`${process.env.NEXT_TENNIS_CIRCLE_LOGO_URI}`}
alt="Tennis Circle"
height="80"
className="mx-auto mt-4 object-fill"
/>
<Heading className="text-black text-[24px] font-normal text-center p-0 my-[30px] mx-0">
Cancellation of {event_name}
</Heading>
<Text className="text-black text-[14px] leading-[24px]">
Hey, we are sorry to inform you that {event_name} has been
cancelled. Your tickets will be automatically refunded.
</Text>
<Hr className="border border-solid border-[#eaeaea] my-[26px] mx-0 w-full" />
</Container>
</Body>
</Tailwind>
</Html>
);
};

export default DeleteEmail;
35 changes: 33 additions & 2 deletions src/app/api/events/delete/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,53 @@ import { NextRequest } from "next/server";
import { logger } from "@lib/winston";
import { ServerResponse } from "@helpers/serverResponse";
import z from "zod";
import { AttendeeList, User } from "@models";
import axios from "axios";
import { resend, sendMail } from "@lib";
import DeleteEmail from "@emails/DeleteEventEmail";

const DeleteSchema = z.object({
const deleteSchema = z.object({
event_id: z.string({ required_error: "Event is required" }),
secret: z.string({ required_error: "Invalid webhook secret" }),
});
export const POST = async (request: NextRequest) => {
const body = await request.json();

const validation = DeleteSchema.safeParse(body);
const validation = deleteSchema.safeParse(body);

if (validation.success) {
try {
if (body.secret !== process.env.NEXT_CONTENTFUL_SECRET) {
return ServerResponse.unauthorizedError("Invalid contentful request");
}

const event = await axios.post(
`${process.env.NEXT_PUBLIC_HOSTNAME}/api/events/detail`,
{
id: body.event_id,
},
);

const eventName = event.data.name;

const attendeeList = await AttendeeList.findOne({
event_id: body.event_id,
}).lean();

const emailList: string[] = await User.find({
_id: attendeeList.attendees,
})
.select("email_address")
.lean();

await sendMail({
to: emailList,
subject: `Cancellation of ${eventName}`,
emailComponent: DeleteEmail({
event_name: eventName,
}),
});

return ServerResponse.success("success");
} catch (e) {
logger.error(e);
Expand Down
18 changes: 9 additions & 9 deletions src/lib/resend.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use server';
import React from 'react';
import {Resend} from 'resend';
import {renderAsync} from '@react-email/render';
import {logger} from './winston';
import {ServerResponse} from '@helpers';
"use server";
import React from "react";
import { Resend } from "resend";
import { renderAsync } from "@react-email/render";
import { logger } from "./winston";
import { ServerResponse } from "@helpers";

const resend = new Resend(process.env.NEXT_RESEND_API);
export const resend = new Resend(process.env.NEXT_RESEND_API);

interface SendMailProps {
to: string;
to: string | string[];
subject: string;
emailComponent: React.ReactElement;
}
Expand All @@ -30,7 +30,7 @@ export const sendMail = async ({
} catch (e: unknown) {
logger.error(e);
return ServerResponse.serverError(
'We are currently experiencing a problem with our email server.'
"We are currently experiencing a problem with our email server.",
);
}
};
1 change: 1 addition & 0 deletions src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from "./User";
export * from "./Token";
export * from "./EmailToken";
export * from "./InstagramToken";
export * from "./AttendeeList";

0 comments on commit 0026d85

Please sign in to comment.