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.

Update confirm-payouts.ts

Steven Teyb65993cf9abe
diff --git a/apps/web/lib/actions/partners/confirm-payouts.ts b/apps/web/lib/actions/partners/confirm-payouts.ts
index c42034d0dd..be767ddbb3 100644
--- a/apps/web/lib/actions/partners/confirm-payouts.ts
+++ b/apps/web/lib/actions/partners/confirm-payouts.ts
@@ -4,7 +4,8 @@ import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-progr
import { qstash } from "@/lib/cron";
import { CUTOFF_PERIOD_ENUM } from "@/lib/partners/cutoff-period";
import { stripe } from "@/lib/stripe";
-import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
+import { prisma } from "@dub/prisma";
+import { APP_DOMAIN_WITH_NGROK, getFirstAndLastDay } from "@dub/utils";
import z from "zod";
import { authActionClient } from "../safe-action";
@@ -23,12 +24,32 @@ export const confirmPayoutsAction = authActionClient
const { workspace, user } = ctx;
const { paymentMethodId, cutoffPeriod } = parsedInput;
- getDefaultProgramIdOrThrow(workspace);
+ const programId = getDefaultProgramIdOrThrow(workspace);
if (!workspace.stripeId) {
throw new Error("Workspace does not have a valid Stripe ID.");
}
+ const { firstDay } = getFirstAndLastDay(workspace.billingCycleStart);
+ const totalPaidPayouts = await prisma.invoice.aggregate({
+ where: {
+ programId,
+ createdAt: {
+ gte: new Date(firstDay),
+ },
+ },
+ _sum: {
+ amount: true,
+ },
+ });
+
+ if (
+ totalPaidPayouts._sum?.amount &&
+ totalPaidPayouts._sum.amount > workspace.payoutsLimit
+ ) {
+ throw new Error("Payouts limit exceeded.");
+ }
+
const paymentMethod = await stripe.paymentMethods.retrieve(paymentMethodId);
if (paymentMethod.customer !== workspace.stripeId) {