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.

optimize the query

Kiran Kb3771e24f61f
diff --git a/apps/web/app/api/customers/route.ts b/apps/web/app/api/customers/route.ts
index aba28f95cd..4ad784f899 100644
--- a/apps/web/app/api/customers/route.ts
+++ b/apps/web/app/api/customers/route.ts
@@ -1,3 +1,4 @@
+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";
@@ -17,7 +18,6 @@ import {
Program,
ProgramEnrollment,
} from "@prisma/client";
-import { differenceInMonths } from "date-fns";
import { NextResponse } from "next/server";
interface CustomerResponse extends Customer {
@@ -71,59 +71,43 @@ export const GET = withWorkspace(
: {}),
})) as CustomerResponse[];
- // TODO
- // Move this to another methods
- const determineCustomerDiscount = async (customer: CustomerResponse) => {
- const discount =
- customer.link?.programEnrollment?.discount ||
- customer.link?.programEnrollment?.program.defaultDiscount ||
- null;
+ const firstPurchaseMap = new Map();
- if (!discount) {
- return null;
- }
-
- // Lifetime discount
- if (discount.maxDuration === null) {
- return discount;
- }
-
- const firstPurchase = await prisma.commission.findFirst({
- where: {
- customerId: customer.id,
- type: "sale",
- },
- orderBy: {
- createdAt: "asc",
+ const firstPurchases = await prisma.commission.findMany({
+ where: {
+ customerId: {
+ in: customers.map((customer) => customer.id),
},
- });
+ type: "sale",
+ },
+ orderBy: {
+ createdAt: "asc",
+ },
+ distinct: ["customerId"],
+ });
- // No purchase yet so we can apply the discount
- if (!firstPurchase) {
- return discount;
+ firstPurchases.forEach((purchase) => {
+ if (!firstPurchaseMap.has(purchase.customerId)) {
+ firstPurchaseMap.set(purchase.customerId, purchase);
}
-
- const monthsDifference = differenceInMonths(
- new Date(),
- firstPurchase.createdAt,
- );
-
- return monthsDifference < discount.maxDuration ? discount : null;
- };
-
- const processedCustomers = await Promise.all(
- customers.map(async (customer) => {
- return {
- ...customer,
- discount: await determineCustomerDiscount(customer),
- };
- }),
- );
+ });
+
+ // TODO:
+ // Test customers with no first purchase
+
+ const processedCustomers = customers.map((customer) => {
+ return {
+ ...customer,
+ discount: determineCustomerDiscount(
+ customer,
+ firstPurchaseMap.get(customer.id),
+ ),
+ };
+ });
return NextResponse.json(
CustomerSchema.array().parse(processedCustomers.map(transformCustomer)),
);
- P;
},
{
requiredPlan: [
diff --git a/apps/web/lib/api/customers/determine-customer-discount.ts b/apps/web/lib/api/customers/determine-customer-discount.ts
new file mode 100644
index 0000000000..4dcfc26930
--- /dev/null
+++ b/apps/web/lib/api/customers/determine-customer-discount.ts
@@ -0,0 +1,45 @@
+import { Commission, Discount } from "@prisma/client";
+import { differenceInMonths } from "date-fns";
+
+type CustomerWithLink = {
+ link: {
+ programEnrollment: {
+ program: {
+ defaultDiscount: Discount;
+ };
+ discount: Discount | null;
+ };
+ };
+};
+
+export const determineCustomerDiscount = (
+ customer: CustomerWithLink,
+ firstPurchase: Pick<Commission, "createdAt"> | null,
+) => {
+ // Use the partner discount or fallback to the program discount
+ const discount =
+ customer.link?.programEnrollment?.discount ||
+ customer.link?.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;
+};