-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ab98352
commit 0263ef0
Showing
1 changed file
with
25 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -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 } | ||
|
@@ -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" }); | ||
} | ||
|
||
|
@@ -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", | ||
|