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.

transformCustomer

Steven Teyd4cc92ee0cd9
diff --git a/apps/web/app/api/customers/[id]/route.ts b/apps/web/app/api/customers/[id]/route.ts
index 3f7c3d448a..8071442128 100644
--- a/apps/web/app/api/customers/[id]/route.ts
+++ b/apps/web/app/api/customers/[id]/route.ts
@@ -1,10 +1,10 @@
import { getCustomerOrThrow } from "@/lib/api/customers/get-customer-or-throw";
+import { transformCustomer } from "@/lib/api/customers/transform-customer";
import { DubApiError } from "@/lib/api/errors";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
import {
CustomerSchema,
- ExpandedCustomerSchema,
updateCustomerBodySchema,
} from "@/lib/zod/schemas/customers";
import { prisma } from "@dub/prisma";
@@ -25,14 +25,7 @@ export const GET = withWorkspace(
},
);
- return NextResponse.json(
- ExpandedCustomerSchema.parse({
- ...customer,
- partner: {
- couponId: customer.link?.programEnrollment?.couponId ?? null,
- },
- }),
- );
+ return NextResponse.json(CustomerSchema.parse(transformCustomer(customer)));
},
{
requiredAddOn: "conversion",
@@ -60,11 +53,17 @@ export const PATCH = withWorkspace(
},
data: { name, email, avatar, externalId },
include: {
- link: true,
+ link: {
+ include: {
+ programEnrollment: true,
+ },
+ },
},
});
- return NextResponse.json(CustomerSchema.parse(updatedCustomer));
+ return NextResponse.json(
+ CustomerSchema.parse(transformCustomer(updatedCustomer)),
+ );
} catch (error) {
if (error.code === "P2002") {
throw new DubApiError({
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 285ab72f15..510986bc5e 100644
--- a/apps/web/lib/api/customers/get-customer-or-throw.ts
+++ b/apps/web/lib/api/customers/get-customer-or-throw.ts
@@ -1,6 +1,6 @@
import { prisma } from "@dub/prisma";
-import { Customer, Link, ProgramEnrollment } from "@dub/prisma/client";
import { DubApiError } from "../errors";
+import { CustomerWithLink } from "./transform-customer";
export const getCustomerOrThrow = async (
{
@@ -11,15 +11,7 @@ export const getCustomerOrThrow = async (
workspaceId: string;
},
{ expand }: { expand?: "link"[] } = {},
-): Promise<
- Customer & {
- link?:
- | (Link & {
- programEnrollment?: ProgramEnrollment | null;
- })
- | null;
- }
-> => {
+): Promise<CustomerWithLink> => {
const customer = await prisma.customer.findUnique({
where: {
...(id.startsWith("ext_")
diff --git a/apps/web/lib/api/customers/transform-customer.ts b/apps/web/lib/api/customers/transform-customer.ts
new file mode 100644
index 0000000000..366b06f202
--- /dev/null
+++ b/apps/web/lib/api/customers/transform-customer.ts
@@ -0,0 +1,22 @@
+import { Customer, Link, ProgramEnrollment } from "@dub/prisma/client";
+
+export interface CustomerWithLink extends Customer {
+ link?:
+ | (Link & {
+ programEnrollment?: ProgramEnrollment | null;
+ })
+ | null;
+}
+
+export const transformCustomer = (customer: CustomerWithLink) => {
+ return {
+ ...customer,
+ partner: customer.link?.programEnrollment
+ ? {
+ id: customer.link.programEnrollment.partnerId,
+ shortLink: customer.link.shortLink,
+ couponId: customer.link.programEnrollment.couponId,
+ }
+ : null,
+ };
+};
diff --git a/apps/web/lib/openapi/customers/get-customer.ts b/apps/web/lib/openapi/customers/get-customer.ts
index 294d7b8e82..32bb661e7e 100644
--- a/apps/web/lib/openapi/customers/get-customer.ts
+++ b/apps/web/lib/openapi/customers/get-customer.ts
@@ -1,6 +1,6 @@
import { openApiErrorResponses } from "@/lib/openapi/responses";
import { ZodOpenApiOperationObject } from "zod-openapi";
-import { ExpandedCustomerSchema } from "../../zod/schemas/customers";
+import { CustomerSchema } from "../../zod/schemas/customers";
export const getCustomer: ZodOpenApiOperationObject = {
operationId: "getCustomer",
@@ -8,14 +8,14 @@ export const getCustomer: ZodOpenApiOperationObject = {
summary: "Retrieve a customer",
description: "Retrieve a customer by ID for the authenticated workspace.",
requestParams: {
- path: ExpandedCustomerSchema.pick({ id: true }),
+ path: CustomerSchema.pick({ id: true }),
},
responses: {
"200": {
description: "The customer object.",
content: {
"application/json": {
- schema: ExpandedCustomerSchema,
+ schema: CustomerSchema,
},
},
},
diff --git a/apps/web/lib/zod/schemas/customers.ts b/apps/web/lib/zod/schemas/customers.ts
index ca49528466..eda22a1ccb 100644
--- a/apps/web/lib/zod/schemas/customers.ts
+++ b/apps/web/lib/zod/schemas/customers.ts
@@ -37,22 +37,14 @@ export const CustomerSchema = z.object({
avatar: z.string().nullish().describe("Avatar URL of the customer."),
country: z.string().nullish().describe("Country of the customer."),
createdAt: z.date().describe("The date the customer was created."),
-});
-
-export const ExpandedCustomerSchema = CustomerSchema.merge(
- z.object({
- link: LinkSchema.pick({
- id: true,
- domain: true,
- key: true,
- shortLink: true,
- programId: true,
- }).nullish(),
- partner: z.object({
+ partner: z
+ .object({
+ id: z.string().nullish(),
+ shortLink: z.string().nullish(),
couponId: z.string().nullish(),
- }),
- }),
-);
+ })
+ .nullish(),
+});
export const CUSTOMERS_MAX_PAGE_SIZE = 100;