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.

warn early if CUTOFF_PERIOD_MAX_PAYOUTS exceeded

Steven Tey98b5c46c8087
diff --git a/apps/web/app/(ee)/api/cron/payouts/process/process-payouts.ts b/apps/web/app/(ee)/api/cron/payouts/process/process-payouts.ts
index 44adb7a191..afe9a64331 100644
--- a/apps/web/app/(ee)/api/cron/payouts/process/process-payouts.ts
+++ b/apps/web/app/(ee)/api/cron/payouts/process/process-payouts.ts
@@ -9,13 +9,7 @@ import { stripe } from "@/lib/stripe";
import { createFxQuote } from "@/lib/stripe/create-fx-quote";
import { calculatePayoutFeeForMethod } from "@/lib/stripe/payment-methods";
import { prisma } from "@dub/prisma";
-import {
- Partner,
- Payout,
- Program,
- ProgramPayoutMode,
- Project,
-} from "@dub/prisma/client";
+import { Program, ProgramPayoutMode, Project } from "@dub/prisma/client";
import { APP_DOMAIN_WITH_NGROK, currencyFormatter, log } from "@dub/utils";
const paymentMethodToCurrency = {
@@ -49,11 +43,6 @@ interface ProcessPayoutsProps {
excludedPayoutIds?: string[];
}
-interface ExtendedPayout
- extends Pick<Payout, "id" | "amount" | "mode" | "periodStart" | "periodEnd"> {
- partner: Pick<Partner, "email" | "payoutsEnabledAt">;
-}
-
export async function processPayouts({
workspace,
program,
diff --git a/apps/web/lib/actions/partners/confirm-payouts.ts b/apps/web/lib/actions/partners/confirm-payouts.ts
index 35982bd71a..f2783c1a7c 100644
--- a/apps/web/lib/actions/partners/confirm-payouts.ts
+++ b/apps/web/lib/actions/partners/confirm-payouts.ts
@@ -2,9 +2,12 @@
import { createId } from "@/lib/api/create-id";
import { getEligiblePayouts } from "@/lib/api/payouts/get-eligible-payouts";
+import { getPayoutEligibilityFilter } from "@/lib/api/payouts/payout-eligibility-filter";
import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
import { getProgramOrThrow } from "@/lib/api/programs/get-program-or-throw";
import {
+ CUTOFF_PERIOD_MAX_PAYOUTS,
+ INVOICE_MIN_PAYOUT_AMOUNT_CENTS,
PAYMENT_METHOD_TYPES,
STRIPE_PAYMENT_METHOD_NORMALIZATION,
} from "@/lib/constants/payouts";
@@ -74,7 +77,7 @@ export const confirmPayoutsAction = authActionClient
);
}
- if (amount < 1000) {
+ if (amount < INVOICE_MIN_PAYOUT_AMOUNT_CENTS) {
throw new Error(
"Your payout total is less than the minimum invoice amount of $10.",
);
@@ -85,6 +88,25 @@ export const confirmPayoutsAction = authActionClient
programId,
});
+ if (cutoffPeriod) {
+ const totalEligiblePayouts = await prisma.payout.aggregate({
+ where: {
+ ...(selectedPayoutId
+ ? { id: selectedPayoutId }
+ : excludedPayoutIds && excludedPayoutIds.length > 0
+ ? { id: { notIn: excludedPayoutIds } }
+ : {}),
+ ...getPayoutEligibilityFilter(program),
+ },
+ _count: true,
+ });
+ if (totalEligiblePayouts._count > CUTOFF_PERIOD_MAX_PAYOUTS) {
+ throw new Error(
+ `You cannot specify a cutoff period when the number of eligible payouts is greater than ${CUTOFF_PERIOD_MAX_PAYOUTS}.`,
+ );
+ }
+ }
+
if (program.payoutMode !== "internal") {
const [eligiblePayouts, payoutWebhooks] = await Promise.all([
getEligiblePayouts({
diff --git a/apps/web/lib/constants/payouts.ts b/apps/web/lib/constants/payouts.ts
index 4fb5876174..6d39595dbb 100644
--- a/apps/web/lib/constants/payouts.ts
+++ b/apps/web/lib/constants/payouts.ts
@@ -8,10 +8,12 @@ export const FOREX_MARKUP_RATE = 0.005; // 0.5%
export const PAYOUT_HOLDING_PERIOD_DAYS = [0, 7, 14, 30, 60, 90];
export const ALLOWED_MIN_PAYOUT_AMOUNTS = [0, 1000, 2000, 5000, 10000];
+export const INVOICE_MIN_PAYOUT_AMOUNT_CENTS = 1000; // $10
export const MIN_WITHDRAWAL_AMOUNT_CENTS = 1000; // $10
export const BELOW_MIN_WITHDRAWAL_FEE_CENTS = 50; // $0.50
export const ELIGIBLE_PAYOUTS_MAX_PAGE_SIZE = 500;
+export const CUTOFF_PERIOD_MAX_PAYOUTS = 1000;
// Direct debit payment types for Partner payout
export const DIRECT_DEBIT_PAYMENT_TYPES_INFO: {
diff --git a/apps/web/ui/partners/confirm-payouts-sheet.tsx b/apps/web/ui/partners/confirm-payouts-sheet.tsx
index eaf8f7ba6a..9705ef5b93 100644
--- a/apps/web/ui/partners/confirm-payouts-sheet.tsx
+++ b/apps/web/ui/partners/confirm-payouts-sheet.tsx
@@ -1,9 +1,11 @@
import { confirmPayoutsAction } from "@/lib/actions/partners/confirm-payouts";
import { clientAccessCheck } from "@/lib/client-access-check";
import {
+ CUTOFF_PERIOD_MAX_PAYOUTS,
DIRECT_DEBIT_PAYMENT_METHOD_TYPES,
ELIGIBLE_PAYOUTS_MAX_PAGE_SIZE,
FAST_ACH_FEE_CENTS,
+ INVOICE_MIN_PAYOUT_AMOUNT_CENTS,
} from "@/lib/constants/payouts";
import { exceededLimitError } from "@/lib/exceeded-limit-error";
import {
@@ -405,7 +407,8 @@ function ConfirmPayoutsSheetContent() {
),
},
// only show cutoff period if there are less than 1,000 payouts
- ...(eligiblePayoutsCount && eligiblePayoutsCount.count <= 1000
+ ...(eligiblePayoutsCount &&
+ eligiblePayoutsCount.count <= CUTOFF_PERIOD_MAX_PAYOUTS
? [
{
key: "Cutoff Period",
@@ -613,7 +616,7 @@ function ConfirmPayoutsSheetContent() {
),
className: "[&_tr:last-child>td]:border-b-transparent",
scrollWrapperClassName: "min-h-[40px]",
- resourceName: (p) => `eligible payout${p ? "s" : ""}`,
+ resourceName: (p) => `payout${p ? "s" : ""}`,
pagination,
onPaginationChange: setPagination,
rowCount: eligiblePayoutsCount?.count ?? 0,
@@ -738,7 +741,7 @@ function ConfirmPayoutsSheetContent() {
cta="Upgrade"
href={`/${slug}/settings/billing/upgrade`}
/>
- ) : amount && amount < 1000 ? (
+ ) : amount && amount < INVOICE_MIN_PAYOUT_AMOUNT_CENTS ? (
"Your payout total is less than the minimum invoice amount of $10."
) : (
permissionsError || undefined