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.

Refactor payout calculations to include clawbacks and update earnings filtering logic. Introduce CLAWBACK_REASONS_MAP for better reason handling in earnings display.

Kiran K05387547b752
diff --git a/apps/web/app/(ee)/api/cron/payouts/create-payout.ts b/apps/web/app/(ee)/api/cron/payouts/create-payout.ts
index 9e92640487..650abfb931 100644
--- a/apps/web/app/(ee)/api/cron/payouts/create-payout.ts
+++ b/apps/web/app/(ee)/api/cron/payouts/create-payout.ts
@@ -116,12 +116,16 @@ export const createPayout = async ({
`Found ${clawbacks.length} pending clawbacks for partner ${partnerId} in program ${programId}.`,
);
+ const allCommissions = [...commissions, ...clawbacks];
+
// earliest commission date
- const periodStart = commissions[0].createdAt;
+ const periodStart = allCommissions[0].createdAt;
// end of the month of the latest commission date
// e.g. if the latest sale is 2024-12-16, the periodEnd should be 2024-12-31
- const periodEnd = endOfMonth(commissions[commissions.length - 1].createdAt);
+ const periodEnd = endOfMonth(
+ allCommissions[allCommissions.length - 1].createdAt,
+ );
await prisma.$transaction(async (tx) => {
// check if the partner has another pending payout (take the latest entry)
@@ -151,13 +155,13 @@ export const createPayout = async ({
});
}
- console.log(`Payout ID to use: ${payout.id}`);
+ console.log(`Using payout ID ${payout.id}.`);
// update the commissions to processed and set the payoutId
await tx.commission.updateMany({
where: {
id: {
- in: commissions.map(({ id }) => id),
+ in: allCommissions.map(({ id }) => id),
},
},
data: {
@@ -167,7 +171,7 @@ export const createPayout = async ({
});
console.log(
- `Updated ${commissions.length} commissions to processed and set payoutId to ${payout.id}.`,
+ `Updated ${allCommissions.length} commissions to processed and set payoutId to ${payout.id}.`,
);
// get the total earnings for the commissions in the payout
diff --git a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/earnings/count/route.ts b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/earnings/count/route.ts
index 3f9983ed1c..0df02fe0f2 100644
--- a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/earnings/count/route.ts
+++ b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/earnings/count/route.ts
@@ -34,7 +34,7 @@ export const GET = withPartnerProfile(
const where: Prisma.CommissionWhereInput = {
earnings: {
- gt: 0,
+ not: 0,
},
programId: program.id,
partnerId: partner.id,
diff --git a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/earnings/route.ts b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/earnings/route.ts
index 6695c5a83c..0dc49d783c 100644
--- a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/earnings/route.ts
+++ b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/earnings/route.ts
@@ -42,7 +42,9 @@ export const GET = withPartnerProfile(
const earnings = await prisma.commission.findMany({
where: {
earnings: {
- gt: 0,
+ not: {
+ equals: 0,
+ },
},
programId: program.id,
partnerId: partner.id,
diff --git a/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/earnings/earnings-table.tsx b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/earnings/earnings-table.tsx
index 7028e5c28b..b0990552ae 100644
--- a/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/earnings/earnings-table.tsx
+++ b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/earnings/earnings-table.tsx
@@ -3,6 +3,7 @@
import usePartnerEarningsCount from "@/lib/swr/use-partner-earnings-count";
import useProgramEnrollment from "@/lib/swr/use-program-enrollment";
import { PartnerEarningsResponse } from "@/lib/types";
+import { CLAWBACK_REASONS_MAP } from "@/lib/zod/schemas/commissions";
import { CustomerRowItem } from "@/ui/customers/customer-row-item";
import { CommissionStatusBadges } from "@/ui/partners/commission-status-badges";
import { CommissionTypeBadge } from "@/ui/partners/commission-type-badge";
@@ -13,12 +14,14 @@ import {
LinkLogo,
StatusBadge,
Table,
+ Tooltip,
usePagination,
useRouterStuff,
useTable,
} from "@dub/ui";
import { CircleDollar } from "@dub/ui/icons";
import {
+ cn,
currencyFormatter,
fetcher,
formatDateTime,
@@ -160,11 +163,44 @@ export function EarningsTablePartner({ limit }: { limit?: number }) {
id: "earnings",
header: "Earnings",
accessorKey: "earnings",
- cell: ({ row }) =>
- currencyFormatter(row.original.earnings / 100, {
+ cell: ({ row }) => {
+ const commission = row.original;
+
+ const earnings = currencyFormatter(commission.earnings / 100, {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
- }),
+ });
+
+ if (commission.description) {
+ const reason =
+ CLAWBACK_REASONS_MAP[commission.description]?.description ??
+ commission.description;
+
+ return (
+ <Tooltip content={reason}>
+ <span
+ className={cn(
+ "cursor-help truncate underline decoration-dotted underline-offset-2",
+ commission.earnings < 0 && "text-red-600",
+ )}
+ >
+ {earnings}
+ </span>
+ </Tooltip>
+ );
+ }
+
+ return (
+ <span
+ className={cn(
+ commission.earnings < 0 && "text-red-600",
+ "truncate",
+ )}
+ >
+ {earnings}
+ </span>
+ );
+ },
},
{
header: "Status",
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/commissions/commission-table.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/commissions/commission-table.tsx
index 7b1b98a46c..f56e4c1b59 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/commissions/commission-table.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/commissions/commission-table.tsx
@@ -3,7 +3,7 @@
import useCommissionsCount from "@/lib/swr/use-commissions-count";
import useWorkspace from "@/lib/swr/use-workspace";
import { CommissionResponse } from "@/lib/types";
-import { CLAWBACK_REASONS } from "@/lib/zod/schemas/commissions";
+import { CLAWBACK_REASONS_MAP } from "@/lib/zod/schemas/commissions";
import { CustomerRowItem } from "@/ui/customers/customer-row-item";
import { CommissionRowMenu } from "@/ui/partners/commission-row-menu";
import { CommissionStatusBadges } from "@/ui/partners/commission-status-badges";
@@ -24,6 +24,7 @@ import {
} from "@dub/ui";
import { MoneyBill2 } from "@dub/ui/icons";
import {
+ cn,
currencyFormatter,
fetcher,
formatDateTime,
@@ -159,20 +160,34 @@ const CommissionTableInner = memo(
});
if (commission.description) {
- const reasonDescription = CLAWBACK_REASONS.find(
- (r) => r.value === commission.description,
- )?.description;
+ const reason =
+ CLAWBACK_REASONS_MAP[commission.description]?.description ??
+ commission.description;
return (
- <Tooltip content={reasonDescription}>
- <span className="cursor-help truncate underline decoration-dotted underline-offset-2">
+ <Tooltip content={reason}>
+ <span
+ className={cn(
+ "cursor-help truncate underline decoration-dotted underline-offset-2",
+ commission.earnings < 0 && "text-red-600",
+ )}
+ >
{earnings}
</span>
</Tooltip>
);
}
- return earnings;
+ return (
+ <span
+ className={cn(
+ commission.earnings < 0 && "text-red-600",
+ "truncate",
+ )}
+ >
+ {earnings}
+ </span>
+ );
},
},
{
diff --git a/apps/web/lib/zod/schemas/commissions.ts b/apps/web/lib/zod/schemas/commissions.ts
index 4126b75bcb..c16ea2322d 100644
--- a/apps/web/lib/zod/schemas/commissions.ts
+++ b/apps/web/lib/zod/schemas/commissions.ts
@@ -179,6 +179,10 @@ export const CLAWBACK_REASONS = [
},
];
+export const CLAWBACK_REASONS_MAP = Object.fromEntries(
+ CLAWBACK_REASONS.map((r) => [r.value, r]),
+);
+
export const createClawbackSchema = z.object({
workspaceId: z.string(),
partnerId: z.string(),