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.

2.3 KB
Raw Blame
import { DubApiError } from "@/lib/api/errors";
import { withWorkspace } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
import { stripeAppClient } from "@/lib/stripe";
import { StripeCustomerSchema } from "@/lib/zod/schemas/customers";
import { STRIPE_INTEGRATION_ID } from "@dub/utils";
import { NextResponse } from "next/server";
import * as z from "zod/v4";

export const GET = withWorkspace(async ({ workspace, searchParams }) => {
  const { search } = z
    .object({
      search: z.string(),
    })
    .parse(searchParams);

  if (!workspace.stripeConnectId) {
    throw new DubApiError({
      code: "bad_request",
      message:
        "Your workspace isn't connected to Stripe yet. Please install the Stripe integration under /settings/integrations/stripe to proceed.",
    });
  }

  const installedStripeIntegration =
    await prisma.installedIntegration.findFirst({
      where: {
        projectId: workspace.id,
        integrationId: STRIPE_INTEGRATION_ID,
      },
      select: {
        id: true,
      },
    });

  if (!installedStripeIntegration) {
    throw new DubApiError({
      code: "bad_request",
      message:
        "The Stripe integration is not installed on your workspace. Please install the Stripe integration under /settings/integrations/stripe to proceed.",
    });
  }

  const stripe = stripeAppClient({
    mode: workspace.stripeConnectMode,
  });

  const { data } = await stripe.customers.search(
    {
      query: `email~"${search}"`,
      limit: 100,
      expand: ["data.subscriptions"],
    },
    {
      stripeAccount: workspace.stripeConnectId,
    },
  );

  const existingCustomers = await prisma.customer.findMany({
    where: {
      stripeCustomerId: {
        in: data.map((customer) => customer.id),
      },
      projectId: workspace.id,
    },
    select: {
      id: true,
      stripeCustomerId: true,
    },
  });

  const stripeCustomers = StripeCustomerSchema.array().parse(
    data.map((customer) => ({
      id: customer.id,
      email: customer.email,
      name: customer.name,
      country: customer.address?.country ?? null,
      subscriptions: customer.subscriptions?.data.length ?? 0,
      dubCustomerId:
        existingCustomers.find((c) => c.stripeCustomerId === customer.id)?.id ??
        null,
    })),
  );

  return NextResponse.json(stripeCustomers);
});