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 to EnrolledPartnerSchema

Steven Tey3f71fb2e740d
diff --git a/apps/web/app/(ee)/api/partners/route.ts b/apps/web/app/(ee)/api/partners/route.ts
index 705c7baa7f..3ccb1e865e 100644
--- a/apps/web/app/(ee)/api/partners/route.ts
+++ b/apps/web/app/(ee)/api/partners/route.ts
@@ -7,9 +7,8 @@ import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
import {
createPartnerSchema,
- EnrolledPartnerBasicSchema,
EnrolledPartnerSchema,
- partnersQuerySchemaExtended,
+ getPartnersQuerySchemaExtended,
} from "@/lib/zod/schemas/partners";
import { NextResponse } from "next/server";
import { z } from "zod";
@@ -18,7 +17,7 @@ import { z } from "zod";
export const GET = withWorkspace(
async ({ workspace, searchParams }) => {
const programId = getDefaultProgramIdOrThrow(workspace);
- const parsedParams = partnersQuerySchemaExtended.parse(searchParams);
+ const parsedParams = getPartnersQuerySchemaExtended.parse(searchParams);
const partners = await getPartners({
...parsedParams,
@@ -26,11 +25,7 @@ export const GET = withWorkspace(
programId,
});
- const responseSchema = parsedParams.includeExpandedFields
- ? EnrolledPartnerSchema
- : EnrolledPartnerBasicSchema;
-
- return NextResponse.json(z.array(responseSchema).parse(partners));
+ return NextResponse.json(z.array(EnrolledPartnerSchema).parse(partners));
},
{
requiredPlan: [
diff --git a/apps/web/lib/api/partners/get-partners.ts b/apps/web/lib/api/partners/get-partners.ts
index 4429e9c862..8548e0fd2f 100644
--- a/apps/web/lib/api/partners/get-partners.ts
+++ b/apps/web/lib/api/partners/get-partners.ts
@@ -1,4 +1,4 @@
-import { partnersQuerySchemaExtended } from "@/lib/zod/schemas/partners";
+import { getPartnersQuerySchemaExtended } from "@/lib/zod/schemas/partners";
import { prisma } from "@dub/prisma";
import { Prisma } from "@prisma/client";
import { z } from "zod";
@@ -24,7 +24,7 @@ const sortColumnExtraMap = {
netRevenue: "totalSaleAmount",
};
-type PartnerFilters = z.infer<typeof partnersQuerySchemaExtended> & {
+type PartnerFilters = z.infer<typeof getPartnersQuerySchemaExtended> & {
workspaceId: string;
programId: string;
};
diff --git a/apps/web/lib/openapi/partners/index.ts b/apps/web/lib/openapi/partners/index.ts
index 78fd922786..1163138f3b 100644
--- a/apps/web/lib/openapi/partners/index.ts
+++ b/apps/web/lib/openapi/partners/index.ts
@@ -1,7 +1,7 @@
import { ZodOpenApiPathsObject } from "zod-openapi";
import { createPartner } from "./create-partner";
import { createPartnerLink } from "./create-partner-link";
-import { getPartners } from "./get-partners";
+import { listPartners } from "./list-partners";
import { retrievePartnerAnalytics } from "./retrieve-analytics";
import { retrievePartnerLinks } from "./retrieve-partner-links";
import { upsertPartnerLink } from "./upsert-partner-link";
@@ -9,7 +9,7 @@ import { upsertPartnerLink } from "./upsert-partner-link";
export const partnersPaths: ZodOpenApiPathsObject = {
"/partners": {
post: createPartner,
- get: getPartners,
+ get: listPartners,
},
"/partners/links": {
post: createPartnerLink,
diff --git a/apps/web/lib/openapi/partners/get-partners.ts b/apps/web/lib/openapi/partners/list-partners.ts
similarity index 69%
rename from apps/web/lib/openapi/partners/get-partners.ts
rename to apps/web/lib/openapi/partners/list-partners.ts
index 2782aa0aaa..6b860e5256 100644
--- a/apps/web/lib/openapi/partners/get-partners.ts
+++ b/apps/web/lib/openapi/partners/list-partners.ts
@@ -2,17 +2,17 @@ import { openApiErrorResponses } from "@/lib/openapi/responses";
import z from "@/lib/zod";
import {
EnrolledPartnerSchema,
- partnersQuerySchema,
+ getPartnersQuerySchema,
} from "@/lib/zod/schemas/partners";
import { ZodOpenApiOperationObject } from "zod-openapi";
-export const getPartners: ZodOpenApiOperationObject = {
- operationId: "getPartners",
+export const listPartners: ZodOpenApiOperationObject = {
+ operationId: "listPartners",
"x-speakeasy-name-override": "list",
- summary: "Retrieve a list of partners",
- description: "Retrieve a list of partners for the program.",
+ summary: "List all partners",
+ description: "List all partners for a partner program.",
requestParams: {
- query: partnersQuerySchema,
+ query: getPartnersQuerySchema,
},
responses: {
"200": {
diff --git a/apps/web/lib/swr/use-partners.ts b/apps/web/lib/swr/use-partners.ts
index 13fd671dbb..e3effa1a6f 100644
--- a/apps/web/lib/swr/use-partners.ts
+++ b/apps/web/lib/swr/use-partners.ts
@@ -2,10 +2,10 @@ import { fetcher } from "@dub/utils";
import useSWR, { SWRConfiguration } from "swr";
import { z } from "zod";
import { EnrolledPartnerProps } from "../types";
-import { partnersQuerySchemaExtended } from "../zod/schemas/partners";
+import { getPartnersQuerySchemaExtended } from "../zod/schemas/partners";
import useWorkspace from "./use-workspace";
-const partialQuerySchema = partnersQuerySchemaExtended.partial();
+const partialQuerySchema = getPartnersQuerySchemaExtended.partial();
export default function usePartners(
{
diff --git a/apps/web/lib/zod/schemas/partners.ts b/apps/web/lib/zod/schemas/partners.ts
index 02d64a8a2c..2666ebf3b5 100644
--- a/apps/web/lib/zod/schemas/partners.ts
+++ b/apps/web/lib/zod/schemas/partners.ts
@@ -49,7 +49,7 @@ export const exportPartnersColumnsDefault = exportPartnerColumns
.filter((column) => column.default)
.map((column) => column.id);
-export const partnersQuerySchema = z
+export const getPartnersQuerySchema = z
.object({
status: z
.nativeEnum(ProgramEnrollmentStatus)
@@ -91,7 +91,7 @@ export const partnersQuerySchema = z
includeExpandedFields: booleanQuerySchema
.optional()
.describe(
- "Whether to include expanded fields on the partner (`reward`, `discount`, `online-presence`).",
+ "Whether to include stats fields on the partner (`clicks`, `leads`, `sales`, `saleAmount`, `commissions`, `netRevenue`). If false, those fields will be returned as 0.",
)
.openapi({ example: "true" }),
search: z
@@ -104,7 +104,7 @@ export const partnersQuerySchema = z
})
.merge(getPaginationQuerySchema({ pageSize: PARTNERS_MAX_PAGE_SIZE }));
-export const partnersQuerySchemaExtended = partnersQuerySchema.merge(
+export const getPartnersQuerySchemaExtended = getPartnersQuerySchema.merge(
z.object({
partnerIds: z
.union([z.string(), z.array(z.string())])
@@ -116,7 +116,7 @@ export const partnersQuerySchemaExtended = partnersQuerySchema.merge(
}),
);
-export const partnersExportQuerySchema = partnersQuerySchemaExtended
+export const partnersExportQuerySchema = getPartnersQuerySchemaExtended
.omit({ page: true, pageSize: true })
.merge(
z.object({
@@ -127,7 +127,7 @@ export const partnersExportQuerySchema = partnersQuerySchemaExtended
}),
);
-export const partnersCountQuerySchema = partnersQuerySchemaExtended
+export const partnersCountQuerySchema = getPartnersQuerySchemaExtended
.omit({
sortBy: true,
sortOrder: true,
@@ -252,7 +252,8 @@ export const PartnerSchema = z
})
.merge(PartnerOnlinePresenceSchema);
-export const EnrolledPartnerBasicSchema = PartnerSchema.pick({
+// Used externally by GET+POST /api/partners and partner.enrolled webhook
+export const EnrolledPartnerSchema = PartnerSchema.pick({
id: true,
name: true,
email: true,
@@ -268,46 +269,46 @@ export const EnrolledPartnerBasicSchema = PartnerSchema.pick({
program: true,
}),
)
- .merge(PartnerOnlinePresenceSchema);
-
-// 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,
- }),
-});
+ .merge(PartnerOnlinePresenceSchema)
+ .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(),
diff --git a/apps/web/tests/partners/list-partners.test.ts b/apps/web/tests/partners/list-partners.test.ts
index 2cdc88f3f5..11fba782c9 100644
--- a/apps/web/tests/partners/list-partners.test.ts
+++ b/apps/web/tests/partners/list-partners.test.ts
@@ -1,17 +1,11 @@
import { EnrolledPartnerProps } from "@/lib/types";
-import {
- EnrolledPartnerBasicSchema as EnrolledPartnerBasicSchemaDate,
- EnrolledPartnerSchema as EnrolledPartnerSchemaDate,
-} from "@/lib/zod/schemas/partners";
+import { 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";
import { normalizedPartnerDateFields } from "./resource";
// type coercion for date fields
-const EnrolledPartnerBasicSchema = EnrolledPartnerBasicSchemaDate.merge(
- normalizedPartnerDateFields,
-);
const EnrolledPartnerSchema = EnrolledPartnerSchemaDate.merge(
normalizedPartnerDateFields,
);
@@ -31,7 +25,7 @@ describe.sequential("GET /partners", async () => {
if (data.length > 0) {
// Validate each partner against the basic schema
data.forEach((partner) => {
- const parsed = EnrolledPartnerBasicSchema.parse(partner);
+ const parsed = EnrolledPartnerSchema.parse(partner);
expect(parsed.programId).toBe(E2E_PROGRAM.id);
});
}
@@ -70,7 +64,7 @@ describe.sequential("GET /partners", async () => {
// All partners should have approved status
data.forEach((partner) => {
- const parsed = EnrolledPartnerBasicSchema.parse(partner);
+ const parsed = EnrolledPartnerSchema.parse(partner);
expect(parsed.status).toBe("approved");
});
});
@@ -88,7 +82,7 @@ describe.sequential("GET /partners", async () => {
// All partners should be from US
data.forEach((partner) => {
- const parsed = EnrolledPartnerBasicSchema.parse(partner);
+ const parsed = EnrolledPartnerSchema.parse(partner);
expect(parsed.country).toBe("US");
});
});
@@ -106,7 +100,7 @@ describe.sequential("GET /partners", async () => {
// All partners should have the specified tenantId
data.forEach((partner) => {
- const parsed = EnrolledPartnerBasicSchema.parse(partner);
+ const parsed = EnrolledPartnerSchema.parse(partner);
expect(parsed.tenantId).toBe(E2E_PARTNER.tenantId);
});
});