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.

Check payout limit and empty selection with server payout amount

Kiran K71cc44e31bd4
diff --git a/apps/web/lib/actions/partners/confirm-payouts.ts b/apps/web/lib/actions/partners/confirm-payouts.ts
index 1d5a7f2fa5..c5eea1b3aa 100644
--- a/apps/web/lib/actions/partners/confirm-payouts.ts
+++ b/apps/web/lib/actions/partners/confirm-payouts.ts
@@ -96,19 +96,6 @@ export const confirmPayoutsAction = authActionClient
);
}
- // if workspace's payouts usage + the current invoice amount
- // is greater than the workspace's payouts limit, throw an error
- if (workspace.payoutsUsage + amount > workspace.payoutsLimit) {
- throw new Error(
- exceededLimitError({
- plan: workspace.plan,
- planPeriod: workspace.planPeriod,
- limit: workspace.payoutsLimit,
- type: "payouts",
- }),
- );
- }
-
// TODO: Remove this once we can support cutoff periods for invoices with > 1,000 payouts
if (cutoffPeriod) {
const totalEligiblePayouts = await prisma.payout.aggregate({
@@ -146,6 +133,28 @@ export const confirmPayoutsAction = authActionClient
}),
]);
+ const payoutAmount = eligiblePayouts.reduce(
+ (sum, payout) => sum + payout.amount,
+ 0,
+ );
+
+ if (eligiblePayouts.length === 0 || payoutAmount <= 0) {
+ throw new Error("There are no eligible payouts to confirm.");
+ }
+
+ // if workspace's payouts usage + the current invoice amount
+ // is greater than the workspace's payouts limit, throw an error
+ if (workspace.payoutsUsage + payoutAmount > workspace.payoutsLimit) {
+ throw new Error(
+ exceededLimitError({
+ plan: workspace.plan,
+ planPeriod: workspace.planPeriod,
+ limit: workspace.payoutsLimit,
+ type: "payouts",
+ }),
+ );
+ }
+
if (program.payoutMode !== ProgramPayoutMode.internal) {
// Check if the invoice includes any external payouts
const hasExternalPayouts = eligiblePayouts.find(
@@ -182,11 +191,6 @@ export const confirmPayoutsAction = authActionClient
throw new Error("Fast settlement is only supported for ACH payment.");
}
- const payoutAmount = eligiblePayouts.reduce(
- (sum, payout) => sum + payout.amount,
- 0,
- );
-
const externalPayoutAmount = eligiblePayouts.reduce(
(sum, payout) =>
payout.mode === PayoutMode.external ? sum + payout.amount : sum,