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.

split payout-methods and dots-user

Steven Tey8cc935760e51
diff --git a/apps/web/app/api/partners/[partnerId]/dots-user/route.ts b/apps/web/app/api/partners/[partnerId]/dots-user/route.ts
index d68c9718ed..321cddb01c 100644
--- a/apps/web/app/api/partners/[partnerId]/dots-user/route.ts
+++ b/apps/web/app/api/partners/[partnerId]/dots-user/route.ts
@@ -1,38 +1,9 @@
import { withPartner } from "@/lib/auth/partner";
-import { retrieveDotsFlow } from "@/lib/dots/retrieve-dots-flow";
import { retrieveDotsUser } from "@/lib/dots/retrieve-dots-user";
-import { prisma } from "@/lib/prisma";
-import { redis } from "@/lib/upstash";
import { NextResponse } from "next/server";
// GET /api/partners/[partnerId]/dots-user – get Dots user info for a partner
export const GET = withPartner(async ({ partner }) => {
- let dotsUserId = partner.dotsUserId;
-
- if (!dotsUserId) {
- const cachedFlowId = await redis.get<string>(
- `dots-flow-cache:${partner.id}`,
- );
- if (cachedFlowId) {
- const flow = await retrieveDotsFlow({ flowId: cachedFlowId });
- if (flow.user_id) {
- dotsUserId = flow.user_id;
- await Promise.allSettled([
- prisma.partner.update({
- where: { id: partner.id },
- data: { dotsUserId },
- }),
- redis.del(`dots-flow-cache:${partner.id}`),
- ]);
- }
- }
- }
-
- // if there is still no dots ID
- if (!dotsUserId) {
- return NextResponse.json({});
- }
-
const dotsUser = await retrieveDotsUser(partner);
return NextResponse.json(dotsUser);
diff --git a/apps/web/app/api/partners/[partnerId]/payout-methods/route.ts b/apps/web/app/api/partners/[partnerId]/payout-methods/route.ts
new file mode 100644
index 0000000000..7f47d32270
--- /dev/null
+++ b/apps/web/app/api/partners/[partnerId]/payout-methods/route.ts
@@ -0,0 +1,14 @@
+import { withPartner } from "@/lib/auth/partner";
+import { retrievePayoutMethods } from "@/lib/dots/retrieve-payout-methods";
+import { NextResponse } from "next/server";
+
+// GET /api/partners/[partnerId]/payout-methods – get payout methods for a partner
+export const GET = withPartner(async ({ partner }) => {
+ if (!partner.dotsUserId) {
+ return NextResponse.json([]);
+ }
+
+ const payoutMethods = await retrievePayoutMethods(partner.dotsUserId);
+
+ return NextResponse.json(payoutMethods);
+});
diff --git a/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/settings/payouts/page-client.tsx b/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/settings/payouts/page-client.tsx
index a2f4c4f536..b9d6567616 100644
--- a/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/settings/payouts/page-client.tsx
+++ b/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/settings/payouts/page-client.tsx
@@ -3,6 +3,7 @@
import { createDotsFlowAction } from "@/lib/actions/partners/create-dots-flow";
import useDotsUser from "@/lib/swr/use-dots-user";
import usePartnerProfile from "@/lib/swr/use-partner-profile";
+import usePayoutMethods from "@/lib/swr/use-payout-methods";
import LayoutLoader from "@/ui/layout/layout-loader";
import { usePayoutWithdrawSheet } from "@/ui/partners/payout-withdraw-sheet";
import { AnimatedEmptyState } from "@/ui/shared/animated-empty-state";
@@ -21,13 +22,16 @@ import { useState } from "react";
import { toast } from "sonner";
import { PartnerWithdrawalsActivity } from "./activity";
import { ComplianceButton } from "./compliance-button";
-import PayoutMethodCard from "./payout-method-card";
+import PayoutMethodCard, {
+ PayoutMethodCardSkeleton,
+} from "./payout-method-card";
export function PayoutsSettingsPageClient() {
const { partnerId } = useParams<{ partnerId: string }>();
const { partner } = usePartnerProfile();
const { dotsUser, isLoading, mutate } = useDotsUser();
+ const { payoutMethods, isLoading: payoutMethodsLoading } = usePayoutMethods();
const { executeAsync, isExecuting } = useAction(createDotsFlowAction, {
onSuccess({ data }) {
@@ -79,79 +83,81 @@ export function PayoutsSettingsPageClient() {
{partner?.dotsUserId && dotsUser?.verified ? (
<div className="grid grid-cols-1 gap-4">
<div className="grid gap-4 rounded-lg border border-neutral-300 bg-white p-5">
- <div className="grid divide-neutral-200 rounded-lg border border-neutral-200 bg-neutral-50 max-sm:divide-y sm:grid-cols-[repeat(2,minmax(0,1fr))] sm:divide-x">
- <div className="flex flex-col p-4">
- <div className="flex flex-wrap justify-between gap-x-5 gap-y-1">
- <div className="whitespace-nowrap p-1 text-sm text-neutral-500">
- Withdrawable balance
- </div>
- <div>
- {dotsUser ? (
- <Button
- text="Withdraw funds"
- onClick={() => setShowPayoutWithdrawSheet(true)}
- className="h-7 w-fit px-2"
- disabledTooltip={
- dotsUser.payout_methods.length === 0
- ? "You need to connect a payout method first"
- : !dotsUser.compliance.submitted
- ? "You need to verify your identity first"
- : !dotsUser.wallet.withdrawable_amount
- ? "You need a positive balance to withdraw funds"
- : undefined
- }
- />
- ) : (
- <div className="h-7 w-24 animate-pulse rounded-md bg-neutral-200" />
- )}
- </div>
- </div>
- <div className="mt-6 flex grow flex-col justify-end p-1">
- <div className="flex flex-wrap items-end justify-between gap-x-4 gap-y-3">
- <div className="whitespace-nowrap text-2xl text-neutral-800">
- {currencyFormatter(
- dotsUser.wallet.withdrawable_amount / 100,
- {
- minimumFractionDigits: 2,
- maximumFractionDigits: 2,
- },
- )}{" "}
- USD
+ {dotsUser?.wallet && (
+ <div className="grid divide-neutral-200 rounded-lg border border-neutral-200 bg-neutral-50 max-sm:divide-y sm:grid-cols-[repeat(2,minmax(0,1fr))] sm:divide-x">
+ <div className="flex flex-col p-4">
+ <div className="flex flex-wrap justify-between gap-x-5 gap-y-1">
+ <div className="whitespace-nowrap p-1 text-sm text-neutral-500">
+ Withdrawable balance
</div>
- {partner.country === "US" &&
- dotsUser.wallet.withdrawable_amount > 0 && (
- <FreeWithdrawalProgress
- balance={dotsUser.wallet.withdrawable_amount}
+ <div>
+ {dotsUser ? (
+ <Button
+ text="Withdraw funds"
+ onClick={() => setShowPayoutWithdrawSheet(true)}
+ className="h-7 w-fit px-2"
+ disabledTooltip={
+ payoutMethods.length === 0
+ ? "You need to connect a payout method first"
+ : !dotsUser.compliance.submitted
+ ? "You need to verify your identity first"
+ : !dotsUser.wallet.withdrawable_amount
+ ? "You need a positive balance to withdraw funds"
+ : undefined
+ }
/>
+ ) : (
+ <div className="h-7 w-24 animate-pulse rounded-md bg-neutral-200" />
)}
+ </div>
</div>
- </div>
- </div>
- <div className="flex flex-col p-4">
- <div className="flex justify-between gap-5">
- <div className="p-1 text-sm text-neutral-500">
- Pending balance
+ <div className="mt-6 flex grow flex-col justify-end p-1">
+ <div className="flex flex-wrap items-end justify-between gap-x-4 gap-y-3">
+ <div className="whitespace-nowrap text-2xl text-neutral-800">
+ {currencyFormatter(
+ dotsUser.wallet.withdrawable_amount / 100,
+ {
+ minimumFractionDigits: 2,
+ maximumFractionDigits: 2,
+ },
+ )}{" "}
+ USD
+ </div>
+ {partner.country === "US" &&
+ dotsUser.wallet.withdrawable_amount > 0 && (
+ <FreeWithdrawalProgress
+ balance={dotsUser.wallet.withdrawable_amount}
+ />
+ )}
+ </div>
</div>
</div>
- <div className="mt-6 flex grow flex-col justify-end p-1">
- {dotsUser ? (
- <div className="text-2xl text-neutral-800">
- {currencyFormatter(
- dotsUser?.wallet.pending_amount / 100,
- {
- minimumFractionDigits: 2,
- maximumFractionDigits: 2,
- },
- )}{" "}
- USD
+ <div className="flex flex-col p-4">
+ <div className="flex justify-between gap-5">
+ <div className="p-1 text-sm text-neutral-500">
+ Pending balance
</div>
- ) : (
- <div className="h-8 w-32 animate-pulse rounded bg-neutral-200" />
- )}
+ </div>
+ <div className="mt-6 flex grow flex-col justify-end p-1">
+ {dotsUser ? (
+ <div className="text-2xl text-neutral-800">
+ {currencyFormatter(
+ dotsUser?.wallet.pending_amount / 100,
+ {
+ minimumFractionDigits: 2,
+ maximumFractionDigits: 2,
+ },
+ )}{" "}
+ USD
+ </div>
+ ) : (
+ <div className="h-8 w-32 animate-pulse rounded bg-neutral-200" />
+ )}
+ </div>
</div>
</div>
- </div>
- {dotsUser?.payout_methods.length === 0 ? (
+ )}
+ {!payoutMethodsLoading && payoutMethods.length === 0 ? (
<AnimatedEmptyState
title="No payout methods connected"
description="Connect a payout method to start withdrawing funds"
@@ -188,15 +194,19 @@ export function PayoutsSettingsPageClient() {
/>
</div>
<div className="grid gap-4">
- {dotsUser.payout_methods.map(
- ({ platform, default: isDefault }) => (
- <PayoutMethodCard
- key={platform}
- platform={platform}
- isDefault={isDefault}
- />
- ),
- )}
+ {payoutMethodsLoading
+ ? Array.from({ length: 3 }).map((_, i) => (
+ <PayoutMethodCardSkeleton key={i} />
+ ))
+ : payoutMethods.map(
+ ({ platform, default: isDefault }) => (
+ <PayoutMethodCard
+ key={platform}
+ platform={platform}
+ isDefault={isDefault}
+ />
+ ),
+ )}
</div>
</div>
)}
diff --git a/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/settings/payouts/payout-method-card.tsx b/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/settings/payouts/payout-method-card.tsx
index dcddb22062..de8bf249e8 100644
--- a/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/settings/payouts/payout-method-card.tsx
+++ b/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/settings/payouts/payout-method-card.tsx
@@ -39,3 +39,14 @@ export default function PayoutMethodCard({
</div>
);
}
+
+export function PayoutMethodCardSkeleton() {
+ return (
+ <div className="flex items-center justify-between rounded-lg border border-neutral-200 bg-white p-4">
+ <div className="flex items-center gap-3">
+ <div className="h-10 w-10 animate-pulse rounded-full bg-neutral-200" />
+ <div className="h-4 w-24 min-w-0 animate-pulse rounded-md bg-neutral-200" />
+ </div>
+ </div>
+ );
+}
diff --git a/apps/web/lib/dots/retrieve-dots-user.ts b/apps/web/lib/dots/retrieve-dots-user.ts
index ada8f20a86..449dd5a6c0 100644
--- a/apps/web/lib/dots/retrieve-dots-user.ts
+++ b/apps/web/lib/dots/retrieve-dots-user.ts
@@ -3,34 +3,28 @@ import { dotsFetch } from "./fetch";
import { dotsUserSchema } from "./schemas";
export const retrieveDotsUser = async (partner: PartnerProps) => {
- const [dotsUser, payoutMethods] = await Promise.all([
- dotsFetch(`/users/${partner.dotsUserId}`, {
- method: "GET",
- dotsAppId: "default",
- }),
- dotsFetch(`/users/${partner.dotsUserId}/payout-methods`, {
- method: "GET",
- dotsAppId: "default",
- }),
- ]);
+ const dotsUser = await dotsFetch(`/users/${partner.dotsUserId}`, {
+ method: "GET",
+ dotsAppId: "default",
+ });
return dotsUserSchema.parse({
...dotsUser,
- compliance: {
- ...dotsUser.compliance,
- submitted:
- partner.country === "US"
- ? dotsUser.compliance.w9.tax_id_collected
- : dotsUser.compliance.w8_ben_collected,
- },
- wallet: {
- ...dotsUser.wallet,
- pending_amount:
- dotsUser.wallet.amount - dotsUser.wallet.withdrawable_amount,
- },
- payout_methods: payoutMethods.map((method) => ({
- ...method,
- default: dotsUser.default_payout_method === method.platform,
- })),
+ ...(dotsUser.compliance && {
+ compliance: {
+ ...dotsUser.compliance,
+ submitted:
+ partner.country === "US"
+ ? dotsUser.compliance.w9.tax_id_collected
+ : dotsUser.compliance.w8_ben_collected,
+ },
+ }),
+ ...(dotsUser.wallet && {
+ wallet: {
+ ...dotsUser.wallet,
+ pending_amount:
+ dotsUser.wallet.amount - dotsUser.wallet.withdrawable_amount,
+ },
+ }),
});
};
diff --git a/apps/web/lib/dots/retrieve-payout-methods.ts b/apps/web/lib/dots/retrieve-payout-methods.ts
new file mode 100644
index 0000000000..e39fe99a4f
--- /dev/null
+++ b/apps/web/lib/dots/retrieve-payout-methods.ts
@@ -0,0 +1,11 @@
+import { dotsFetch } from "./fetch";
+import { payoutMethodSchema } from "./schemas";
+
+export const retrievePayoutMethods = async (dotsUserId: string) => {
+ const payoutMethods = await dotsFetch(`/users/${dotsUserId}/payout-methods`, {
+ method: "GET",
+ dotsAppId: "default",
+ });
+
+ return payoutMethods.map((method) => payoutMethodSchema.parse(method));
+};
diff --git a/apps/web/lib/dots/schemas.ts b/apps/web/lib/dots/schemas.ts
index d1af9c56dd..3f423d197b 100644
--- a/apps/web/lib/dots/schemas.ts
+++ b/apps/web/lib/dots/schemas.ts
@@ -113,11 +113,12 @@ export const dotsUserSchema = z.object({
}),
status: z.enum(["verified", "unverified", "disabled", "in_review"]),
verified: z.boolean(),
- wallet: z.object({
- withdrawable_amount: z.number(),
- pending_amount: z.number(),
- }),
- default_payout_method: dotsPayoutPlatforms.nullable(),
- payout_methods: z.array(payoutMethodSchema),
+ wallet: z
+ .object({
+ withdrawable_amount: z.number(),
+ pending_amount: z.number(),
+ })
+ .nullish(),
+ default_payout_method: dotsPayoutPlatforms.nullish(),
compliance: z.any(),
});
diff --git a/apps/web/lib/swr/use-payout-methods.ts b/apps/web/lib/swr/use-payout-methods.ts
new file mode 100644
index 0000000000..a260de3fe7
--- /dev/null
+++ b/apps/web/lib/swr/use-payout-methods.ts
@@ -0,0 +1,36 @@
+import { fetcher } from "@dub/utils";
+import { useParams } from "next/navigation";
+import { useMemo } from "react";
+import useSWR from "swr";
+import { PayoutMethod } from "../dots/types";
+import useDotsUser from "./use-dots-user";
+
+export default function usePayoutMethods() {
+ const { partnerId } = useParams();
+
+ const { dotsUser } = useDotsUser();
+
+ const {
+ data: payoutMethods,
+ error,
+ isLoading,
+ mutate,
+ } = useSWR<PayoutMethod[]>(
+ partnerId ? `/api/partners/${partnerId}/payout-methods` : null,
+ fetcher,
+ );
+
+ const payoutMethodsWithDefault = useMemo(() => {
+ return payoutMethods?.map((method) => ({
+ ...method,
+ default: dotsUser?.default_payout_method === method.platform,
+ }));
+ }, [payoutMethods, dotsUser]);
+
+ return {
+ payoutMethods: payoutMethodsWithDefault || [],
+ error,
+ isLoading,
+ mutate,
+ };
+}
diff --git a/apps/web/ui/partners/payout-withdraw-sheet.tsx b/apps/web/ui/partners/payout-withdraw-sheet.tsx
index df529c70b2..2eabcd8955 100644
--- a/apps/web/ui/partners/payout-withdraw-sheet.tsx
+++ b/apps/web/ui/partners/payout-withdraw-sheet.tsx
@@ -2,6 +2,7 @@ import { withdrawFundsAction } from "@/lib/actions/partners/withdraw-funds";
import { DotsPayoutPlatform } from "@/lib/dots/types";
import useDotsUser from "@/lib/swr/use-dots-user";
import usePartnerProfile from "@/lib/swr/use-partner-profile";
+import usePayoutMethods from "@/lib/swr/use-payout-methods";
import { X } from "@/ui/shared/icons";
import { Button, Icon, Sheet } from "@dub/ui";
import {
@@ -32,6 +33,7 @@ function PayoutWithdrawSheetContent({ setIsOpen }: PayoutWithdrawSheetProps) {
const { partnerId } = useParams<{ partnerId: string }>();
const { partner, error: partnerError } = usePartnerProfile();
const { dotsUser, error: dotsUserError } = useDotsUser();
+ const { payoutMethods } = usePayoutMethods();
const summaryData = useMemo(
() => ({
@@ -57,8 +59,8 @@ function PayoutWithdrawSheetContent({ setIsOpen }: PayoutWithdrawSheetProps) {
Date: formatDate(new Date(), { month: "short" }),
Total: dotsUserError ? (
"-"
- ) : dotsUser ? (
- currencyFormatter((dotsUser?.wallet.withdrawable_amount ?? 0) / 100, {
+ ) : dotsUser?.wallet ? (
+ currencyFormatter((dotsUser.wallet.withdrawable_amount ?? 0) / 100, {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
})
@@ -73,11 +75,10 @@ function PayoutWithdrawSheetContent({ setIsOpen }: PayoutWithdrawSheetProps) {
useState<DotsPayoutPlatform | null>(null);
useEffect(() => {
- if (!dotsUser?.payout_methods?.length || selectedPayoutMethod !== null)
- return;
+ if (!payoutMethods.length || selectedPayoutMethod !== null) return;
setSelectedPayoutMethod(
- dotsUser.default_payout_method ?? dotsUser.payout_methods[0].platform,
+ dotsUser?.default_payout_method ?? payoutMethods[0].platform,
);
}, [dotsUser, selectedPayoutMethod]);
@@ -124,9 +125,9 @@ function PayoutWithdrawSheetContent({ setIsOpen }: PayoutWithdrawSheetProps) {
<div className="p-6 pt-2">
<div className="text-base font-medium text-neutral-900">Method</div>
<div className="mt-4 flex flex-col gap-2">
- {dotsUser?.payout_methods ? (
+ {payoutMethods.length ? (
<>
- {dotsUser.payout_methods
+ {payoutMethods
.sort((a, b) => Number(b.default) - Number(a.default)) // Show default first
.map((method) => {
const platform =