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.

api changes

Kiran Ka583dd723f12
diff --git a/apps/web/app/(ee)/api/programs/[programId]/payouts/count/route.ts b/apps/web/app/(ee)/api/programs/[programId]/payouts/count/route.ts
index 64f0fbe0b4..24047e988b 100644
--- a/apps/web/app/(ee)/api/programs/[programId]/payouts/count/route.ts
+++ b/apps/web/app/(ee)/api/programs/[programId]/payouts/count/route.ts
@@ -9,14 +9,27 @@ import { NextResponse } from "next/server";
export const GET = withWorkspace(
async ({ workspace, params, searchParams }) => {
const { programId } = params;
- const parsed = payoutsCountQuerySchema.parse(searchParams);
+
+ const {
+ partnerId,
+ groupBy,
+ eligibility,
+ status,
+ invoiceId,
+ excludeCurrentMonth,
+ } = payoutsCountQuerySchema.parse(searchParams);
const { minPayoutAmount } = await getProgramOrThrow({
workspaceId: workspace.id,
programId,
});
- const { partnerId, groupBy, eligibility, status, invoiceId } = parsed;
+ const now = new Date();
+ const currentMonthStart = new Date(
+ Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), 1),
+ );
+
+ console.log("currentMonthStart", currentMonthStart);
const where: Prisma.PayoutWhereInput = {
programId,
@@ -32,6 +45,15 @@ export const GET = withWorkspace(
},
}),
...(invoiceId && { invoiceId }),
+ ...(excludeCurrentMonth && {
+ commissions: {
+ some: {
+ createdAt: {
+ lt: currentMonthStart,
+ },
+ },
+ },
+ }),
};
// Get payout count by status
@@ -61,6 +83,8 @@ export const GET = withWorkspace(
}
});
+ console.log(counts)
+
return NextResponse.json(counts);
}
diff --git a/apps/web/app/(ee)/api/programs/[programId]/payouts/route.ts b/apps/web/app/(ee)/api/programs/[programId]/payouts/route.ts
index 073e47a1a1..f2b82662f8 100644
--- a/apps/web/app/(ee)/api/programs/[programId]/payouts/route.ts
+++ b/apps/web/app/(ee)/api/programs/[programId]/payouts/route.ts
@@ -5,6 +5,7 @@ import {
payoutsQuerySchema,
} from "@/lib/zod/schemas/payouts";
import { prisma } from "@dub/prisma";
+import { Commission } from "@prisma/client";
import { NextResponse } from "next/server";
import z from "zod";
@@ -28,6 +29,7 @@ export const GET = withWorkspace(
sortOrder,
page,
pageSize,
+ excludeCurrentMonth,
} = parsed;
const payouts = await prisma.payout.findMany({
@@ -50,6 +52,9 @@ export const GET = withWorkspace(
include: {
partner: true,
user: true,
+ ...(excludeCurrentMonth && {
+ commissions: true,
+ }),
},
skip: (page - 1) * pageSize,
take: pageSize,
@@ -58,6 +63,46 @@ export const GET = withWorkspace(
},
});
- return NextResponse.json(z.array(PayoutResponseSchema).parse(payouts));
+ const result = excludeCurrentMonth
+ ? await excludeCurrentMonthCommissions(payouts)
+ : payouts;
+
+ return NextResponse.json(z.array(PayoutResponseSchema).parse(result));
},
);
+
+// Exclude the current month's commissions from the payouts
+const excludeCurrentMonthCommissions = async (
+ payouts: (z.infer<typeof PayoutResponseSchema> & {
+ commissions: Commission[];
+ })[],
+) => {
+ const now = new Date();
+ const currentMonthStart = new Date(
+ Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), 1),
+ );
+
+ return payouts.map((payout) => {
+ const newPayoutAmount = payout.commissions.reduce((acc, commission) => {
+ const commissionDate = new Date(commission.createdAt);
+
+ if (commissionDate < currentMonthStart) {
+ console.log({
+ payoutId: payout.id,
+ commissionId: commission.id,
+ commissionAmount: commission.amount,
+ });
+
+ return acc + commission.amount;
+ }
+
+ return acc;
+ }, 0);
+
+ return {
+ ...payout,
+ amount: newPayoutAmount,
+ periodEnd: currentMonthStart,
+ };
+ });
+};
diff --git a/apps/web/lib/zod/schemas/payouts.ts b/apps/web/lib/zod/schemas/payouts.ts
index 9a54f5374c..7c9b76f26b 100644
--- a/apps/web/lib/zod/schemas/payouts.ts
+++ b/apps/web/lib/zod/schemas/payouts.ts
@@ -1,6 +1,6 @@
import { PayoutStatus } from "@dub/prisma/client";
import { z } from "zod";
-import { getPaginationQuerySchema } from "./misc";
+import { booleanQuerySchema, getPaginationQuerySchema } from "./misc";
import { PartnerSchema, PAYOUTS_MAX_PAGE_SIZE } from "./partners";
import { ProgramSchema } from "./programs";
@@ -27,6 +27,7 @@ export const payoutsQuerySchema = z
programId: z.string().optional(),
invoiceId: z.string().optional(),
eligibility: z.enum(["eligible", "ineligible"]).optional(),
+ excludeCurrentMonth: booleanQuerySchema.optional().default("false"),
sortBy: z
.enum(["createdAt", "periodStart", "amount", "paidAt"])
.default("amount"),
@@ -41,6 +42,7 @@ export const payoutsCountQuerySchema = payoutsQuerySchema
partnerId: true,
eligibility: true,
invoiceId: true,
+ excludeCurrentMonth: true,
})
.merge(
z.object({
diff --git a/apps/web/ui/partners/payout-invoice-sheet.tsx b/apps/web/ui/partners/payout-invoice-sheet.tsx
index 03c2af44a4..f60fe5b304 100644
--- a/apps/web/ui/partners/payout-invoice-sheet.tsx
+++ b/apps/web/ui/partners/payout-invoice-sheet.tsx
@@ -91,6 +91,7 @@ function PayoutInvoiceSheetContent({ setIsOpen }: PayoutInvoiceSheetProps) {
} = usePayoutsCount<PayoutsCount[]>({
groupBy: "status",
eligibility: "eligible",
+ excludeCurrentMonth: excludeCurrentMonth ? "true" : "false",
});
const {
@@ -102,9 +103,14 @@ function PayoutInvoiceSheetContent({ setIsOpen }: PayoutInvoiceSheetProps) {
status: "pending",
eligibility: "eligible",
sortBy: "amount",
+ excludeCurrentMonth: excludeCurrentMonth ? "true" : "false",
},
});
+ console.log({
+ eligiblePayouts,
+ });
+
const { executeAsync, isPending } = useAction(confirmPayoutsAction, {
onSuccess: async () => {
await mutatePrefix(`/api/programs/${programId}/payouts`);
@@ -172,6 +178,10 @@ function PayoutInvoiceSheetContent({ setIsOpen }: PayoutInvoiceSheetProps) {
};
}
+ const amount =
+ eligiblePayoutsCount?.find((p) => p.status === PayoutStatus.pending)
+ ?.amount ?? 0;
+
const fee = amount * (selectedPaymentMethod?.fee ?? 0);
const total = amount + fee;
@@ -258,6 +268,7 @@ function PayoutInvoiceSheetContent({ setIsOpen }: PayoutInvoiceSheetProps) {
eligiblePayoutsCountLoading,
paymentMethods,
selectedPaymentMethod,
+ excludeCurrentMonth,
]);
const table = useTable({