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.

includeExpandedFields

Steven Teyb5b8041b6576
diff --git a/apps/web/app/api/customers/[id]/route.ts b/apps/web/app/api/customers/[id]/route.ts
index 8e37f7fe90..4ce232557a 100644
--- a/apps/web/app/api/customers/[id]/route.ts
+++ b/apps/web/app/api/customers/[id]/route.ts
@@ -5,6 +5,7 @@ import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
import {
CustomerSchema,
+ getCustomersQuerySchema,
updateCustomerBodySchema,
} from "@/lib/zod/schemas/customers";
import { prisma } from "@dub/prisma";
@@ -12,17 +13,17 @@ import { NextResponse } from "next/server";
// GET /api/customers/:id – Get a customer by ID
export const GET = withWorkspace(
- async ({ workspace, params }) => {
+ async ({ workspace, params, searchParams }) => {
const { id } = params;
+ const { includeExpandedFields } =
+ getCustomersQuerySchema.parse(searchParams);
const customer = await getCustomerOrThrow(
{
id,
workspaceId: workspace.id,
},
- {
- expand: ["link"],
- },
+ { includeExpandedFields },
);
return NextResponse.json(CustomerSchema.parse(transformCustomer(customer)));
@@ -34,8 +35,10 @@ export const GET = withWorkspace(
// PATCH /api/customers/:id – Update a customer by ID
export const PATCH = withWorkspace(
- async ({ workspace, params, req }) => {
+ async ({ workspace, params, req, searchParams }) => {
const { id } = params;
+ const { includeExpandedFields } =
+ getCustomersQuerySchema.parse(searchParams);
const { name, email, avatar, externalId } = updateCustomerBodySchema.parse(
await parseRequestBody(req),
@@ -52,18 +55,22 @@ export const PATCH = withWorkspace(
id: customer.id,
},
data: { name, email, avatar, externalId },
- include: {
- link: {
- include: {
- programEnrollment: {
- include: {
- partner: true,
- discount: true,
+ ...(includeExpandedFields
+ ? {
+ include: {
+ link: {
+ include: {
+ programEnrollment: {
+ include: {
+ partner: true,
+ discount: true,
+ },
+ },
+ },
},
},
- },
- },
- },
+ }
+ : {}),
});
return NextResponse.json(
diff --git a/apps/web/app/api/customers/route.ts b/apps/web/app/api/customers/route.ts
index 863331dbf2..6031583b52 100644
--- a/apps/web/app/api/customers/route.ts
+++ b/apps/web/app/api/customers/route.ts
@@ -14,7 +14,8 @@ import { NextResponse } from "next/server";
// GET /api/customers – Get all customers
export const GET = withWorkspace(
async ({ workspace, searchParams }) => {
- const { email, externalId } = getCustomersQuerySchema.parse(searchParams);
+ const { email, externalId, includeExpandedFields } =
+ getCustomersQuerySchema.parse(searchParams);
const customers = await prisma.customer.findMany({
where: {
@@ -26,18 +27,22 @@ export const GET = withWorkspace(
orderBy: {
createdAt: "desc",
},
- include: {
- link: {
- include: {
- programEnrollment: {
- include: {
- partner: true,
- discount: true,
+ ...(includeExpandedFields
+ ? {
+ include: {
+ link: {
+ include: {
+ programEnrollment: {
+ include: {
+ partner: true,
+ discount: true,
+ },
+ },
+ },
},
},
- },
- },
- },
+ }
+ : {}),
});
return NextResponse.json(
diff --git a/apps/web/lib/api/customers/get-customer-or-throw.ts b/apps/web/lib/api/customers/get-customer-or-throw.ts
index 95795a0e7a..7d67561d73 100644
--- a/apps/web/lib/api/customers/get-customer-or-throw.ts
+++ b/apps/web/lib/api/customers/get-customer-or-throw.ts
@@ -10,7 +10,11 @@ export const getCustomerOrThrow = async (
id: string;
workspaceId: string;
},
- { expand }: { expand?: "link"[] } = {},
+ {
+ includeExpandedFields = false,
+ }: {
+ includeExpandedFields?: boolean;
+ } = {},
): Promise<CustomerWithLink> => {
const customer = await prisma.customer.findUnique({
where: {
@@ -23,7 +27,7 @@ export const getCustomerOrThrow = async (
}
: { id }),
},
- ...(expand?.includes("link")
+ ...(includeExpandedFields
? {
include: {
link: {
diff --git a/apps/web/lib/api/customers/transform-customer.ts b/apps/web/lib/api/customers/transform-customer.ts
index e146ccce7c..6fd2f3c882 100644
--- a/apps/web/lib/api/customers/transform-customer.ts
+++ b/apps/web/lib/api/customers/transform-customer.ts
@@ -23,8 +23,8 @@ export const transformCustomer = (customer: CustomerWithLink) => {
const programEnrollment = customer.link?.programEnrollment;
return {
...customer,
- link: customer.link || null,
- partner: programEnrollment?.partner || null,
- discount: programEnrollment?.discount || null,
+ link: customer.link || undefined,
+ partner: programEnrollment?.partner || undefined,
+ discount: programEnrollment?.discount || undefined,
};
};
diff --git a/apps/web/lib/openapi/customers/get-customer.ts b/apps/web/lib/openapi/customers/get-customer.ts
index 32bb661e7e..8236f0a8ae 100644
--- a/apps/web/lib/openapi/customers/get-customer.ts
+++ b/apps/web/lib/openapi/customers/get-customer.ts
@@ -1,6 +1,9 @@
import { openApiErrorResponses } from "@/lib/openapi/responses";
import { ZodOpenApiOperationObject } from "zod-openapi";
-import { CustomerSchema } from "../../zod/schemas/customers";
+import {
+ CustomerSchema,
+ getCustomersQuerySchema,
+} from "../../zod/schemas/customers";
export const getCustomer: ZodOpenApiOperationObject = {
operationId: "getCustomer",
@@ -9,6 +12,7 @@ export const getCustomer: ZodOpenApiOperationObject = {
description: "Retrieve a customer by ID for the authenticated workspace.",
requestParams: {
path: CustomerSchema.pick({ id: true }),
+ query: getCustomersQuerySchema.pick({ includeExpandedFields: true }),
},
responses: {
"200": {
diff --git a/apps/web/lib/zod/schemas/customers.ts b/apps/web/lib/zod/schemas/customers.ts
index a04aa7b027..6eefca2444 100644
--- a/apps/web/lib/zod/schemas/customers.ts
+++ b/apps/web/lib/zod/schemas/customers.ts
@@ -1,7 +1,7 @@
import z from "@/lib/zod";
import { DiscountSchema } from "./discount";
import { LinkSchema } from "./links";
-import { getPaginationQuerySchema } from "./misc";
+import { booleanQuerySchema, getPaginationQuerySchema } from "./misc";
export const getCustomersQuerySchema = z.object({
email: z
@@ -16,6 +16,11 @@ export const getCustomersQuerySchema = z.object({
.describe(
"A case-sensitive filter on the list based on the customer's `externalId` field. The value must be a string.",
),
+ includeExpandedFields: booleanQuerySchema
+ .optional()
+ .describe(
+ "Whether to include expanded fields on the customer (`link`, `partner`, `discount`).",
+ ),
});
export const createCustomerBodySchema = z.object({