Skip to content

Commit

Permalink
refactor send route
Browse files Browse the repository at this point in the history
  • Loading branch information
aabassiouni committed Sep 19, 2024
1 parent ab98352 commit 0263ef0
Showing 1 changed file with 25 additions and 85 deletions.
110 changes: 25 additions & 85 deletions apps/web/src/app/api/[waitlistID]/send/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,34 @@ import { Resend } from "resend";
const resend = new Resend(env().RESEND_API_KEY);

export async function POST(request: NextRequest, context: { params: { waitlistID: string } }) {
console.log("sending emails");

const data = (await request.json()) as {
selectionMethod: string;
inviteCount: number;
invitesList: { email: string; id: string }[];
};

const { selectionMethod, inviteCount, invitesList } = data;
const type = request.nextUrl.searchParams.get("type");
const waitlistID = context.params.waitlistID;

// get user id from clerk
const { userId } = auth();

if (!userId) {
return NextResponse.redirect("/login");
}

const type = request.nextUrl.searchParams.get("type");

const waitlistID = context.params.waitlistID;

// check if user has waitlist
const workspace = await checkWorkspace(waitlistID);

if (!workspace) {
return NextResponse.redirect("/dashboard");
}

const data = (await request.json()) as {
selectionMethod: string;
inviteCount: number;
invitesList: { email: string; id: string }[];
};

const { selectionMethod, inviteCount, invitesList } = data;

const { bodyText, header, subject } = await getEmailTemplateForUser({
waitlistID,
template: "invite",
});

const {
logoFileURL: websiteLogo,
supportEmail,
Expand All @@ -61,81 +57,26 @@ export async function POST(request: NextRequest, context: { params: { waitlistID
}

switch (type) {
case "count":
return await handleCount();
case "list":
return await handleList();
}

async function handleCount() {
if (!workspace) {
return NextResponse.redirect("/dashboard");
case "count": {
const emailsList = await getInvitesListByCount(selectionMethod, inviteCount, waitlistID);
return await handleSend(emailsList);
}

const emailsList = await getInvitesListByCount(selectionMethod, inviteCount, waitlistID);

const resendResponse =
process.env.NODE_ENV === "development"
? { data: { data: [{ id: "1" }, { id: "2" }] }, error: null }
: await resend.batch.send(
emailsList.map((email) => {
return {
from: "Ali B <[email protected]>",
to: email.email,
subject: subject ?? "",
react: InviteTemplate({
bodyText,
header,
websiteLogo,
brandColor,
supportEmail,
websiteLink,
websiteName,
}),
};
}),
);

if (!resendResponse.error) {
if (resendResponse.data) {
console.log("Resend response", resendResponse.data);
const emailIDs = resendResponse.data.data.map((email) => email.id);
await createInvite(waitlistID, emailIDs, emailsList);
}
} else {
console.log("Error sending emails:", resendResponse.error);
return NextResponse.json({ message: "error" });
case "list": {
const emailsList = invitesList;
return await handleSend(emailsList);
}

await updateRemainingInvitesForWorkspace({
workspaceID: workspace.workspaceID,
remainingInvites: workspace.remainingInvites - inviteCount,
});

if (workspace?.stripeSubscriptionID) {
const subId = workspace?.stripeSubscriptionID;
if (!subId) {
return NextResponse.json({ code: "NO_SUBSCRIPTION_FOUND", message: "error" });
}
const sub = await stripe.subscriptions.retrieve(subId);
console.log("recording usage for", sub.items.data[0].id);
await stripe.subscriptionItems.createUsageRecord(sub.items.data[0].id, {
quantity: inviteCount,
timestamp: "now",
action: "increment",
});
}

return NextResponse.json({ message: "success" });
}

async function handleList() {
async function handleSend(
emailsList: {
email: string;
id: string;
}[],
) {
if (!workspace) {
return NextResponse.redirect("/dashboard");
}

const emailsList = invitesList;

const resendResponse =
process.env.NODE_ENV === "development"
? { data: { data: [{ id: "1" }, { id: "2" }] }, error: null }
Expand All @@ -160,12 +101,10 @@ export async function POST(request: NextRequest, context: { params: { waitlistID

if (!resendResponse.error) {
if (resendResponse.data) {
console.log("Resend response", resendResponse.data);
const emailIDs = resendResponse.data.data.map((email) => email.id);
await createInvite(waitlistID, emailIDs, emailsList);
}
} else {
console.log("Error sending emails:", resendResponse.error);
return NextResponse.json({ message: "error" });
}

Expand All @@ -179,8 +118,9 @@ export async function POST(request: NextRequest, context: { params: { waitlistID
if (!subId) {
return NextResponse.json({ code: "NO_SUBSCRIPTION_FOUND", message: "error" });
}

const sub = await stripe.subscriptions.retrieve(subId);
console.log("recording usage for", sub.items.data[0].id);

await stripe.subscriptionItems.createUsageRecord(sub.items.data[0].id, {
quantity: inviteCount,
timestamp: "now",
Expand Down

0 comments on commit 0263ef0

Please sign in to comment.