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.

ExpandedCustomerSchema

Steven Teycd302c36a141
diff --git a/apps/web/app/api/customers/[id]/route.ts b/apps/web/app/api/customers/[id]/route.ts
index b62a58604e..3f7c3d448a 100644
--- a/apps/web/app/api/customers/[id]/route.ts
+++ b/apps/web/app/api/customers/[id]/route.ts
@@ -4,6 +4,7 @@ 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";
@@ -24,7 +25,14 @@ export const GET = withWorkspace(
},
);
- return NextResponse.json(CustomerSchema.parse(customer));
+ return NextResponse.json(
+ ExpandedCustomerSchema.parse({
+ ...customer,
+ partner: {
+ couponId: customer.link?.programEnrollment?.couponId ?? null,
+ },
+ }),
+ );
},
{
requiredAddOn: "conversion",
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts
index 95ae990915..f88e00c313 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts
@@ -1,5 +1,4 @@
import { withWorkspace } from "@/lib/auth";
-import { getDubCustomer } from "@/lib/dub";
import { stripe } from "@/lib/stripe";
import { APP_DOMAIN } from "@dub/utils";
import { NextResponse } from "next/server";
@@ -47,9 +46,7 @@ export const POST = withWorkspace(async ({ req, workspace, session }) => {
});
return NextResponse.json({ url });
} else {
- const customer = await getDubCustomer(session.user.id);
- const isReferral =
- customer?.link?.programId === "prog_d8pl69xXCv4AoHNT281pHQdo";
+ // const customer = await getDubCustomer(session.user.id);
// For both new users and users with canceled subscriptions
const stripeSession = await stripe.checkout.sessions.create({
@@ -69,7 +66,7 @@ export const POST = withWorkspace(async ({ req, workspace, session }) => {
success_url: `${APP_DOMAIN}/${workspace.slug}?${onboarding ? "onboarded" : "upgraded"}=true&plan=${plan}&period=${period}`,
cancel_url: baseUrl,
line_items: [{ price: prices.data[0].id, quantity: 1 }],
- ...(isReferral
+ ...(false
? {
discounts: [
{
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 990543040e..285ab72f15 100644
--- a/apps/web/lib/api/customers/get-customer-or-throw.ts
+++ b/apps/web/lib/api/customers/get-customer-or-throw.ts
@@ -1,4 +1,5 @@
import { prisma } from "@dub/prisma";
+import { Customer, Link, ProgramEnrollment } from "@dub/prisma/client";
import { DubApiError } from "../errors";
export const getCustomerOrThrow = async (
@@ -9,8 +10,16 @@ export const getCustomerOrThrow = async (
id: string;
workspaceId: string;
},
- { expand }: { expand?: ("link" | "project")[] } = {},
-) => {
+ { expand }: { expand?: "link"[] } = {},
+): Promise<
+ Customer & {
+ link?:
+ | (Link & {
+ programEnrollment?: ProgramEnrollment | null;
+ })
+ | null;
+ }
+> => {
const customer = await prisma.customer.findUnique({
where: {
...(id.startsWith("ext_")
@@ -22,9 +31,9 @@ export const getCustomerOrThrow = async (
}
: { id }),
},
- include: {
- link: expand?.includes("link"),
- },
+ ...(expand?.includes("link")
+ ? { include: { link: { include: { programEnrollment: true } } } }
+ : {}),
});
if (!customer || customer.projectId !== workspaceId) {
diff --git a/apps/web/lib/openapi/customers/get-customer.ts b/apps/web/lib/openapi/customers/get-customer.ts
index 32bb661e7e..294d7b8e82 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 { CustomerSchema } from "../../zod/schemas/customers";
+import { ExpandedCustomerSchema } 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: CustomerSchema.pick({ id: true }),
+ path: ExpandedCustomerSchema.pick({ id: true }),
},
responses: {
"200": {
description: "The customer object.",
content: {
"application/json": {
- schema: CustomerSchema,
+ schema: ExpandedCustomerSchema,
},
},
},
diff --git a/apps/web/lib/zod/schemas/customers.ts b/apps/web/lib/zod/schemas/customers.ts
index 836c179f1a..ca49528466 100644
--- a/apps/web/lib/zod/schemas/customers.ts
+++ b/apps/web/lib/zod/schemas/customers.ts
@@ -37,15 +37,23 @@ 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."),
- link: LinkSchema.pick({
- id: true,
- domain: true,
- key: true,
- shortLink: true,
- programId: true,
- }).nullish(),
});
+export const ExpandedCustomerSchema = CustomerSchema.merge(
+ z.object({
+ link: LinkSchema.pick({
+ id: true,
+ domain: true,
+ key: true,
+ shortLink: true,
+ programId: true,
+ }).nullish(),
+ partner: z.object({
+ couponId: z.string().nullish(),
+ }),
+ }),
+);
+
export const CUSTOMERS_MAX_PAGE_SIZE = 100;
export const customersQuerySchema = z
diff --git a/apps/web/lib/zod/schemas/partners.ts b/apps/web/lib/zod/schemas/partners.ts
index ba9b9758a4..d159ded7e3 100644
--- a/apps/web/lib/zod/schemas/partners.ts
+++ b/apps/web/lib/zod/schemas/partners.ts
@@ -51,6 +51,7 @@ export const PartnerSchema = z.object({
bio: z.string().nullable(),
status: z.nativeEnum(PartnerStatus),
stripeConnectId: z.string().nullable(),
+ couponId: z.string().nullish(),
payoutsEnabled: z.boolean(),
createdAt: z.date(),
updatedAt: z.date(),