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 schemas and update related files

Kiran Kd1b69d2bdc8b
diff --git a/apps/web/lib/api/partners/enroll-partner.ts b/apps/web/lib/api/partners/enroll-partner.ts
index ab1f6a6041..444942adbf 100644
--- a/apps/web/lib/api/partners/enroll-partner.ts
+++ b/apps/web/lib/api/partners/enroll-partner.ts
@@ -5,7 +5,7 @@ import { updateConfig } from "@/lib/edge-config";
import { recordLink } from "@/lib/tinybird";
import { PartnerLinkProps, WorkspaceProps } from "@/lib/types";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
-import { EnrolledPartnerSchema } from "@/lib/zod/schemas/partners";
+import { EnrolledPartnerResponseSchema } from "@/lib/zod/schemas/partners";
import { prisma } from "@dub/prisma";
import { Prisma } from "@dub/prisma/client";
import { waitUntil } from "@vercel/functions";
@@ -102,7 +102,7 @@ export const enrollPartner = async ({
},
});
- const enrolledPartner = EnrolledPartnerSchema.parse({
+ const enrolledPartner = EnrolledPartnerResponseSchema.parse({
...upsertedPartner,
...upsertedPartner.programs[0],
links: [link],
diff --git a/apps/web/lib/openapi/partners/create-partner.ts b/apps/web/lib/openapi/partners/create-partner.ts
index 91b71968ff..2101852ec3 100644
--- a/apps/web/lib/openapi/partners/create-partner.ts
+++ b/apps/web/lib/openapi/partners/create-partner.ts
@@ -1,7 +1,7 @@
import { openApiErrorResponses } from "@/lib/openapi/responses";
import {
createPartnerSchema,
- EnrolledPartnerSchema,
+ EnrolledPartnerResponseSchema,
} from "@/lib/zod/schemas/partners";
import { ZodOpenApiOperationObject } from "zod-openapi";
@@ -23,7 +23,7 @@ export const createPartner: ZodOpenApiOperationObject = {
description: "The created partner",
content: {
"application/json": {
- schema: EnrolledPartnerSchema,
+ schema: EnrolledPartnerResponseSchema,
},
},
},
diff --git a/apps/web/lib/webhook/sample-events/partner-created.json b/apps/web/lib/webhook/sample-events/partner-created.json
index 459d574aae..a3efc0ee87 100644
--- a/apps/web/lib/webhook/sample-events/partner-created.json
+++ b/apps/web/lib/webhook/sample-events/partner-created.json
@@ -1,33 +1,35 @@
{
- "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.",
- "stripeConnectId": null,
- "payoutsEnabled": false,
- "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
- }
- ]
+ "id": "evt_BmAVZcjmXGfqQALqmDpzatEs6",
+ "event": "partner.created",
+ "createdAt": "2025-02-07T17:58:19.517Z",
+ "data": {
+ "id": "cm6v2l38p000zubyl5fly3i7w",
+ "name": "Matthew Hayden",
+ "email": "[email protected]",
+ "image": null,
+ "country": "MR",
+ "bio": "I'm a content creator who works with brands to grow their business.",
+ "tenantId": "64dc9a8c-5cf9-4446-b53b-cdc15199fafc",
+ "programId": "prog_CYCu7IMAapjkRpTnr8F1azjN",
+ "status": "approved",
+ "clicks": 0,
+ "leads": 0,
+ "sales": 0,
+ "salesAmount": 0,
+ "earnings": 0,
+ "createdAt": "2025-02-07T17:58:18.793Z",
+ "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/schemas.ts b/apps/web/lib/webhook/schemas.ts
index af228ec961..0ac9af252b 100644
--- a/apps/web/lib/webhook/schemas.ts
+++ b/apps/web/lib/webhook/schemas.ts
@@ -2,7 +2,7 @@ import z from "@/lib/zod";
import { clickEventSchema } from "../zod/schemas/clicks";
import { CustomerSchema } from "../zod/schemas/customers";
import { linkEventSchema } from "../zod/schemas/links";
-import { PartnerSchema } from "../zod/schemas/partners";
+import { EnrolledPartnerResponseSchema } from "../zod/schemas/partners";
import { WEBHOOK_TRIGGERS } from "./constants";
const saleSchema = z.object({
@@ -104,7 +104,7 @@ export const webhookEventSchema = z
id: z.string(),
event: z.literal("partner.created"),
createdAt: z.string(),
- data: PartnerSchema,
+ data: EnrolledPartnerResponseSchema,
})
.openapi({
ref: "PartnerCreatedEvent",
diff --git a/apps/web/lib/webhook/types.ts b/apps/web/lib/webhook/types.ts
index eaae11481e..76043f8a70 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 { EnrolledPartnerSchema } from "../zod/schemas/partners";
+import { EnrolledPartnerResponseSchema } from "../zod/schemas/partners";
import { WEBHOOK_TRIGGERS } from "./constants";
import {
clickWebhookEventSchema,
@@ -19,7 +19,9 @@ export type LeadEventDataProps = z.infer<typeof leadWebhookEventSchema>;
export type SaleEventDataProps = z.infer<typeof saleWebhookEventSchema>;
-export type PartnerEventDataProps = z.infer<typeof EnrolledPartnerSchema>;
+export type PartnerEventDataProps = z.infer<
+ typeof EnrolledPartnerResponseSchema
+>;
export type EventDataProps =
| LinkEventDataProps
diff --git a/apps/web/lib/zod/schemas/partners.ts b/apps/web/lib/zod/schemas/partners.ts
index 6027bf968d..e468aec888 100644
--- a/apps/web/lib/zod/schemas/partners.ts
+++ b/apps/web/lib/zod/schemas/partners.ts
@@ -78,6 +78,29 @@ export const EnrolledPartnerSchema = PartnerSchema.omit({
salesAmount: z.number().default(0),
});
+// Used by POST /api/partners and webhook (partner.created)
+export const EnrolledPartnerResponseSchema = EnrolledPartnerSchema.pick({
+ id: true,
+ name: true,
+ email: true,
+ image: true,
+ country: true,
+ bio: true,
+ tenantId: true,
+ programId: true,
+ status: true,
+ clicks: true,
+ leads: true,
+ sales: true,
+ salesAmount: true,
+ earnings: true,
+ createdAt: true,
+ links: true,
+}).extend({
+ tenantId: z.string(),
+ programId: z.string(),
+});
+
export const LeaderboardPartnerSchema = z.object({
id: z.string(),
name: z.string().transform((name) => {
diff --git a/apps/web/tests/webhooks/index.test.ts b/apps/web/tests/webhooks/index.test.ts
index 95a5d04f9c..4db3739553 100644
--- a/apps/web/tests/webhooks/index.test.ts
+++ b/apps/web/tests/webhooks/index.test.ts
@@ -11,7 +11,7 @@ import {
import z from "@/lib/zod";
import { CustomerSchema } from "@/lib/zod/schemas/customers";
import { linkEventSchema } from "@/lib/zod/schemas/links";
-import { EnrolledPartnerSchema } from "@/lib/zod/schemas/partners";
+import { EnrolledPartnerResponseSchema } from "@/lib/zod/schemas/partners";
import { describe, expect, test } from "vitest";
const webhook = {
@@ -32,9 +32,7 @@ const saleWebhookEventSchemaExtended = saleWebhookEventSchema.extend({
customer: customerSchemaExtended,
});
-const schema = EnrolledPartnerSchema;
-
-const enrolledPartnerSchemaExtended = schema.extend({
+const enrolledPartnerSchemaExtended = EnrolledPartnerResponseSchema.extend({
createdAt: z.string().transform((str) => new Date(str)),
updatedAt: z.string().transform((str) => new Date(str)),
});