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.

simplify schema + add tests

Steven Tey2ae426147a44
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index 875288307b..966787abeb 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -49,7 +49,7 @@ import {
import { createOAuthAppSchema, oAuthAppSchema } from "./zod/schemas/oauth";
import {
createPartnerSchema,
- EnrolledPartnerSchemaExtended,
+ EnrolledPartnerSchema,
PartnerSchema,
} from "./zod/schemas/partners";
import {
@@ -411,9 +411,7 @@ export type PartnerProfileCustomerProps = z.infer<
export type PartnerProfileLinkProps = z.infer<typeof PartnerProfileLinkSchema>;
-export type EnrolledPartnerProps = z.infer<
- typeof EnrolledPartnerSchemaExtended
->;
+export type EnrolledPartnerProps = z.infer<typeof EnrolledPartnerSchema>;
export type DiscountProps = z.infer<typeof DiscountSchema>;
diff --git a/apps/web/lib/zod/schemas/partners.ts b/apps/web/lib/zod/schemas/partners.ts
index 7de16c1842..35b85fd646 100644
--- a/apps/web/lib/zod/schemas/partners.ts
+++ b/apps/web/lib/zod/schemas/partners.ts
@@ -252,73 +252,6 @@ export const PartnerSchema = z
})
.merge(PartnerOnlinePresenceSchema);
-// Used externally by GET+POST /api/partners and partner.enrolled webhook
-export const EnrolledPartnerSchema = PartnerSchema.pick({
- id: true,
- name: true,
- email: true,
- image: true,
- description: true,
- country: true,
- website: true,
- youtube: true,
- twitter: true,
- linkedin: true,
- instagram: true,
- tiktok: true,
- paypalEmail: true,
- stripeConnectId: true,
- payoutsEnabledAt: true,
- createdAt: true,
-})
- .merge(
- ProgramEnrollmentSchema.pick({
- status: true,
- programId: true,
- tenantId: true,
- links: true,
- }),
- )
- .extend({
- clicks: z
- .number()
- .default(0)
- .describe("The total number of clicks on the partner's links."),
- leads: z
- .number()
- .default(0)
- .describe("The total number of leads generated by the partner's links."),
- sales: z
- .number()
- .default(0)
- .describe("The total number of sales generated by the partner's links."),
- saleAmount: z
- .number()
- .default(0)
- .describe(
- "The total amount of sales (in cents) generated by the partner's links.",
- ),
- totalCommissions: z
- .number()
- .default(0)
- .describe(
- "The total commissions paid to the partner for their referrals.",
- ),
- netRevenue: z
- .number()
- .default(0)
- .describe("The total net revenue generated by the partner."),
- earnings: z
- .number()
- .default(0)
- .describe(
- "DEPRECATED: The total earnings/commissions accrued by the partner's links.",
- )
- .openapi({
- deprecated: true,
- }),
- });
-
export const EnrolledPartnerBasicSchema = PartnerSchema.pick({
id: true,
name: true,
@@ -336,45 +269,51 @@ export const EnrolledPartnerBasicSchema = PartnerSchema.pick({
stripeConnectId: true,
payoutsEnabledAt: true,
createdAt: true,
-})
- .merge(PartnerOnlinePresenceSchema)
- .merge(
- ProgramEnrollmentSchema.pick({
- status: true,
- programId: true,
- tenantId: true,
- links: true,
- }),
- )
- .extend({
- applicationId: z
- .string()
- .nullish()
- .describe(
- "If the partner submitted an application to join the program, this is the ID of the application.",
- ),
- clickRewardId: z.string().nullish(),
- leadRewardId: z.string().nullish(),
- saleRewardId: z.string().nullish(),
- discountId: z.string().nullish(),
- bannedAt: z.date().nullish(),
- bannedReason: z
- .enum(Object.keys(BAN_PARTNER_REASONS) as [PartnerBannedReason])
- .nullish(),
- });
-
-export const EnrolledPartnerSchemaExtended = EnrolledPartnerBasicSchema.merge(
- EnrolledPartnerSchema.pick({
- clicks: true,
- leads: true,
- sales: true,
- saleAmount: true,
- totalCommissions: true,
- netRevenue: true,
- earnings: true,
+}).merge(
+ ProgramEnrollmentSchema.omit({
+ program: true,
}),
);
+// Used externally by GET+POST /api/partners and partner.enrolled webhook
+export const EnrolledPartnerSchema = EnrolledPartnerBasicSchema.extend({
+ clicks: z
+ .number()
+ .default(0)
+ .describe("The total number of clicks on the partner's links."),
+ leads: z
+ .number()
+ .default(0)
+ .describe("The total number of leads generated by the partner's links."),
+ sales: z
+ .number()
+ .default(0)
+ .describe("The total number of sales generated by the partner's links."),
+ saleAmount: z
+ .number()
+ .default(0)
+ .describe(
+ "The total amount of sales (in cents) generated by the partner's links.",
+ ),
+ totalCommissions: z
+ .number()
+ .default(0)
+ .describe("The total commissions paid to the partner for their referrals."),
+ netRevenue: z
+ .number()
+ .default(0)
+ .describe("The total net revenue generated by the partner."),
+ earnings: z
+ .number()
+ .default(0)
+ .describe(
+ "DEPRECATED: The total earnings/commissions accrued by the partner's links.",
+ )
+ .openapi({
+ deprecated: true,
+ }),
+});
+
export const LeaderboardPartnerSchema = z.object({
id: z.string(),
name: z.string(),
diff --git a/apps/web/lib/zod/schemas/programs.ts b/apps/web/lib/zod/schemas/programs.ts
index 64423940e7..952820c2c6 100644
--- a/apps/web/lib/zod/schemas/programs.ts
+++ b/apps/web/lib/zod/schemas/programs.ts
@@ -3,7 +3,11 @@ import {
DUB_PARTNERS_ANALYTICS_INTERVAL,
} from "@/lib/analytics/constants";
import { ALLOWED_MIN_PAYOUT_AMOUNTS } from "@/lib/partners/constants";
-import { LinkStructure, ProgramEnrollmentStatus } from "@dub/prisma/client";
+import {
+ LinkStructure,
+ PartnerBannedReason,
+ ProgramEnrollmentStatus,
+} from "@dub/prisma/client";
import { z } from "zod";
import { DiscountSchema } from "./discount";
import { LinkSchema } from "./links";
@@ -89,6 +93,7 @@ export const ProgramEnrollmentSchema = z.object({
),
programId: z.string().describe("The program's unique ID on Dub."),
program: ProgramSchema,
+ createdAt: z.date(),
status: z
.nativeEnum(ProgramEnrollmentStatus)
.describe("The status of the partner's enrollment in the program."),
@@ -98,8 +103,21 @@ export const ProgramEnrollmentSchema = z.object({
.describe("The partner's referral links in this program."),
totalCommissions: z.number().default(0),
rewards: z.array(RewardSchema).nullish(),
+ clickRewardId: z.string().nullish(),
+ leadRewardId: z.string().nullish(),
+ saleRewardId: z.string().nullish(),
+ discountId: z.string().nullish(),
discount: DiscountSchema.nullish(),
- createdAt: z.date(),
+ applicationId: z
+ .string()
+ .nullish()
+ .describe(
+ "If the partner submitted an application to join the program, this is the ID of the application.",
+ ),
+ bannedAt: z.date().nullish(),
+ bannedReason: z
+ .enum(Object.keys(PartnerBannedReason) as [PartnerBannedReason])
+ .nullish(),
});
export const ProgramInviteSchema = z.object({
diff --git a/apps/web/tests/partners/list-partners.test.ts b/apps/web/tests/partners/list-partners.test.ts
new file mode 100644
index 0000000000..36bf5dce50
--- /dev/null
+++ b/apps/web/tests/partners/list-partners.test.ts
@@ -0,0 +1,145 @@
+import { EnrolledPartnerProps } from "@/lib/types";
+import z from "@/lib/zod";
+import {
+ EnrolledPartnerBasicSchema as EnrolledPartnerBasicSchemaDate,
+ EnrolledPartnerSchema as EnrolledPartnerSchemaDate,
+} from "@/lib/zod/schemas/partners";
+import { describe, expect, test } from "vitest";
+import { IntegrationHarness } from "../utils/integration";
+import { E2E_PARTNER, E2E_PROGRAM } from "../utils/resource";
+
+// type coercion for date fields
+const EnrolledPartnerBasicSchema = EnrolledPartnerBasicSchemaDate.extend({
+ payoutsEnabledAt: z.string().nullable(),
+ createdAt: z.string(),
+});
+const EnrolledPartnerSchema = EnrolledPartnerSchemaDate.extend({
+ payoutsEnabledAt: z.string().nullable(),
+ createdAt: z.string(),
+});
+
+describe.sequential("GET /partners", async () => {
+ const h = new IntegrationHarness();
+ const { http } = await h.init();
+
+ test("returns list of partners with basic fields", async () => {
+ const { data, status } = await http.get<EnrolledPartnerProps[]>({
+ path: "/partners",
+ });
+
+ expect(status).toEqual(200);
+ expect(Array.isArray(data)).toBe(true);
+
+ if (data.length > 0) {
+ // Validate each partner against the basic schema
+ data.forEach((partner) => {
+ const parsed = EnrolledPartnerBasicSchema.parse(partner);
+ expect(parsed.programId).toBe(E2E_PROGRAM.id);
+ });
+ }
+ });
+
+ test("returns list of partners with expanded fields", async () => {
+ const { data, status } = await http.get<EnrolledPartnerProps[]>({
+ path: "/partners",
+ query: {
+ includeExpandedFields: "true",
+ },
+ });
+
+ expect(status).toEqual(200);
+ expect(Array.isArray(data)).toBe(true);
+
+ if (data.length > 0) {
+ // Validate each partner against the expanded schema
+ data.forEach((partner) => {
+ const parsed = EnrolledPartnerSchema.parse(partner);
+ expect(parsed.programId).toBe(E2E_PROGRAM.id);
+ });
+ }
+ });
+
+ test("filters partners by status", async () => {
+ const { data, status } = await http.get<EnrolledPartnerProps[]>({
+ path: "/partners",
+ query: {
+ status: "approved",
+ },
+ });
+
+ expect(status).toEqual(200);
+ expect(Array.isArray(data)).toBe(true);
+
+ // All partners should have approved status
+ data.forEach((partner) => {
+ const parsed = EnrolledPartnerBasicSchema.parse(partner);
+ expect(parsed.status).toBe("approved");
+ });
+ });
+
+ test("filters partners by country", async () => {
+ const { data, status } = await http.get<EnrolledPartnerProps[]>({
+ path: "/partners",
+ query: {
+ country: "US",
+ },
+ });
+
+ expect(status).toEqual(200);
+ expect(Array.isArray(data)).toBe(true);
+
+ // All partners should be from US
+ data.forEach((partner) => {
+ const parsed = EnrolledPartnerBasicSchema.parse(partner);
+ expect(parsed.country).toBe("US");
+ });
+ });
+
+ test("filters partners by tenantId", async () => {
+ const { data, status } = await http.get<EnrolledPartnerProps[]>({
+ path: "/partners",
+ query: {
+ tenantId: E2E_PARTNER.tenantId,
+ },
+ });
+
+ expect(status).toEqual(200);
+ expect(Array.isArray(data)).toBe(true);
+
+ // All partners should have the specified tenantId
+ data.forEach((partner) => {
+ const parsed = EnrolledPartnerBasicSchema.parse(partner);
+ expect(parsed.tenantId).toBe(E2E_PARTNER.tenantId);
+ });
+ });
+
+ test("filters partners by specific IDs", async () => {
+ // First get a list of partners to get some IDs
+ const { data: allPartners } = await http.get<EnrolledPartnerProps[]>({
+ path: "/partners",
+ });
+
+ if (allPartners.length > 0) {
+ const partnerIds = allPartners
+ .slice(0, 2)
+ .map((p) => EnrolledPartnerBasicSchema.parse(p).id);
+
+ const { data, status } = await http.get<EnrolledPartnerProps[]>({
+ path: "/partners",
+ query: {
+ ids: partnerIds.join(","),
+ },
+ });
+
+ expect(status).toEqual(200);
+ expect(Array.isArray(data)).toBe(true);
+ expect(data.length).toBeLessThanOrEqual(partnerIds.length);
+
+ // All returned partners should have one of the specified IDs
+ data.forEach((partner) => {
+ const parsed = EnrolledPartnerBasicSchema.parse(partner);
+ expect(partnerIds).toContain(parsed.id);
+ });
+ }
+ });
+});