The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Fix customer discount implementation
Steven Teyf690f52bccd7
diff --git a/apps/web/app/(ee)/api/customers/[id]/route.ts b/apps/web/app/(ee)/api/customers/[id]/route.tsindex 77b5c67d5e..cb2959886d 100644--- a/apps/web/app/(ee)/api/customers/[id]/route.ts+++ b/apps/web/app/(ee)/api/customers/[id]/route.ts@@ -1,4 +1,3 @@-import { determineCustomerDiscount } from "@/lib/api/customers/determine-customer-discount";import { getCustomerOrThrow } from "@/lib/api/customers/get-customer-or-throw";import { transformCustomer } from "@/lib/api/customers/transform-customer";import { DubApiError } from "@/lib/api/errors";@@ -13,7 +12,6 @@ import {} from "@/lib/zod/schemas/customers";import { prisma } from "@dub/prisma";import { nanoid, R2_URL } from "@dub/utils";-import { Discount } from "@prisma/client";import { waitUntil } from "@vercel/functions";import { NextResponse } from "next/server";@@ -34,40 +32,11 @@ export const GET = withWorkspace(},);- let discount: Discount | null = null;-- if (includeExpandedFields) {- const firstPurchase = await prisma.commission.findFirst({- where: {- customerId: customer.id,- type: "sale",- },- orderBy: {- createdAt: "asc",- },- select: {- createdAt: true,- },- });-- discount = determineCustomerDiscount({- customerLink: customer.link,- firstPurchase,- });- }-const responseSchema = includeExpandedFields? CustomerEnrichedSchema: CustomerSchema;- return NextResponse.json(- responseSchema.parse(- transformCustomer({- ...customer,- ...(includeExpandedFields ? { discount } : {}),- }),- ),- );+ return NextResponse.json(responseSchema.parse(transformCustomer(customer)));},{requiredPlan: [@@ -143,28 +112,6 @@ export const PATCH = withWorkspace();}- let discount: Discount | null = null;-- if (includeExpandedFields) {- const firstPurchase = await prisma.commission.findFirst({- where: {- customerId: customer.id,- type: "sale",- },- orderBy: {- createdAt: "asc",- },- select: {- createdAt: true,- },- });-- discount = determineCustomerDiscount({- customerLink: customer.link,- firstPurchase,- });- }-const responseSchema = includeExpandedFields? CustomerEnrichedSchema: CustomerSchema;@@ -174,7 +121,6 @@ export const PATCH = withWorkspace(transformCustomer({...customer,...updatedCustomer,- ...(includeExpandedFields ? { discount } : {}),}),),);diff --git a/apps/web/app/(ee)/api/customers/route.ts b/apps/web/app/(ee)/api/customers/route.tsindex 001e6b5791..52c04fafda 100644--- a/apps/web/app/(ee)/api/customers/route.ts+++ b/apps/web/app/(ee)/api/customers/route.ts@@ -1,5 +1,4 @@import { createId } from "@/lib/api/create-id";-import { determineCustomerDiscount } from "@/lib/api/customers/determine-customer-discount";import { transformCustomer } from "@/lib/api/customers/transform-customer";import { DubApiError } from "@/lib/api/errors";import { parseRequestBody } from "@/lib/api/utils";@@ -138,29 +137,8 @@ export const GET = withWorkspace(},distinct: ["customerId"],});-- const firstPurchaseMap = new Map(- firstPurchases.map((purchase) => [purchase.customerId, purchase]),- );-- customers.forEach((customer) => {- discounts.set(- customer.id,- determineCustomerDiscount({- customerLink: customer.link,- firstPurchase: firstPurchaseMap.get(customer.id),- }),- );- });}- const processedCustomers = customers.map((customer) => {- return {- ...customer,- discount: discounts.get(customer.id),- };- });-const responseSchema = includeExpandedFields? CustomerEnrichedSchema.merge(z.object({@@ -170,7 +148,7 @@ export const GET = withWorkspace(: CustomerSchema;return NextResponse.json(- responseSchema.array().parse(processedCustomers.map(transformCustomer)),+ responseSchema.array().parse(customers.map(transformCustomer)),);},{diff --git a/apps/web/lib/api/customers/determine-customer-discount.ts b/apps/web/lib/api/customers/determine-customer-discount.tsdeleted file mode 100644index 9d53859fa0..0000000000--- a/apps/web/lib/api/customers/determine-customer-discount.ts+++ /dev/null@@ -1,48 +0,0 @@-import { Commission, Discount } from "@prisma/client";-import { differenceInMonths } from "date-fns";--type CustomerWithLink = {- link?: {- programEnrollment?: {- discount?: Discount | null;- program?: {- defaultDiscount?: Discount;- };- } | null;- } | null;-};--export const determineCustomerDiscount = ({- customerLink,- firstPurchase,-}: {- customerLink: CustomerWithLink["link"];- firstPurchase?: Pick<Commission, "createdAt"> | null;-}) => {- // Use the partner discount or fallback to the program discount- const discount =- customerLink?.programEnrollment?.discount ||- customerLink?.programEnrollment?.program?.defaultDiscount ||- null;-- if (!discount) {- return null;- }-- // Lifetime discount- if (discount.maxDuration === null) {- return discount;- }-- // No purchase yet so we can apply the discount- if (!firstPurchase) {- return discount;- }-- const monthsDifference = differenceInMonths(- new Date(),- firstPurchase.createdAt,- );-- return monthsDifference < discount.maxDuration ? discount : null;-};