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.

Refactor partner enrollment and webhook event handling

Kiran K1bfec7c738f2
diff --git a/apps/web/app/api/partners/route.ts b/apps/web/app/api/partners/route.ts
index 77a04f2e1f..a463f5017c 100644
--- a/apps/web/app/api/partners/route.ts
+++ b/apps/web/app/api/partners/route.ts
@@ -191,10 +191,10 @@ export const POST = withWorkspace(
}),
);
- const createdPartner = await enrollPartner({
+ const partner = await enrollPartner({
programId,
tenantId,
- linkId: partnerLink.id,
+ link: partnerLink,
workspace,
partner: {
name,
@@ -205,13 +205,6 @@ export const POST = withWorkspace(
},
});
- const partner = EnrolledPartnerSchema.parse({
- ...createdPartner,
- links: [partnerLink],
- status: "approved",
- commissionAmount: null,
- });
-
return NextResponse.json(partner, {
status: 201,
});
diff --git a/apps/web/lib/actions/partners/add-partner.ts b/apps/web/lib/actions/partners/add-partner.ts
index 6474640783..b79a9da0cd 100644
--- a/apps/web/lib/actions/partners/add-partner.ts
+++ b/apps/web/lib/actions/partners/add-partner.ts
@@ -58,7 +58,7 @@ export const addPartnerAction = authActionClient
return await enrollPartner({
programId,
- linkId,
+ link,
workspace,
partner: {
name,
diff --git a/apps/web/lib/api/partners/enroll-partner.ts b/apps/web/lib/api/partners/enroll-partner.ts
index 1d1445fac3..124429824a 100644
--- a/apps/web/lib/api/partners/enroll-partner.ts
+++ b/apps/web/lib/api/partners/enroll-partner.ts
@@ -3,9 +3,9 @@
import { createId } from "@/lib/api/utils";
import { updateConfig } from "@/lib/edge-config";
import { recordLink } from "@/lib/tinybird";
-import { WorkspaceProps } from "@/lib/types";
+import { PartnerLinkProps, WorkspaceProps } from "@/lib/types";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
-import { PartnerSchema } from "@/lib/zod/schemas/partners";
+import { EnrolledPartnerSchema } from "@/lib/zod/schemas/partners";
import { prisma } from "@dub/prisma";
import { Prisma } from "@dub/prisma/client";
import { waitUntil } from "@vercel/functions";
@@ -14,13 +14,12 @@ import { includeTags } from "../links/include-tags";
export const enrollPartner = async ({
programId,
- linkId,
tenantId,
workspace,
+ link,
partner,
}: {
programId: string;
- linkId: string;
tenantId?: string;
workspace: Pick<WorkspaceProps, "id" | "webhookEnabled">;
partner: {
@@ -30,6 +29,7 @@ export const enrollPartner = async ({
country?: string | null;
description?: string | null;
};
+ link: PartnerLinkProps;
}) => {
if (partner.email) {
const programEnrollment = await prisma.programEnrollment.findFirst({
@@ -75,7 +75,7 @@ export const enrollPartner = async ({
tenantId,
links: {
connect: {
- id: linkId,
+ id: link.id,
},
},
status: "approved",
@@ -97,6 +97,15 @@ export const enrollPartner = async ({
country: partner.country ?? "US",
bio: partner.description,
},
+ include: {
+ programs: true,
+ },
+ });
+
+ const enrolledPartner = EnrolledPartnerSchema.parse({
+ ...upsertedPartner,
+ ...upsertedPartner.programs[0]!,
+ links: [link],
});
waitUntil(
@@ -105,7 +114,7 @@ export const enrollPartner = async ({
prisma.link
.update({
where: {
- id: linkId,
+ id: link.id,
},
data: {
partnerId: upsertedPartner.id,
@@ -113,6 +122,7 @@ export const enrollPartner = async ({
include: includeTags,
})
.then((link) => recordLink(link)),
+
// TODO: Remove this once we open up partners.dub.co to everyone
partner.email &&
updateConfig({
@@ -123,10 +133,10 @@ export const enrollPartner = async ({
sendWorkspaceWebhook({
workspace,
trigger: "partner.created",
- data: PartnerSchema.parse(upsertedPartner),
+ data: enrolledPartner,
}),
]),
);
- return upsertedPartner;
+ return enrolledPartner;
};
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index ba8be355a2..2c3e8fe0ae 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -42,6 +42,7 @@ import {
PayoutSchema,
} from "./zod/schemas/payouts";
import {
+ PartnerLinkSchema,
PartnerProgramInviteSchema,
ProgramEnrollmentSchema,
ProgramInviteSchema,
@@ -355,6 +356,8 @@ export type CustomerProps = z.infer<typeof CustomerSchema>;
export type PartnerProps = z.infer<typeof PartnerSchema>;
+export type PartnerLinkProps = z.infer<typeof PartnerLinkSchema>;
+
export type EnrolledPartnerProps = z.infer<typeof EnrolledPartnerSchema>;
export type DiscountProps = z.infer<typeof DiscountSchema>;
diff --git a/apps/web/lib/webhook/publish.ts b/apps/web/lib/webhook/publish.ts
index c9c1687eca..3facf3b1e3 100644
--- a/apps/web/lib/webhook/publish.ts
+++ b/apps/web/lib/webhook/publish.ts
@@ -16,7 +16,11 @@ export const sendWorkspaceWebhook = async ({
}: {
trigger: WebhookTrigger;
workspace: Pick<WorkspaceProps, "id" | "webhookEnabled">;
- data: LinkEventDataProps | LeadEventDataProps | SaleEventDataProps | PartnerEventDataProps;
+ data:
+ | LinkEventDataProps
+ | LeadEventDataProps
+ | SaleEventDataProps
+ | PartnerEventDataProps;
}) => {
if (!workspace.webhookEnabled) {
return;
diff --git a/apps/web/lib/webhook/sample-events/partner-created.json b/apps/web/lib/webhook/sample-events/partner-created.json
index 6b433c1602..459d574aae 100644
--- a/apps/web/lib/webhook/sample-events/partner-created.json
+++ b/apps/web/lib/webhook/sample-events/partner-created.json
@@ -1,13 +1,33 @@
{
- "id": "pn_BlBxRjQW7pC2MreIvtCcCROr",
+ "id": "pn_23fH8O3WxdwvfxP6ULuv6O1e",
+ "tenantId": "eaed0599-9da4-4ca2-b3b0-41b014b519cc",
"name": "Matthew Hayden",
"email": "[email protected]",
"image": null,
"country": "US",
"bio": "I'm a content creator who works with brands to grow their business.",
- "status": "default",
"stripeConnectId": null,
"payoutsEnabled": false,
- "createdAt": "2025-02-06T17:15:59.658Z",
- "updatedAt": "2025-02-06T17:15:59.658Z"
+ "createdAt": "2025-02-07T08:36:02.169Z",
+ "updatedAt": "2025-02-07T08:36:02.169Z",
+ "status": "approved",
+ "commissionAmount": null,
+ "earnings": 1000,
+ "clicks": 100,
+ "leads": 70,
+ "sales": 25,
+ "salesAmount": 2500,
+ "links": [
+ {
+ "id": "link_5myDHLqhIQvUmUPjchVygF9R",
+ "domain": "refer.dub.co",
+ "key": "track-test",
+ "shortLink": "https://refer.dub.co/track-test",
+ "url": "https://refer.dub.co",
+ "clicks": 100,
+ "leads": 70,
+ "sales": 25,
+ "saleAmount": 2500
+ }
+ ]
}
diff --git a/apps/web/lib/webhook/types.ts b/apps/web/lib/webhook/types.ts
index 86095f0b04..eaae11481e 100644
--- a/apps/web/lib/webhook/types.ts
+++ b/apps/web/lib/webhook/types.ts
@@ -1,6 +1,6 @@
import z from "../zod";
import { linkEventSchema } from "../zod/schemas/links";
-import { PartnerSchema } from "../zod/schemas/partners";
+import { EnrolledPartnerSchema } from "../zod/schemas/partners";
import { WEBHOOK_TRIGGERS } from "./constants";
import {
clickWebhookEventSchema,
@@ -19,7 +19,7 @@ export type LeadEventDataProps = z.infer<typeof leadWebhookEventSchema>;
export type SaleEventDataProps = z.infer<typeof saleWebhookEventSchema>;
-export type PartnerEventDataProps = z.infer<typeof PartnerSchema>;
+export type PartnerEventDataProps = z.infer<typeof EnrolledPartnerSchema>;
export type EventDataProps =
| LinkEventDataProps
diff --git a/apps/web/lib/zod/schemas/programs.ts b/apps/web/lib/zod/schemas/programs.ts
index d5375b0680..dec3b9936b 100644
--- a/apps/web/lib/zod/schemas/programs.ts
+++ b/apps/web/lib/zod/schemas/programs.ts
@@ -43,26 +43,25 @@ export const createProgramSchema = z.object({
url: z.string().nullable(),
});
+export const PartnerLinkSchema = LinkSchema.pick({
+ id: true,
+ domain: true,
+ key: true,
+ shortLink: true,
+ url: true,
+ clicks: true,
+ leads: true,
+ sales: true,
+ saleAmount: true,
+});
+
export const ProgramEnrollmentSchema = z.object({
partnerId: z.string(),
+ tenantId: z.string().nullable(),
programId: z.string(),
program: ProgramSchema,
status: z.nativeEnum(ProgramEnrollmentStatus),
- links: z
- .array(
- LinkSchema.pick({
- id: true,
- domain: true,
- key: true,
- shortLink: true,
- url: true,
- clicks: true,
- leads: true,
- sales: true,
- saleAmount: true,
- }),
- )
- .nullable(),
+ links: z.array(PartnerLinkSchema).nullable(),
discount: DiscountSchema.nullish(),
commissionAmount: z.number().nullable(),
createdAt: z.date(),
diff --git a/apps/web/tests/webhooks/index.test.ts b/apps/web/tests/webhooks/index.test.ts
index d04859d798..e9dc226900 100644
--- a/apps/web/tests/webhooks/index.test.ts
+++ b/apps/web/tests/webhooks/index.test.ts
@@ -101,16 +101,18 @@ const assertQstashMessage = async (
expect(receivedBody.event).toEqual(trigger);
expect(receivedBody.data).toEqual(body);
- if (trigger === "partner.created") {
- PartnerSchema.extend({
- createdAt: z.string().transform((str) => new Date(str)),
- updatedAt: z.string().transform((str) => new Date(str)),
- });
- } else {
- expect(eventSchemas[trigger].safeParse(receivedBody.data).success).toBe(
- true,
- );
- }
+ // if (trigger === "partner.created") {
+ // PartnerSchema.extend({
+ // createdAt: z.string().transform((str) => new Date(str)),
+ // updatedAt: z.string().transform((str) => new Date(str)),
+ // });
+ // } else {
+ // expect(eventSchemas[trigger].safeParse(receivedBody.data).success).toBe(
+ // true,
+ // );
+ // }
+
+ expect(eventSchemas[trigger].safeParse(receivedBody.data).success).toBe(true);
};
// TODO: