The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Update route.ts
Kiran K6fcb329042e5
diff --git a/apps/web/app/(ee)/api/partner-profile/invites/accept/route.ts b/apps/web/app/(ee)/api/partner-profile/invites/accept/route.tsindex 0a773761e2..e37ba30859 100644--- a/apps/web/app/(ee)/api/partner-profile/invites/accept/route.ts+++ b/apps/web/app/(ee)/api/partner-profile/invites/accept/route.ts@@ -5,47 +5,51 @@ import { NextResponse } from "next/server";// POST /api/partner-profile/invites/accept – accept a partner inviteexport const POST = withSession(async ({ session }) => {- const invite = await prisma.partnerInvite.findFirst({- where: {- email: session.user.email,- },- include: {- partner: true,- },- });-- if (!invite) {- throw new DubApiError({- code: "not_found",- message: "No invitation found for your email.",+ // Use a transaction to prevent race conditions when accepting invites+ await prisma.$transaction(async (tx) => {+ // Re-fetch and validate the invite inside the transaction+ const invite = await tx.partnerInvite.findFirst({+ where: {+ email: session.user.email,+ },+ include: {+ partner: true,+ },});- }- if (invite.expires < new Date()) {- throw new DubApiError({- code: "invite_expired",- message: "The invitation has been expired.",- });- }+ if (!invite) {+ throw new DubApiError({+ code: "not_found",+ message: "No invitation found for your email.",+ });+ }- const partner = invite.partner;+ if (invite.expires < new Date()) {+ throw new DubApiError({+ code: "invite_expired",+ message: "The invitation has been expired.",+ });+ }- const existingPartnerMembership = await prisma.partnerUser.count({- where: {- userId: session.user.id,- },- });+ const partner = invite.partner;- if (existingPartnerMembership > 0) {- throw new DubApiError({- code: "conflict",- message:- "You’re already associated with another partner profile. A user can only belong to one partner profile at a time.",+ // Re-check for existing membership inside the transaction+ const existingPartnerMembership = await tx.partnerUser.count({+ where: {+ userId: session.user.id,+ },});- }- await Promise.all([- prisma.partnerUser.create({+ if (existingPartnerMembership > 0) {+ throw new DubApiError({+ code: "conflict",+ message:+ "You're already associated with another partner profile. A user can only belong to one partner profile at a time.",+ });+ }++ // Create the partner user+ await tx.partnerUser.create({data: {userId: session.user.id,role: invite.role,@@ -54,27 +58,43 @@ export const POST = withSession(async ({ session }) => {create: {},},},- }),+ });- prisma.partnerInvite.delete({+ // Delete the invite+ await tx.partnerInvite.delete({where: {email_partnerId: {email: session.user.email,partnerId: partner.id,},},- }),+ });- session.user["defaultPartnerId"] === null &&- prisma.user.update({+ // Conditionally update defaultPartnerId only if it's still null+ // Fetch current user state inside transaction to ensure atomicity+ if (session.user["defaultPartnerId"] === null) {+ const currentUser = await tx.user.findUnique({where: {id: session.user.id,},- data: {- defaultPartnerId: partner.id,+ select: {+ defaultPartnerId: true,},- }),- ]);+ });++ // Only update if defaultPartnerId is still null in the database+ if (currentUser && currentUser.defaultPartnerId === null) {+ await tx.user.update({+ where: {+ id: session.user.id,+ },+ data: {+ defaultPartnerId: partner.id,+ },+ });+ }+ }+ });return NextResponse.json({message: "You are now a member of this partner profile.",