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.

Allow unconnected partners to receive simulated payouts in staging and sandbox (#4664)

Kiran Kd8b994e99e70
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 1840c88c5f..c70fbd9576 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
@@ -47,7 +47,13 @@ interface ProcessPayoutsProps {
>;
program: Pick<
Program,
- "id" | "name" | "logo" | "url" | "minPayoutAmount" | "supportEmail"
+ | "id"
+ | "name"
+ | "logo"
+ | "url"
+ | "minPayoutAmount"
+ | "supportEmail"
+ | "environment"
> & {
payoutMode: ProgramPayoutMode;
};
diff --git a/apps/web/app/(ee)/api/cron/payouts/process/split-payouts.ts b/apps/web/app/(ee)/api/cron/payouts/process/split-payouts.ts
index 55f741848b..4d61191980 100644
--- a/apps/web/app/(ee)/api/cron/payouts/process/split-payouts.ts
+++ b/apps/web/app/(ee)/api/cron/payouts/process/split-payouts.ts
@@ -15,7 +15,10 @@ export async function splitPayouts({
selectedPayoutIds,
excludedPayoutIds,
}: {
- program: Pick<Program, "id" | "name" | "minPayoutAmount" | "payoutMode">;
+ program: Pick<
+ Program,
+ "id" | "name" | "minPayoutAmount" | "payoutMode" | "environment"
+ >;
cutoffPeriod: CUTOFF_PERIOD_TYPES;
selectedPayoutIds?: string[];
excludedPayoutIds?: string[];
diff --git a/apps/web/app/(ee)/api/partner-profile/programs/route.ts b/apps/web/app/(ee)/api/partner-profile/programs/route.ts
index c364927688..b4ccf97e9a 100644
--- a/apps/web/app/(ee)/api/partner-profile/programs/route.ts
+++ b/apps/web/app/(ee)/api/partner-profile/programs/route.ts
@@ -25,6 +25,7 @@ const PROGRAM_SELECT = {
messagingEnabledAt: true,
partnerNetworkEnabledAt: true,
payoutMode: true,
+ environment: true,
defaultFolderId: true,
defaultGroupId: true,
supportEmail: true,
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/payouts/[payoutId]/page.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/payouts/[payoutId]/page.tsx
index 0f4f10f38a..08fd826757 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/payouts/[payoutId]/page.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/payouts/[payoutId]/page.tsx
@@ -2,7 +2,9 @@
import { clientAccessCheck } from "@/lib/client-access-check";
import { formatCommissionDescriptionTooltip } from "@/lib/commissions/format-commission-description-tooltip";
+import { isProductionEnvironment } from "@/lib/sandbox/environment";
import { usePayout } from "@/lib/swr/use-payout";
+import useProgram from "@/lib/swr/use-program";
import useWorkspace from "@/lib/swr/use-workspace";
import { CommissionResponse, PayoutResponse } from "@/lib/types";
import { CustomerAvatar } from "@/ui/customers/customer-avatar";
@@ -40,7 +42,7 @@ import {
formatDateTimeSmart,
} from "@dub/utils";
import { formatPeriod } from "@dub/utils/src/functions/datetime";
-import { PayoutStatus } from "@prisma/client";
+import { PayoutStatus, ProgramPayoutMode } from "@prisma/client";
import Link from "next/link";
import { redirect, useRouter } from "next/navigation";
import { useMemo } from "react";
@@ -507,6 +509,7 @@ function PayoutDetailsskeleton() {
function PayoutConfirmButton() {
const { slug, role } = useWorkspace();
+ const { program } = useProgram();
const { payout } = usePayout();
const router = useRouter();
@@ -518,31 +521,47 @@ function PayoutConfirmButton() {
const permissionsError =
typeof _permissionsError === "string" ? _permissionsError : null;
+ const programLoaded = !!program;
+ const isProductionProgram = isProductionEnvironment(program?.environment);
+ const requiresTenantId =
+ programLoaded &&
+ !isProductionProgram &&
+ program?.payoutMode === ProgramPayoutMode.external;
+ const missingExternalTenant = !payout?.partner.tenantId && requiresTenantId;
+ const partnerNotConnected =
+ !payout?.partner.payoutsEnabledAt && isProductionProgram;
+ const disabledTooltip = !programLoaded
+ ? undefined
+ : missingExternalTenant
+ ? "This partner does not have a tenant ID configured, which is required to process external payouts."
+ : partnerNotConnected
+ ? "This partner has not [connected a bank account](https://dub.co/help/article/receiving-payouts) to receive payouts yet, which means they won't be able to receive payouts from your program."
+ : permissionsError || undefined;
+ const confirmBlocked = !programLoaded || !!disabledTooltip;
+
const url = `/${slug}/program/payouts?status=pending&confirmPayouts=true&selectedPayoutIds=${payout?.id}`;
useKeyboardShortcut("c", () => router.push(url), {
- enabled:
- !!payout?.id && !!payout.partner.payoutsEnabledAt && !permissionsError,
+ enabled: !!payout?.id && !confirmBlocked,
});
if (payout?.status !== "pending") {
return null;
}
+ const button = (
+ <Button
+ text="Confirm payout"
+ className="h-8 px-3 sm:h-9"
+ shortcut="C"
+ disabled={!programLoaded}
+ disabledTooltip={disabledTooltip}
+ />
+ );
+
return (
<div className="flex items-center gap-2">
- <Link href={url}>
- <Button
- text="Confirm payout"
- className="h-8 px-3 sm:h-9"
- shortcut="C"
- disabledTooltip={
- !payout.partner.payoutsEnabledAt
- ? "This partner has not [connected a bank account](https://dub.co/help/article/receiving-payouts) to receive payouts yet, which means they won't be able to receive payouts from your program."
- : permissionsError || undefined
- }
- />
- </Link>
+ {confirmBlocked ? button : <Link href={url}>{button}</Link>}
</div>
);
}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/payouts/payout-table.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/payouts/payout-table.tsx
index d1a7f0800e..ce4db7b8a6 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/payouts/payout-table.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/payouts/payout-table.tsx
@@ -1,5 +1,6 @@
"use client";
+import { isProductionEnvironment } from "@/lib/sandbox/environment";
import useGroups from "@/lib/swr/use-groups";
import { usePayoutsCount } from "@/lib/swr/use-payouts-count";
import useProgram from "@/lib/swr/use-program";
@@ -35,6 +36,7 @@ import { formatPeriod } from "@dub/utils/src/functions/datetime";
import { fetcher } from "@dub/utils/src/functions/fetcher";
import {
PartnerPayoutMethod,
+ PayoutMode,
PayoutStatus,
ProgramPayoutMode,
} from "@prisma/client";
@@ -44,52 +46,9 @@ import { useMemo } from "react";
import { toast } from "sonner";
import useSWR from "swr";
import { PayoutPaidCell } from "./payout-paid-cell";
+import { usePayoutBatchConfirm } from "./use-payout-batch-confirm";
import { usePayoutFilters } from "./use-payout-filters";
-/** Matches server `getPayoutEligibilityFilter` and in-table warnings in `AmountRowItem`. */
-function isPayoutEligibleForBatchConfirm(
- payout: PayoutResponse,
- {
- minPayoutAmount,
- programPayoutMode,
- }: {
- minPayoutAmount: number;
- programPayoutMode: ProgramPayoutMode;
- },
-) {
- if (payout.status !== PayoutStatus.pending) {
- return false;
- }
-
- if (payout.amount < minPayoutAmount) {
- return false;
- }
-
- // Derive effective mode the same way server does
- const effectiveMode = payout.partner.payoutsEnabledAt
- ? "internal"
- : programPayoutMode === "hybrid" || programPayoutMode === "external"
- ? "external"
- : programPayoutMode;
-
- if (effectiveMode === "external" && !payout.partner?.tenantId) {
- return false;
- }
- if (effectiveMode === "internal" && !payout.partner?.payoutsEnabledAt) {
- return false;
- }
-
- if (
- payout.partner.defaultPayoutMethod === PartnerPayoutMethod.tremendous &&
- (payout.amount < TREMENDOUS_MIN_PAYOUT_AMOUNT_CENTS ||
- payout.amount > TREMENDOUS_MAX_PAYOUT_AMOUNT_CENTS)
- ) {
- return false;
- }
-
- return true;
-}
-
const PAYOUTS_MAX_PAGE_SIZE = 50;
const payoutsColumns = {
@@ -121,9 +80,8 @@ export function PayoutTable() {
defaultProgramId,
} = useWorkspace();
- const { program } = useProgram();
const { groups } = useGroups();
- const minPayoutAmount = program?.minPayoutAmount ?? 0;
+ const { getBatchConfirm } = usePayoutBatchConfirm();
const { columnVisibility, setColumnVisibility } = useColumnVisibility(
"payouts-table-columns",
@@ -303,61 +261,18 @@ export function PayoutTable() {
const selectedPayouts = table
.getSelectedRowModel()
.rows.map((r) => r.original);
-
- const eligibilityCtx = {
- minPayoutAmount,
- programPayoutMode: program?.payoutMode ?? "internal",
- };
-
- const hasIneligibleAmongSelection =
- selectedPayouts.length > 0 &&
- selectedPayouts.some(
- (payout) => !isPayoutEligibleForBatchConfirm(payout, eligibilityCtx),
- );
+ const { disabled, disabledTooltip, eligiblePayouts } =
+ getBatchConfirm(selectedPayouts);
return (
<Button
variant="primary"
text="Confirm selected"
className="h-7 w-fit rounded-lg px-2.5"
- disabled={hasIneligibleAmongSelection}
- disabledTooltip={
- hasIneligibleAmongSelection ? (
- <div className="max-w-xs space-y-2 px-4 py-3 text-left text-sm">
- <p>
- Your selection includes payouts that are ineligible for
- payment:
- </p>
- <ul className="list-disc space-y-1 pl-4 marker:text-neutral-500">
- <li>
- Below the{" "}
- <a
- href="https://dub.co/help/article/partner-payouts#minimum-payout-amount"
- target="_blank"
- className="cursor-help underline decoration-dotted underline-offset-2"
- >
- minimum payout amount
- </a>
- </li>
- <li>Partner has not connected payouts</li>
- <li>
- Not within the gift card payout amount range of{" "}
- {MIN_GIFT_CARD_PAYOUT_AMOUNT} –{" "}
- {MAX_GIFT_CARD_PAYOUT_AMOUNT}
- </li>
- </ul>
- </div>
- ) : undefined
- }
+ disabled={disabled}
+ disabledTooltip={disabledTooltip}
onClick={() => {
- const current = table
- .getSelectedRowModel()
- .rows.map((r) => r.original);
- const pendingEligible = current.filter((p) =>
- isPayoutEligibleForBatchConfirm(p, eligibilityCtx),
- );
-
- if (pendingEligible.length === 0) {
+ if (eligiblePayouts.length === 0) {
toast.error("Select at least one eligible pending payout.");
return;
}
@@ -365,7 +280,7 @@ export function PayoutTable() {
queryParams({
set: {
confirmPayouts: "true",
- selectedPayoutIds: pendingEligible.map((p) => p.id).join(","),
+ selectedPayoutIds: eligiblePayouts.map((p) => p.id).join(","),
},
del: ["selectedPayoutId", "excludedPayoutIds"],
});
@@ -456,6 +371,8 @@ function AmountRowItem({
const minPayoutAmount = program?.minPayoutAmount || 0;
const display = currencyFormatter(payout.amount);
+ const programLoaded = !!program;
+ const isProductionProgram = isProductionEnvironment(program?.environment);
if (payout.status === PayoutStatus.pending) {
if (payout.amount < minPayoutAmount) {
@@ -479,27 +396,34 @@ function AmountRowItem({
);
}
- if (payout.mode === "external") {
+ if (payout.mode === PayoutMode.external) {
+ const missingTenantId = !payout.partner?.tenantId;
+ const requiresTenantId =
+ !programLoaded ||
+ isProductionProgram ||
+ program?.payoutMode === ProgramPayoutMode.external;
+ const blocksExternalPayout = missingTenantId && requiresTenantId;
+
return (
<div className="flex items-center gap-1.5">
<DynamicTooltipWrapper
tooltipProps={{
- content: payout.partner?.tenantId ? undefined : (
+ content: blocksExternalPayout ? (
<TooltipContent
title="This partner does not have a tenant ID configured, which is required to process external payouts."
cta="Learn more"
href="http://dub.co/docs/partners/external-payouts"
target="_blank"
/>
- ),
+ ) : undefined,
}}
>
<span
className={cn(
"truncate",
- payout.partner?.tenantId
- ? "text-neutral-700"
- : "text-neutral-400 underline decoration-dotted underline-offset-2",
+ blocksExternalPayout
+ ? "text-neutral-400 underline decoration-dotted underline-offset-2"
+ : "text-neutral-700",
)}
>
{display}
@@ -510,7 +434,11 @@ function AmountRowItem({
);
}
- if (payout.mode === "internal" && !payout.partner?.payoutsEnabledAt) {
+ if (
+ payout.mode === PayoutMode.internal &&
+ !payout.partner?.payoutsEnabledAt &&
+ (!programLoaded || isProductionProgram)
+ ) {
return (
<Tooltip content="This partner has not [connected a bank account](https://dub.co/help/article/receiving-payouts) to receive payouts yet, which means they won't be able to receive payouts from your program.">
<span className="cursor-help truncate text-neutral-400 underline decoration-dotted underline-offset-2">
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/payouts/use-payout-batch-confirm.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/payouts/use-payout-batch-confirm.tsx
new file mode 100644
index 0000000000..dcc41f7451
--- /dev/null
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/payouts/use-payout-batch-confirm.tsx
@@ -0,0 +1,211 @@
+import { getEffectivePayoutMode } from "@/lib/api/payouts/get-effective-payout-mode";
+import { isProductionEnvironment } from "@/lib/sandbox/environment";
+import useProgram from "@/lib/swr/use-program";
+import {
+ TREMENDOUS_MAX_PAYOUT_AMOUNT_CENTS,
+ TREMENDOUS_MIN_PAYOUT_AMOUNT_CENTS,
+} from "@/lib/tremendous/constants";
+import { PayoutResponse, ProgramProps } from "@/lib/types";
+import { currencyFormatter } from "@dub/utils";
+import {
+ PartnerPayoutMethod,
+ PayoutMode,
+ PayoutStatus,
+ ProgramPayoutMode,
+} from "@prisma/client";
+import { ReactNode, useMemo } from "react";
+
+type BatchConfirmBlocker =
+ | "belowMinimum"
+ | "notConnected"
+ | "giftCardRange"
+ | "missingTenantId";
+
+type BatchConfirmEligibility = {
+ minPayoutAmount: number;
+ programPayoutMode: ProgramPayoutMode;
+ environment: ProgramProps["environment"] | undefined;
+};
+
+const MIN_GIFT_CARD_PAYOUT_AMOUNT = currencyFormatter(
+ TREMENDOUS_MIN_PAYOUT_AMOUNT_CENTS,
+ {
+ trailingZeroDisplay: "stripIfInteger",
+ },
+);
+
+const MAX_GIFT_CARD_PAYOUT_AMOUNT = currencyFormatter(
+ TREMENDOUS_MAX_PAYOUT_AMOUNT_CENTS,
+ {
+ trailingZeroDisplay: "stripIfInteger",
+ },
+);
+
+// Matches server `getPayoutEligibilityFilter` and in-table warnings in `AmountRowItem`.
+function getBatchConfirmBlockers(
+ payout: PayoutResponse,
+ { minPayoutAmount, programPayoutMode, environment }: BatchConfirmEligibility,
+): BatchConfirmBlocker[] {
+ const blockers: BatchConfirmBlocker[] = [];
+
+ if (payout.amount < minPayoutAmount) {
+ blockers.push("belowMinimum");
+ }
+
+ if (
+ payout.partner.defaultPayoutMethod === PartnerPayoutMethod.tremendous &&
+ (payout.amount < TREMENDOUS_MIN_PAYOUT_AMOUNT_CENTS ||
+ payout.amount > TREMENDOUS_MAX_PAYOUT_AMOUNT_CENTS)
+ ) {
+ blockers.push("giftCardRange");
+ }
+
+ // Non-production internal and hybrid payouts are simulated, so a missing
+ // payout connection should not block them. External mode still requires a tenant ID.
+ if (
+ environment != null &&
+ !isProductionEnvironment(environment) &&
+ programPayoutMode !== ProgramPayoutMode.external
+ ) {
+ return blockers;
+ }
+
+ // Derive effective mode the same way server does
+ const effectiveMode = getEffectivePayoutMode({
+ payoutMode: programPayoutMode,
+ payoutsEnabledAt: payout.partner.payoutsEnabledAt ?? null,
+ });
+
+ if (effectiveMode === PayoutMode.external && !payout.partner?.tenantId) {
+ blockers.push("missingTenantId");
+ }
+
+ if (
+ effectiveMode === PayoutMode.internal &&
+ !payout.partner?.payoutsEnabledAt
+ ) {
+ blockers.push("notConnected");
+ }
+
+ return blockers;
+}
+
+function isPayoutEligibleForBatchConfirm(
+ payout: PayoutResponse,
+ eligibility: BatchConfirmEligibility,
+) {
+ if (payout.status !== PayoutStatus.pending) {
+ return false;
+ }
+
+ return getBatchConfirmBlockers(payout, eligibility).length === 0;
+}
+
+const BATCH_CONFIRM_REASONS: {
+ id: BatchConfirmBlocker;
+ label: ReactNode;
+}[] = [
+ {
+ id: "belowMinimum",
+ label: (
+ <>
+ Below the{" "}
+ <a
+ href="https://dub.co/help/article/partner-payouts#minimum-payout-amount"
+ target="_blank"
+ className="cursor-help underline decoration-dotted underline-offset-2"
+ >
+ minimum payout amount
+ </a>
+ </>
+ ),
+ },
+ {
+ id: "notConnected",
+ label: "Partner has not connected payouts",
+ },
+ {
+ id: "missingTenantId",
+ label:
+ "Partner does not have a tenant ID, which is required for external payouts.",
+ },
+ {
+ id: "giftCardRange",
+ label: `Not within the gift card payout amount range of ${MIN_GIFT_CARD_PAYOUT_AMOUNT} – ${MAX_GIFT_CARD_PAYOUT_AMOUNT}`,
+ },
+];
+
+function BatchConfirmTooltip({
+ blockers,
+}: {
+ blockers: Set<BatchConfirmBlocker>;
+}) {
+ const reasons = BATCH_CONFIRM_REASONS.filter((reason) =>
+ blockers.has(reason.id),
+ );
+
+ const items =
+ reasons.length > 0
+ ? reasons
+ : [{ id: "notPending", label: "Payout is not pending" }];
+
+ return (
+ <div className="max-w-xs space-y-2 px-4 py-3 text-left text-sm">
+ <p>Your selection includes payouts that are ineligible for payment:</p>
+ <ul className="list-disc space-y-1 pl-4 marker:text-neutral-500">
+ {items.map((item) => (
+ <li key={item.id}>{item.label}</li>
+ ))}
+ </ul>
+ </div>
+ );
+}
+
+function getBatchConfirm(
+ payouts: PayoutResponse[],
+ eligibility: BatchConfirmEligibility,
+) {
+ const eligiblePayouts = payouts.filter((payout) =>
+ isPayoutEligibleForBatchConfirm(payout, eligibility),
+ );
+
+ const disabled =
+ payouts.length > 0 && eligiblePayouts.length !== payouts.length;
+
+ const blockers = new Set(
+ payouts.flatMap((payout) =>
+ payout.status === PayoutStatus.pending
+ ? getBatchConfirmBlockers(payout, eligibility)
+ : [],
+ ),
+ );
+
+ return {
+ disabled,
+ disabledTooltip: disabled ? (
+ <BatchConfirmTooltip blockers={blockers} />
+ ) : undefined,
+ eligiblePayouts,
+ };
+}
+
+export function usePayoutBatchConfirm() {
+ const { program } = useProgram();
+
+ const eligibility = useMemo<BatchConfirmEligibility>(
+ () => ({
+ minPayoutAmount: program?.minPayoutAmount ?? 0,
+ programPayoutMode: program?.payoutMode ?? ProgramPayoutMode.internal,
+ environment: program?.environment,
+ }),
+ [program?.minPayoutAmount, program?.payoutMode, program?.environment],
+ );
+
+ return useMemo(
+ () => ({
+ getBatchConfirm: (payouts: PayoutResponse[]) =>
+ getBatchConfirm(payouts, eligibility),
+ }),
+ [eligibility],
+ );
+}
diff --git a/apps/web/lib/api/payouts/get-eligible-payouts.ts b/apps/web/lib/api/payouts/get-eligible-payouts.ts
index 8fc4b2c702..ca0ddfc919 100644
--- a/apps/web/lib/api/payouts/get-eligible-payouts.ts
+++ b/apps/web/lib/api/payouts/get-eligible-payouts.ts
@@ -16,7 +16,10 @@ import { payoutIdSelectionWhere } from "./payout-id-selection-where";
interface GetEligiblePayoutsProps
extends z.output<typeof eligiblePayoutsQuerySchema> {
- program: Pick<Program, "id" | "name" | "minPayoutAmount" | "payoutMode">;
+ program: Pick<
+ Program,
+ "id" | "name" | "minPayoutAmount" | "payoutMode" | "environment"
+ >;
}
export async function getEligiblePayouts({
diff --git a/apps/web/lib/api/payouts/payout-eligibility-filter.ts b/apps/web/lib/api/payouts/payout-eligibility-filter.ts
index 2cc04531f1..525b18e951 100644
--- a/apps/web/lib/api/payouts/payout-eligibility-filter.ts
+++ b/apps/web/lib/api/payouts/payout-eligibility-filter.ts
@@ -1,13 +1,17 @@
+import { isProductionEnvironment } from "@/lib/sandbox/environment";
import {
TREMENDOUS_MAX_PAYOUT_AMOUNT_CENTS,
TREMENDOUS_MIN_PAYOUT_AMOUNT_CENTS,
} from "@/lib/tremendous/constants";
-import { Prisma, Program } from "@prisma/client";
+import { Prisma, Program, ProgramPayoutMode } from "@prisma/client";
export function getPayoutEligibilityFilter({
program,
}: {
- program: Pick<Program, "id" | "minPayoutAmount" | "payoutMode">;
+ program: Pick<
+ Program,
+ "id" | "minPayoutAmount" | "payoutMode" | "environment"
+ >;
}): Prisma.PayoutWhereInput {
const commonWhere: Prisma.PayoutWhereInput = {
programId: program.id,
@@ -28,6 +32,20 @@ export function getPayoutEligibilityFilter({
},
};
+ // Non-production internal and hybrid payouts are simulated, so a missing
+ // payout connection should not block them. External mode still requires a tenant ID.
+ if (
+ !isProductionEnvironment(program.environment) &&
+ (
+ [
+ ProgramPayoutMode.internal,
+ ProgramPayoutMode.hybrid,
+ ] as ProgramPayoutMode[]
+ ).includes(program.payoutMode)
+ ) {
+ return commonWhere;
+ }
+
switch (program.payoutMode) {
// Internal mode: all payouts are internal, require payoutsEnabledAt !== null
case "internal":
diff --git a/apps/web/lib/zod/schemas/programs.ts b/apps/web/lib/zod/schemas/programs.ts
index 42c4857deb..1c5f38e1ef 100644
--- a/apps/web/lib/zod/schemas/programs.ts
+++ b/apps/web/lib/zod/schemas/programs.ts
@@ -11,6 +11,7 @@ import {
ProgramApplicationRejectionReason,
ProgramEnrollmentStatus,
ProgramPayoutMode,
+ WorkspaceEnvironment,
} from "@prisma/client";
import * as z from "zod/v4";
import { DiscountSchema } from "./discount";
@@ -66,6 +67,7 @@ export const ProgramSchema = z.object({
messagingEnabledAt: z.date().nullish(),
partnerNetworkEnabledAt: z.date().nullish(),
payoutMode: z.enum(ProgramPayoutMode).default("internal"),
+ environment: z.enum(WorkspaceEnvironment),
rewards: z.array(RewardSchema).nullish(),
discounts: z.array(DiscountSchema).nullish(),
categories: z.array(z.enum(Category)).nullish(),
diff --git a/apps/web/ui/partners/partner-row-item.tsx b/apps/web/ui/partners/partner-row-item.tsx
index fdd11ed978..533967d04a 100644
--- a/apps/web/ui/partners/partner-row-item.tsx
+++ b/apps/web/ui/partners/partner-row-item.tsx
@@ -1,7 +1,12 @@
+import { isProductionEnvironment } from "@/lib/sandbox/environment";
import useProgram from "@/lib/swr/use-program";
import { CircleArrowRight, DynamicTooltipWrapper, GreekTemple } from "@dub/ui";
import { cn, formatDateTimeSmart } from "@dub/utils";
-import { PartnerNetworkStatus, PartnerPayoutMethod } from "@prisma/client";
+import {
+ PartnerNetworkStatus,
+ PartnerPayoutMethod,
+ WorkspaceEnvironment,
+} from "@prisma/client";
import { CircleMinus } from "lucide-react";
import Link from "next/link";
import { useParams } from "next/navigation";
@@ -68,51 +73,64 @@ const PAYOUT_STATUS_CONFIG = {
iconClassName: "border-red-300 bg-red-200 text-red-800",
indicatorColor: "bg-red-500",
},
+ simulated: {
+ indicatorColor: "bg-yellow-500",
+ },
} as const;
function usePartnerPayoutStatus(partner: PartnerRowItemProps["partner"]) {
const { program } = useProgram();
const showPayoutsEnabled = "payoutsEnabledAt" in partner;
+ let statusKey: keyof typeof PAYOUT_STATUS_CONFIG | null = null;
- const isExternalPayoutEnabled =
- showPayoutsEnabled &&
- (() => {
- switch (program?.payoutMode) {
- case "external":
- return true;
- case "hybrid":
- return partner.payoutsEnabledAt === null;
- case "internal":
- return false;
- default:
- return false;
- }
- })();
-
- const statusKey: keyof typeof PAYOUT_STATUS_CONFIG | null = showPayoutsEnabled
- ? isExternalPayoutEnabled
- ? "external"
- : partner.payoutsEnabledAt
- ? "enabled"
- : "disabled"
- : null;
+ // Non-production programs always show a simulated payout. In production, external
+ // programs and unconnected hybrid partners are external; everyone else is
+ // enabled once payouts are connected and disabled until then.
+ if (showPayoutsEnabled && program) {
+ if (!isProductionEnvironment(program.environment)) {
+ statusKey = "simulated";
+ } else if (
+ program?.payoutMode === "external" ||
+ (program?.payoutMode === "hybrid" && partner.payoutsEnabledAt === null)
+ ) {
+ statusKey = "external";
+ } else if (partner.payoutsEnabledAt) {
+ statusKey = "enabled";
+ } else {
+ statusKey = "disabled";
+ }
+ }
return {
statusKey,
showPayoutsEnabled,
+ environment: program?.environment,
};
}
function PartnerPayoutStatusTooltip({
statusKey,
partner,
+ environment,
}: {
statusKey: keyof typeof PAYOUT_STATUS_CONFIG | null;
partner: PartnerRowItemProps["partner"];
+ environment?: WorkspaceEnvironment;
}) {
if (!statusKey) return null;
+ if (statusKey === "simulated") {
+ if (!environment) return null;
+
+ return (
+ <div className="max-w-xs text-pretty p-2.5 text-sm text-neutral-500">
+ This is a {environment} workspace so payouts will be simulated, no real
+ money will be transferred.
+ </div>
+ );
+ }
+
const {
title,
description,
@@ -175,7 +193,8 @@ export function PartnerRowItem({
suffix,
}: PartnerRowItemProps) {
const { slug } = useParams();
- const { statusKey, showPayoutsEnabled } = usePartnerPayoutStatus(partner);
+ const { statusKey, showPayoutsEnabled, environment } =
+ usePartnerPayoutStatus(partner);
const As = showPermalink ? Link : "div";
@@ -190,6 +209,7 @@ export function PartnerRowItem({
<PartnerPayoutStatusTooltip
statusKey={statusKey}
partner={partner}
+ environment={environment}
/>
),
}