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.

fix /payouts/count

Kiran K200ee65378d7
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 10c6f205fb..d2e6d1611b 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
@@ -43,63 +43,71 @@ export const GET = withWorkspace(
},
}),
...(invoiceId && { invoiceId }),
- ...(excludeCurrentMonth && {
- commissions: {
- some: {
- createdAt: {
- lt: currentMonthStart,
- },
- },
- },
- }),
};
// Get payout count by status
if (groupBy === "status") {
- const payouts = await prisma.payout.groupBy({
- by: ["status"],
- where,
- _count: true,
- _sum: {
- amount: true,
- },
- });
-
- // console.log("payouts", payouts);
+ if (!excludeCurrentMonth) {
+ const payouts = await prisma.payout.groupBy({
+ by: ["status"],
+ where,
+ _count: true,
+ _sum: {
+ amount: true,
+ },
+ });
- // const commissions = await prisma.commission.groupBy({
- // by: ["payoutId"],
- // where: {
- // ...(excludeCurrentMonth && {
- // createdAt: {
- // lt: currentMonthStart,
- // },
- // }),
- // },
- // _sum: {
- // earnings: true,
- // },
- // });
+ const counts = payouts.map((p) => ({
+ status: p.status,
+ count: p._count,
+ amount: p._sum.amount,
+ }));
- // console.log("commissions", commissions);
+ Object.values(PayoutStatus).forEach((status) => {
+ if (!counts.find((p) => p.status === status)) {
+ counts.push({
+ status,
+ count: 0,
+ amount: 0,
+ });
+ }
+ });
- const counts = payouts.map((p) => ({
- status: p.status,
- count: p._count,
- amount: p._sum.amount,
- }));
+ return NextResponse.json(counts);
+ }
- Object.values(PayoutStatus).forEach((status) => {
- if (!counts.find((p) => p.status === status)) {
- counts.push({
- status,
- count: 0,
- amount: 0,
- });
- }
+ // Special case for excludeCurrentMonth on the payout invoice sheet
+ const commissions = await prisma.commission.groupBy({
+ by: ["payoutId"],
+ where: {
+ programId,
+ payout: {
+ amount: {
+ gte: minPayoutAmount,
+ },
+ },
+ partner: {
+ payoutsEnabledAt: {
+ not: null,
+ },
+ },
+ status: "processed",
+ createdAt: {
+ lt: currentMonthStart,
+ },
+ },
+ _sum: {
+ earnings: true,
+ },
});
- return NextResponse.json(counts);
+ const counts = {
+ status: "pending",
+ count: commissions.length,
+ amount: commissions.reduce((acc, c) => acc + (c._sum.earnings ?? 0), 0),
+ };
+
+ return NextResponse.json([counts]);
}
const count = await prisma.payout.count({
diff --git a/apps/web/lib/zod/schemas/payouts.ts b/apps/web/lib/zod/schemas/payouts.ts
index 7c9b76f26b..c87252af5a 100644
--- a/apps/web/lib/zod/schemas/payouts.ts
+++ b/apps/web/lib/zod/schemas/payouts.ts
@@ -27,7 +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"),
+ excludeCurrentMonth: booleanQuerySchema.optional(),
sortBy: z
.enum(["createdAt", "periodStart", "amount", "paidAt"])
.default("amount"),
diff --git a/apps/web/ui/partners/payout-invoice-sheet.tsx b/apps/web/ui/partners/payout-invoice-sheet.tsx
index 4bec2dfd65..050c130fbb 100644
--- a/apps/web/ui/partners/payout-invoice-sheet.tsx
+++ b/apps/web/ui/partners/payout-invoice-sheet.tsx
@@ -91,7 +91,9 @@ function PayoutInvoiceSheetContent({ setIsOpen }: PayoutInvoiceSheetProps) {
} = usePayoutsCount<PayoutsCount[]>({
groupBy: "status",
eligibility: "eligible",
- excludeCurrentMonth: excludeCurrentMonth ? "true" : "false",
+ ...(excludeCurrentMonth && {
+ excludeCurrentMonth: "true",
+ }),
});
const {
@@ -103,10 +105,14 @@ function PayoutInvoiceSheetContent({ setIsOpen }: PayoutInvoiceSheetProps) {
status: "pending",
eligibility: "eligible",
sortBy: "amount",
- excludeCurrentMonth: excludeCurrentMonth ? "true" : "false",
+ ...(excludeCurrentMonth && {
+ excludeCurrentMonth: "true",
+ }),
},
});
+ console.log("excludeCurrentMonth", excludeCurrentMonth);
+
const { executeAsync, isPending } = useAction(confirmPayoutsAction, {
onSuccess: async () => {
await mutatePrefix(`/api/programs/${programId}/payouts`);
@@ -377,6 +383,7 @@ function PayoutInvoiceSheetContent({ setIsOpen }: PayoutInvoiceSheetProps) {
type="button"
variant="primary"
loading={isPending}
+ disabled={eligiblePayoutsCountLoading || eligiblePayoutsLoading}
onClick={async () => {
if (!workspaceId || !selectedPaymentMethod) {
return;
@@ -389,7 +396,11 @@ function PayoutInvoiceSheetContent({ setIsOpen }: PayoutInvoiceSheetProps) {
excludeCurrentMonth,
});
}}
- text={`Confirm ${invoiceData.Amount} payout`}
+ text={
+ typeof invoiceData.Amount === "string"
+ ? `Confirm ${invoiceData.Amount} payout`
+ : "Confirm payout"
+ }
/>
</div>
</div>