Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Return 404 from withPartnerProfile when the partner is missing

Rafi73cada66a2bc
diff --git a/apps/web/lib/auth/partner.ts b/apps/web/lib/auth/partner.ts
index 8f1d6b8f9c..406642e487 100644
--- a/apps/web/lib/auth/partner.ts
+++ b/apps/web/lib/auth/partner.ts
@@ -237,27 +237,37 @@ export const withPartnerProfile = (
});
}
- const partnerUser = await prisma.partnerUser.findUnique({
- where: {
- userId_partnerId: {
- userId,
- partnerId: defaultPartnerId,
- },
- },
- include: {
- partner: {
- include: {
- industryInterests: true,
- preferredEarningStructures: true,
- salesChannels: true,
- platforms: true,
+ // Load the partner on its own, not as an include, so a PartnerUser row
+ // whose partner was deleted returns a 404 instead of a Prisma error.
+ const [partnerUser, partnerProfile] = await Promise.all([
+ prisma.partnerUser.findUnique({
+ where: {
+ userId_partnerId: {
+ userId,
+ partnerId: defaultPartnerId,
},
},
- },
- });
+ select: {
+ userId: true,
+ role: true,
+ },
+ }),
+
+ prisma.partner.findUnique({
+ where: {
+ id: defaultPartnerId,
+ },
+ include: {
+ industryInterests: true,
+ preferredEarningStructures: true,
+ salesChannels: true,
+ platforms: true,
+ },
+ }),
+ ]);
- // partnerUser relationship doesn't exist
- if (!partnerUser) {
+ // partnerUser relationship or partner doesn't exist
+ if (!partnerUser || !partnerProfile) {
throw new DubApiError({
code: "not_found",
message: "Partner profile not found.",
@@ -273,7 +283,7 @@ export const withPartnerProfile = (
// Beta feature checks
if (featureFlag) {
- const flags = await getPartnerFeatureFlags(partnerUser.partner.id);
+ const flags = await getPartnerFeatureFlags(partnerProfile.id);
if (!flags[featureFlag]) {
throw new DubApiError({
@@ -289,7 +299,7 @@ export const withPartnerProfile = (
salesChannels,
platforms,
...partnerProps
- } = partnerUser.partner;
+ } = partnerProfile;
return await handler({
req,