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.

1.9 KB
Raw Blame
import { prisma } from "@/lib/prisma";
import { Prisma } from "@prisma/client";

/**
 * Race-safe customer get-or-create.
 *
 * Prisma's `customer.upsert()` can still throw P2002 under MySQL concurrency
 * (two requests miss the row, both attempt insert). This helper finds first,
 * creates if missing, and on unique-constraint conflict falls back to find
 * using `where`, so concurrent track-lead / track-sale / etc. share one path
 * instead of failing the request.
 */
export async function getOrCreateCustomer({
  where,
  create,
  findMode = "unique",
}: {
  where: Prisma.CustomerWhereUniqueInput | Prisma.CustomerWhereInput;
  create: Prisma.CustomerUncheckedCreateInput;
  findMode?: "first" | "unique";
}) {
  const customer =
    findMode === "first"
      ? await prisma.customer.findFirst({
          where: where as Prisma.CustomerWhereInput,
        })
      : await prisma.customer.findUnique({
          where: where as Prisma.CustomerWhereUniqueInput,
        });

  if (customer) {
    return {
      customer,
      created: false,
    };
  }

  try {
    const customer = await prisma.customer.create({
      data: create,
    });

    return {
      customer,
      created: true,
    };
  } catch (error) {
    if (
      error instanceof Prisma.PrismaClientKnownRequestError &&
      error.code === "P2002"
    ) {
      console.info(
        "[getOrCreateCustomer] Unique constraint conflict (P2002), falling back to find",
        { target: error.meta?.target },
      );

      const customer =
        findMode === "first"
          ? await prisma.customer.findFirstOrThrow({
              where: where as Prisma.CustomerWhereInput,
            })
          : await prisma.customer.findUniqueOrThrow({
              where: where as Prisma.CustomerWhereUniqueInput,
            });

      return {
        customer,
        created: false,
      };
    }

    throw error;
  }
}