The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Merge branch 'main' into discounts
Steven Teyd5f10284d471
diff --cc apps/web/app/api/customers/[id]/route.tsindex 28694767f6,f8f169434d..4ceabbb735--- a/apps/web/app/api/customers/[id]/route.ts+++ b/apps/web/app/api/customers/[id]/route.ts@@@ -25,41 -24,14 +26,45 @@@ export const GET = withWorkspaceid,workspaceId: workspace.id,},- { includeExpandedFields },+ {+ includeExpandedFields,+ },);+ 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)));+ return NextResponse.json(- CustomerSchema.parse(++ responseSchema.parse(+ transformCustomer({+ ...customer,+ ...(includeExpandedFields ? { discount } : {}),+ }),+ ),+ );},{requiredPlan: [@@@ -98,44 -65,31 +103,48 @@@ export const PATCH = withWorkspacewhere: {id: customer.id,},- data: { name, email, avatar, externalId },- ...(includeExpandedFields- ? {- include: {- link: {- include: {- programEnrollment: {- include: {- partner: true,- discount: true,- },- },- },- },- },- }- : {}),+ data: {+ name,+ email,+ avatar,+ externalId,+ },});+ 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(- CustomerSchema.parse(- responseSchema.parse(transformCustomer(updatedCustomer)),++ responseSchema.parse(+ transformCustomer({+ ...customer,+ ...updatedCustomer,+ ...(includeExpandedFields ? { discount } : {}),+ }),+ ),);} catch (error) {if (error.code === "P2002") {diff --cc apps/web/app/api/customers/route.tsindex 3e0c74102f,28e46848b0..e22c795385--- a/apps/web/app/api/customers/route.ts+++ b/apps/web/app/api/customers/route.ts@@@ -69,52 -44,14 +70,56 @@@ export const GET = withWorkspace},}: {}),+ })) as CustomerResponse[];++ const discounts: Map<string, Discount | null> = new Map();++ if (includeExpandedFields) {+ const firstPurchases = await prisma.commission.findMany({+ where: {+ customerId: {+ in: customers.map((customer) => customer.id),+ },+ type: "sale",+ },+ select: {+ customerId: true,+ createdAt: true,+ },+ orderBy: {+ createdAt: "asc",+ },+ 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+ : CustomerSchema;+return NextResponse.json(- CustomerSchema.array().parse(processedCustomers.map(transformCustomer)),- responseSchema.array().parse(customers.map(transformCustomer)),++ responseSchema.array().parse(processedCustomers.map(transformCustomer)),);},{