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.

Add ability to confirm selected payout

Steven Tey6df433c55e17
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 ca8e3751cf..3364a37da2 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
@@ -33,6 +33,7 @@ export async function processPayouts({
invoiceId,
paymentMethodId,
cutoffPeriod,
+ selectedPayoutId,
excludedPayoutIds,
}: {
workspace: Pick<
@@ -50,6 +51,7 @@ export async function processPayouts({
invoiceId: string;
paymentMethodId: string;
cutoffPeriod?: CUTOFF_PERIOD_TYPES;
+ selectedPayoutId?: string;
excludedPayoutIds?: string[];
}) {
const cutoffPeriodValue = CUTOFF_PERIOD.find(
@@ -58,6 +60,11 @@ export async function processPayouts({
const payouts = await prisma.payout.findMany({
where: {
+ ...(selectedPayoutId
+ ? { id: selectedPayoutId }
+ : excludedPayoutIds
+ ? { id: { notIn: excludedPayoutIds } }
+ : {}),
programId: program.id,
status: "pending",
invoiceId: null,
@@ -82,7 +89,6 @@ export async function processPayouts({
},
],
}),
- ...(excludedPayoutIds && { id: { notIn: excludedPayoutIds } }),
},
select: {
id: true,
diff --git a/apps/web/app/(ee)/api/cron/payouts/process/route.ts b/apps/web/app/(ee)/api/cron/payouts/process/route.ts
index cc72cde793..bd260775aa 100644
--- a/apps/web/app/(ee)/api/cron/payouts/process/route.ts
+++ b/apps/web/app/(ee)/api/cron/payouts/process/route.ts
@@ -16,6 +16,7 @@ const processPayoutsCronSchema = z.object({
invoiceId: z.string(),
paymentMethodId: z.string(),
cutoffPeriod: CUTOFF_PERIOD_ENUM,
+ selectedPayoutId: z.string().optional(),
excludedPayoutIds: z.array(z.string()).optional(),
});
@@ -34,6 +35,7 @@ export async function POST(req: Request) {
invoiceId,
paymentMethodId,
cutoffPeriod,
+ selectedPayoutId,
excludedPayoutIds,
} = processPayoutsCronSchema.parse(JSON.parse(rawBody));
@@ -53,6 +55,7 @@ export async function POST(req: Request) {
await splitPayouts({
program,
cutoffPeriod,
+ selectedPayoutId,
excludedPayoutIds,
});
}
@@ -64,6 +67,7 @@ export async function POST(req: Request) {
invoiceId,
paymentMethodId,
cutoffPeriod,
+ selectedPayoutId,
excludedPayoutIds,
});
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 e5ad661ec7..bbf797951f 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
@@ -10,14 +10,21 @@ import { endOfMonth } from "date-fns";
export async function splitPayouts({
program,
cutoffPeriod,
+ selectedPayoutId,
excludedPayoutIds,
}: {
program: Pick<Program, "id" | "name" | "minPayoutAmount">;
cutoffPeriod: CUTOFF_PERIOD_TYPES;
+ selectedPayoutId?: string;
excludedPayoutIds?: string[];
}) {
const payouts = await prisma.payout.findMany({
where: {
+ ...(selectedPayoutId
+ ? { id: selectedPayoutId }
+ : excludedPayoutIds
+ ? { id: { notIn: excludedPayoutIds } }
+ : {}),
programId: program.id,
status: "pending",
invoiceId: null,
@@ -32,7 +39,6 @@ export async function splitPayouts({
periodStart: {
not: null, // exclude the manual payouts
},
- ...(excludedPayoutIds && { id: { notIn: excludedPayoutIds } }),
},
include: {
commissions: true,
diff --git a/apps/web/app/(ee)/api/programs/[programId]/payouts/eligible/route.ts b/apps/web/app/(ee)/api/programs/[programId]/payouts/eligible/route.ts
index 27457a814c..ed231d2561 100644
--- a/apps/web/app/(ee)/api/programs/[programId]/payouts/eligible/route.ts
+++ b/apps/web/app/(ee)/api/programs/[programId]/payouts/eligible/route.ts
@@ -10,8 +10,9 @@ import { prisma } from "@dub/prisma";
import { NextResponse } from "next/server";
import z from "zod";
-const confirmPayoutsQuerySchema = z.object({
+const eligiblePayoutsQuerySchema = z.object({
cutoffPeriod: CUTOFF_PERIOD_ENUM,
+ selectedPayoutId: z.string().optional(),
});
/*
@@ -28,7 +29,8 @@ const confirmPayoutsQuerySchema = z.object({
export const GET = withWorkspace(async ({ workspace, searchParams }) => {
const programId = getDefaultProgramIdOrThrow(workspace);
- const { cutoffPeriod } = confirmPayoutsQuerySchema.parse(searchParams);
+ const { cutoffPeriod, selectedPayoutId } =
+ eligiblePayoutsQuerySchema.parse(searchParams);
const { minPayoutAmount } = await getProgramOrThrow({
workspaceId: workspace.id,
@@ -41,6 +43,7 @@ export const GET = withWorkspace(async ({ workspace, searchParams }) => {
let payouts = await prisma.payout.findMany({
where: {
+ ...(selectedPayoutId && { id: selectedPayoutId }),
programId,
status: "pending",
amount: {
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/payouts/payout-details-sheet.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/payouts/payout-details-sheet.tsx
index 2fa2b6f678..8b546c40ac 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/payouts/payout-details-sheet.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/payouts/payout-details-sheet.tsx
@@ -251,12 +251,12 @@ function PayoutDetailsSheetContent({ payout }: PayoutDetailsSheetProps) {
{payout.status === "pending" ? (
<Button
type="button"
- variant="secondary"
- text="Confirm all pending payouts"
+ text="Confirm payout"
onClick={() => {
queryParams({
set: {
confirmPayouts: "true",
+ selectedPayoutId: payout.id,
},
del: "payoutId",
scroll: false,
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/payouts/payout-stats.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/payouts/payout-stats.tsx
index a6f3d85b16..fe0a953df7 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/payouts/payout-stats.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/payouts/payout-stats.tsx
@@ -3,7 +3,7 @@
import usePayoutsCount from "@/lib/swr/use-payouts-count";
import useWorkspace from "@/lib/swr/use-workspace";
import { PayoutsCount } from "@/lib/types";
-import { PayoutInvoiceSheet } from "@/ui/partners/payout-invoice-sheet";
+import { ConfirmPayoutsSheet } from "@/ui/partners/confirm-payouts-sheet";
import { PayoutStatus } from "@dub/prisma/client";
import {
Button,
@@ -68,7 +68,7 @@ export function PayoutStats() {
return (
<>
- <PayoutInvoiceSheet />
+ <ConfirmPayoutsSheet />
<div className="grid grid-cols-1 divide-neutral-200 rounded-lg border border-neutral-200 bg-neutral-50 max-sm:divide-y sm:grid-cols-2 sm:divide-x">
<div className="flex flex-col p-4">
<div className="flex justify-between gap-5">
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 57e012c36a..dfce6f656f 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
@@ -68,7 +68,7 @@ const PayoutTableInner = memo(
? `/api/programs/${defaultProgramId}/payouts${getQueryString(
{ workspaceId },
{
- exclude: ["payoutId"],
+ include: ["partnerId", "status", "sortBy", "sortOrder"],
},
)}`
: undefined,
diff --git a/apps/web/lib/actions/partners/confirm-payouts.ts b/apps/web/lib/actions/partners/confirm-payouts.ts
index 36163d2938..0af1a01bee 100644
--- a/apps/web/lib/actions/partners/confirm-payouts.ts
+++ b/apps/web/lib/actions/partners/confirm-payouts.ts
@@ -18,6 +18,7 @@ const confirmPayoutsSchema = z.object({
workspaceId: z.string(),
paymentMethodId: z.string(),
cutoffPeriod: CUTOFF_PERIOD_ENUM,
+ selectedPayoutId: z.string().optional(),
excludedPayoutIds: z.array(z.string()).optional(),
fastSettlement: z.boolean().optional().default(false),
amount: z.number(),
@@ -33,6 +34,7 @@ export const confirmPayoutsAction = authActionClient
const {
paymentMethodId,
cutoffPeriod,
+ selectedPayoutId,
excludedPayoutIds,
fastSettlement,
amount,
@@ -133,6 +135,7 @@ export const confirmPayoutsAction = authActionClient
invoiceId: invoice.id,
paymentMethodId,
cutoffPeriod,
+ selectedPayoutId,
excludedPayoutIds,
},
});
diff --git a/apps/web/ui/partners/payout-invoice-sheet.tsx b/apps/web/ui/partners/confirm-payouts-sheet.tsx
similarity index 94%
rename from apps/web/ui/partners/payout-invoice-sheet.tsx
rename to apps/web/ui/partners/confirm-payouts-sheet.tsx
index 33cd5756ae..5aa327211a 100644
--- a/apps/web/ui/partners/payout-invoice-sheet.tsx
+++ b/apps/web/ui/partners/confirm-payouts-sheet.tsx
@@ -58,7 +58,7 @@ type SelectPaymentMethod =
fastSettlement: boolean;
};
-function PayoutInvoiceSheetContent() {
+function ConfirmPayoutsSheetContent() {
const router = useRouter();
const {
id: workspaceId,
@@ -81,6 +81,10 @@ function PayoutInvoiceSheetContent() {
const [cutoffPeriod, setCutoffPeriod] =
useState<CUTOFF_PERIOD_TYPES>("today");
+ const { queryParams, searchParamsObj } = useRouterStuff();
+
+ const selectedPayoutId = searchParamsObj.selectedPayoutId;
+
const {
data: eligiblePayouts,
error: eligiblePayoutsError,
@@ -89,19 +93,22 @@ function PayoutInvoiceSheetContent() {
`/api/programs/${defaultProgramId}/payouts/eligible?${new URLSearchParams({
workspaceId,
cutoffPeriod,
+ selectedPayoutId,
} as Record<string, any>).toString()}`,
fetcher,
);
- const [excludedPayoutIds, setExcludedPayoutIds] = useState<string[]>([]);
+ const excludedPayoutIds = searchParamsObj.excludedPayoutIds?.split(",") || [];
- const includedPayouts = useMemo(
- () =>
- eligiblePayouts?.filter(
- (payout) => !excludedPayoutIds.includes(payout.id),
- ),
- [eligiblePayouts, excludedPayoutIds],
- );
+ const finalEligiblePayouts = useMemo(() => {
+ // if there's a selected payout id, return the payout directly
+ if (selectedPayoutId) return eligiblePayouts;
+
+ // else, we need to filter out the excluded payout ids (if specified)
+ return eligiblePayouts?.filter(
+ (payout) => !excludedPayoutIds.includes(payout.id),
+ );
+ }, [eligiblePayouts, selectedPayoutId, excludedPayoutIds]);
const { executeAsync: confirmPayouts } = useAction(confirmPayoutsAction, {
onError({ error }) {
@@ -215,7 +222,7 @@ function PayoutInvoiceSheetContent() {
}, [finalPaymentMethods, selectedPaymentMethod]);
const { amount, fee, total, fastAchFee } = useMemo(() => {
- const amount = includedPayouts?.reduce((acc, payout) => {
+ const amount = finalEligiblePayouts?.reduce((acc, payout) => {
return acc + payout.amount;
}, 0);
@@ -241,7 +248,7 @@ function PayoutInvoiceSheetContent() {
total,
fastAchFee,
};
- }, [includedPayouts, selectedPaymentMethod]);
+ }, [finalEligiblePayouts, selectedPaymentMethod]);
const invoiceData = useMemo(() => {
return [
@@ -447,11 +454,18 @@ function PayoutInvoiceSheetContent() {
className="h-6 w-fit px-2"
onClick={() =>
// Toggle excluded
- setExcludedPayoutIds((ids) =>
- ids.includes(row.original.id)
- ? ids.filter((id) => id !== row.original.id)
- : [...ids, row.original.id],
- )
+ queryParams({
+ set: {
+ excludedPayoutIds: excludedPayoutIds.includes(
+ row.original.id,
+ )
+ ? excludedPayoutIds.filter(
+ (id) => id !== row.original.id,
+ )
+ : [...excludedPayoutIds, row.original.id],
+ },
+ replace: true,
+ })
}
/>
</div>
@@ -558,6 +572,7 @@ function PayoutInvoiceSheetContent() {
paymentMethodId: selectedPaymentMethod.id.replace("-fast", ""),
fastSettlement: selectedPaymentMethod.fastSettlement,
cutoffPeriod,
+ selectedPayoutId,
excludedPayoutIds,
amount: amount ?? 0,
fee: fee ?? 0,
@@ -660,7 +675,7 @@ function FastAchPayoutToggle() {
);
}
-export function PayoutInvoiceSheet() {
+export function ConfirmPayoutsSheet() {
const { queryParams } = useRouterStuff();
const [isOpen, setIsOpen] = useState(false);
const { searchParams } = useRouterStuff();
@@ -681,11 +696,11 @@ export function PayoutInvoiceSheet() {
onOpenChange={setIsOpen}
onClose={() => {
queryParams({
- del: "confirmPayouts",
+ del: ["confirmPayouts", "selectedPayoutId", "excludedPayoutIds"],
});
}}
>
- <PayoutInvoiceSheetContent />
+ <ConfirmPayoutsSheetContent />
</Sheet>
);
}