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.

Automatically associate applications with programs

Tim Wilson446db53a7ca1
diff --git a/apps/web/lib/actions/partners/create-program-application.ts b/apps/web/lib/actions/partners/create-program-application.ts
index eb7697f3b6..ccc998eca6 100644
--- a/apps/web/lib/actions/partners/create-program-application.ts
+++ b/apps/web/lib/actions/partners/create-program-application.ts
@@ -1,9 +1,10 @@
"use server";
import { createId, getIP } from "@/lib/api/utils";
+import { getSession } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
import { ratelimit } from "@/lib/upstash";
-import { ProgramApplicationStatus } from "@prisma/client";
+import { Partner, ProgramApplicationStatus } from "@prisma/client";
import z from "../../zod";
import { actionClient } from "../safe-action";
@@ -56,11 +57,18 @@ export const createProgramApplicationAction = actionClient
message: "You have already applied to this program.",
};
+ // Find existing partner to automatically associate
+ const existingPartner = await getExistingPartner({
+ email: data.email,
+ programId: program.id,
+ });
+
const application = await prisma.programApplication.create({
data: {
...parsedInput,
id: createId({ prefix: "pga_" }),
programId: program.id,
+ partnerId: existingPartner?.id,
},
});
@@ -72,3 +80,48 @@ export const createProgramApplicationAction = actionClient
};
}
});
+
+async function getExistingPartner({
+ email,
+ programId,
+}: {
+ email: string;
+ programId: string;
+}) {
+ const session = await getSession();
+
+ let partner: Partner | null = null;
+
+ // First try any currently logged in user
+ if (session?.user.id)
+ partner = await prisma.partner.findFirst({
+ where: {
+ users: { some: { userId: session.user.id } },
+ programs: { none: { programId } },
+ },
+ });
+
+ // Then try to find a matching partner by user email
+ if (!partner)
+ partner = await prisma.partner.findFirst({
+ where: {
+ users: {
+ some: {
+ user: {
+ email,
+ emailVerified: {
+ not: null,
+ },
+ },
+ },
+ },
+ programs: {
+ none: {
+ programId,
+ },
+ },
+ },
+ });
+
+ return partner;
+}
diff --git a/apps/web/lib/actions/partners/onboard-partner.ts b/apps/web/lib/actions/partners/onboard-partner.ts
index 2e29d4ed83..d62fa32e9f 100644
--- a/apps/web/lib/actions/partners/onboard-partner.ts
+++ b/apps/web/lib/actions/partners/onboard-partner.ts
@@ -78,6 +78,16 @@ export const onboardPartnerAction = authUserActionClient
});
}
+ // Associate any matching program applications with the partner
+ await prisma.programApplication.updateMany({
+ where: {
+ email: user.email,
+ partnerId: null,
+ programId: { not: programInvite?.id },
+ },
+ data: { partnerId: partner.id },
+ });
+
// Create the Dots user with DOTS_DEFAULT_APP_ID
const [firstName, lastName] = name.split(" ");
const countryCode = COUNTRY_PHONE_CODES[country];
diff --git a/apps/web/lib/zod/schemas/partners.ts b/apps/web/lib/zod/schemas/partners.ts
index ecbc779475..6e6c898218 100644
--- a/apps/web/lib/zod/schemas/partners.ts
+++ b/apps/web/lib/zod/schemas/partners.ts
@@ -157,7 +157,7 @@ export const getSalesCountQuerySchema = getSalesQuerySchema.omit({
export const onboardPartnerSchema = z.object({
name: z.string().trim().min(1).max(100),
- logo: z.string().nullable(),
+ logo: z.string().optional(),
country: z.enum(Object.keys(COUNTRIES) as [string, ...string[]]),
phoneNumber: z.string().trim().min(1).max(15),
description: z.string().max(5000).nullable(),