The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add the Complete bounties task and earnings values in the partner filters (#4720)
Rafi84e3f896a672
diff --git a/apps/web/app/(ee)/partners.dub.co/(dashboard)/overview/tasks-card.tsx b/apps/web/app/(ee)/partners.dub.co/(dashboard)/overview/tasks-card.tsxindex 8a732b9dcc..ef85c4fa46 100644--- a/apps/web/app/(ee)/partners.dub.co/(dashboard)/overview/tasks-card.tsx+++ b/apps/web/app/(ee)/partners.dub.co/(dashboard)/overview/tasks-card.tsx@@ -2,9 +2,10 @@import { testIds } from "@/lib/e2e/test-ids";import { useProgramMessagesCount } from "@/lib/messages/hooks/use-program-messages-count";+import { usePartnerProfileBounties } from "@/lib/swr/use-partner-profile-bounties";import useProgramEnrollmentsCount from "@/lib/swr/use-program-enrollments-count";import { useMediaQuery, type Icon } from "@dub/ui";-import { Msgs, UserCheck } from "@dub/ui/icons";+import { Msgs, Trophy, UserCheck } from "@dub/ui/icons";import { cn, nFormatter } from "@dub/utils";import Link from "next/link";import { OverviewCard } from "./overview-card";@@ -20,6 +21,9 @@ export function TasksCard({ className }: { className?: string }) {status: "invited",});+ // the same request as the Bounties badge in the sidebar+ const { bountiesCount } = usePartnerProfileBounties();+const tasks = [{icon: Msgs,@@ -33,6 +37,12 @@ export function TasksCard({ className }: { className?: string }) {href: "/programs?tab=invitations",count: invitationsCount ?? 0,},+ {+ icon: Trophy,+ label: "Complete bounties",+ href: "/bounties",+ count: bountiesCount.active,+ },];// only on mobile: hide the card when no action is neededdiff --git a/apps/web/lib/api/partner-profile/get-partner-earnings-count.ts b/apps/web/lib/api/partner-profile/get-partner-earnings-count.tsindex adf1bffd88..95f1053f6e 100644--- a/apps/web/lib/api/partner-profile/get-partner-earnings-count.ts+++ b/apps/web/lib/api/partner-profile/get-partner-earnings-count.ts@@ -67,6 +67,7 @@ export async function getPartnerEarningsCount({return { count };}+ // each group has its count and its total earnings, highest earnings firstconst groupedWhere: Prisma.CommissionWhereInput = {...where,...(type && groupBy !== "type" && { type }),@@ -85,9 +86,12 @@ export async function getPartnerEarningsCount({customerId: { not: null },},_count: true,+ _sum: {+ earnings: true,+ },orderBy: {- _count: {- customerId: "desc",+ _sum: {+ earnings: "desc",},},});@@ -129,7 +133,7 @@ export async function getPartnerEarningsCount({]),);- return counts.map(({ customerId, programId, _count }) => {+ return counts.map(({ customerId, programId, _count, _sum }) => {const customer = customerId ? customersById.get(customerId) : undefined;const customerDataSharingEnabledAt =dataSharingByProgramId.get(programId);@@ -142,6 +146,7 @@ export async function getPartnerEarningsCount({: obfuscateCustomerEmail(customer.email): customer?.name || generateRandomName(),_count,+ earnings: _sum.earnings ?? 0,};});}@@ -154,9 +159,12 @@ export async function getPartnerEarningsCount({...(groupBy === "linkId" && { linkId: { not: null } }),},_count: true,+ _sum: {+ earnings: true,+ },orderBy: {- _count: {- [groupBy]: "desc",+ _sum: {+ earnings: "desc",},},});@@ -180,7 +188,7 @@ export async function getPartnerEarningsCount({const linksById = new Map(links.map((l) => [l.id, l]));- return counts.flatMap(({ linkId, _count }) => {+ return counts.flatMap(({ linkId, _count, _sum }) => {if (!linkId) return [];const link = linksById.get(linkId);if (!link) return [];@@ -191,6 +199,7 @@ export async function getPartnerEarningsCount({key: link.key,url: link.url,_count,+ earnings: _sum.earnings ?? 0,},];});@@ -208,7 +217,7 @@ export async function getPartnerEarningsCount({const programsById = new Map(programs.map((p) => [p.id, p]));- return counts.map(({ programId, _count }) => {+ return counts.map(({ programId, _count, _sum }) => {const program = programsById.get(programId);return {id: programId,@@ -216,9 +225,13 @@ export async function getPartnerEarningsCount({slug: program?.slug,logo: program?.logo,_count,+ earnings: _sum.earnings ?? 0,};});}- return counts;+ return counts.map(({ _sum, ...group }) => ({+ ...group,+ earnings: _sum.earnings ?? 0,+ }));}diff --git a/apps/web/playwright/api/partner-profile/earnings.spec.ts b/apps/web/playwright/api/partner-profile/earnings.spec.tsindex 2bb87f672c..15c3034171 100644--- a/apps/web/playwright/api/partner-profile/earnings.spec.ts+++ b/apps/web/playwright/api/partner-profile/earnings.spec.ts@@ -234,6 +234,7 @@ test.describe("GET /partner-profile/earnings", () => {domain: expect.any(String),key: expect.any(String),_count: 1,+ earnings: 4000,}),]);@@ -246,6 +247,7 @@ test.describe("GET /partner-profile/earnings", () => {id: programB.customerId,email: programB.customerEmail,_count: 1,+ earnings: 4000,}),]);});@@ -257,25 +259,28 @@ test.describe("GET /partner-profile/earnings", () => {}) => {await withPartnerApi(playwright, async (partnerApi) => {const all = await partnerApi.get<- { id: string; slug: string; _count: number }[]+ { id: string; slug: string; _count: number; earnings: number }[]>("/api/partner-profile/earnings/count?groupBy=programId");+ // highest earnings firstexpect(all.status).toEqual(200);expect(all.data).toEqual([- expect.objectContaining({- id: program.id,- slug: programSlug,- _count: 2,- }),expect.objectContaining({id: programB.id,slug: programB.slug,_count: 1,+ earnings: 4000,+ }),+ expect.objectContaining({+ id: program.id,+ slug: programSlug,+ _count: 2,+ earnings: 3500,}),]);const selected = await partnerApi.get<- { id: string; slug: string; _count: number }[]+ { id: string; slug: string; _count: number; earnings: number }[]>(`/api/partner-profile/earnings/count?groupBy=programId&programIdOrSlug=${programB.slug}`,);@@ -286,11 +291,36 @@ test.describe("GET /partner-profile/earnings", () => {id: programB.id,slug: programB.slug,_count: 1,+ earnings: 4000,}),]);});});+ test("sums earnings grouped by commission type and by status", async ({+ playwright,+ }) => {+ await withPartnerApi(playwright, async (partnerApi) => {+ expect(+ await partnerApi.get(+ "/api/partner-profile/earnings/count?groupBy=type",+ ),+ ).toMatchObject({+ status: 200,+ data: [{ type: "custom", _count: 3, earnings: 7500 }],+ });++ expect(+ await partnerApi.get(+ "/api/partner-profile/earnings/count?groupBy=status",+ ),+ ).toMatchObject({+ status: 200,+ data: [{ status: "pending", _count: 3, earnings: 7500 }],+ });+ });+ });+test("filters by program ID or slug", async ({ playwright, program }) => {await withPartnerApi(playwright, async (partnerApi) => {for (const programIdOrSlug of [program.id, programSlug]) {diff --git a/apps/web/playwright/partners/overview.spec.ts b/apps/web/playwright/partners/overview.spec.tsindex aec5879512..368ff8607a 100644--- a/apps/web/playwright/partners/overview.spec.ts+++ b/apps/web/playwright/partners/overview.spec.ts@@ -109,6 +109,12 @@ test.describe("Partner All programs Overview", () => {},});+ // other tests create bounties in the shared test program, so the bounties are mocked here+ let bounties: object[] = [];+ await page.route("**/api/partner-profile/bounties", (route) =>+ route.fulfill({ json: bounties }),+ );+await logIn(page, email, password);await page.goto("/overview");@@ -119,10 +125,15 @@ test.describe("Partner All programs Overview", () => {const messagesTask = tasks.getByRole("link", {name: /Respond to programs/,});+ const bountiesTask = tasks.getByRole("link", {+ name: /Complete bounties/,+ });await expect(tasks).toBeVisible();await expect(invitationsTask).toHaveText(/^Review new invitations\s*1$/);await expect(messagesTask).toHaveText(/^Respond to programs\s*0$/);+ await expect(bountiesTask).toHaveText(/^Complete bounties\s*0$/);+ await expect(bountiesTask).toHaveAttribute("href", "/bounties");await page.setViewportSize(MOBILE_VIEWPORT);await expect(tasks).toBeVisible();@@ -153,6 +164,9 @@ test.describe("Partner All programs Overview", () => {page.waitForResponse((res) =>res.url().includes("/api/partner-profile/messages/count?unread=true"),),+ page.waitForResponse((res) =>+ res.url().endsWith("/api/partner-profile/bounties"),+ ),]);await page.reload();await countsLoaded;@@ -166,6 +180,20 @@ test.describe("Partner All programs Overview", () => {await expect(tasks).toBeVisible();await expect(invitationsTask).toHaveText(/^Review new invitations\s*0$/);await expect(messagesTask).toHaveText(/^Respond to programs\s*0$/);+ await expect(bountiesTask).toHaveText(/^Complete bounties\s*0$/);++ // an active bounty is an action, so mobile shows the card again+ bounties = [+ {+ type: "submission",+ submissions: [],+ maxSubmissions: 1,+ endsAt: null,+ },+ ];+ await page.setViewportSize(MOBILE_VIEWPORT);+ await page.reload();+ await expect(bountiesTask).toHaveText(/^Complete bounties\s*1$/);} finally {if (userId) await prisma.user.delete({ where: { id: userId } });await deletePartner(partnerId);diff --git a/apps/web/ui/partners/earnings/earnings-composite-chart.tsx b/apps/web/ui/partners/earnings/earnings-composite-chart.tsxindex 6b4f7a6301..e9efa0e067 100644--- a/apps/web/ui/partners/earnings/earnings-composite-chart.tsx+++ b/apps/web/ui/partners/earnings/earnings-composite-chart.tsx@@ -26,15 +26,9 @@ import {Sliders,User,} from "@dub/ui/icons";-import {- capitalize,- cn,- currencyFormatter,- linkConstructor,- nFormatter,-} from "@dub/utils";+import { capitalize, cn, currencyFormatter, linkConstructor } from "@dub/utils";import NumberFlow from "@number-flow/react";-import { CommissionType } from "@prisma/client";+import { CommissionStatus, CommissionType } from "@prisma/client";import { useParams } from "next/navigation";import { Fragment, useMemo, useState } from "react";import { usePartnerEarningsFilters } from "./use-partner-earnings-filters";@@ -362,7 +356,7 @@ function EarningsTableControls() {const [selectedFilter, setSelectedFilter] = useState<string | null>(null);const { data: programs } = usePartnerProfileEarningsCount<- { id: string; name?: string; logo?: string | null; _count: number }[]+ { id: string; name?: string; logo?: string | null; earnings: number }[]>({...earningsFilters,// the menu lists every program, also when one program is selected@@ -375,8 +369,16 @@ function EarningsTableControls() {: false),});+ const { data: types } = usePartnerProfileEarningsCount<+ { type: CommissionType; earnings: number }[]+ >({+ ...earningsFilters,+ groupBy: "type",+ enabled: selectedFilter === "type" || searchParamsObj.type ? true : false,+ });+const { data: links } = usePartnerProfileEarningsCount<- { id: string; domain: string; key: string; url: string; _count: number }[]+ { id: string; domain: string; key: string; url: string; earnings: number }[]>({...earningsFilters,groupBy: "linkId",@@ -385,18 +387,19 @@ function EarningsTableControls() {});const { data: customers } = usePartnerProfileEarningsCount<- { id: string; email: string; _count: number }[]+ { id: string; email: string; earnings: number }[]>({...earningsFilters,groupBy: "customerId",enabled:- selectedFilter === "customerId" || searchParamsObj.customerId+ !!programSlug &&+ (selectedFilter === "customerId" || searchParamsObj.customerId? true- : false,+ : false),});const { data: statuses } = usePartnerProfileEarningsCount<- { status: string; _count: number }[]+ { status: CommissionStatus; earnings: number }[]>({...earningsFilters,groupBy: "status",@@ -414,7 +417,7 @@ function EarningsTableControls() {icon: GridIcon,label: "Program",options:- programs?.map(({ id, name, logo, _count }) => ({+ programs?.map(({ id, name, logo, earnings }) => ({value: id,label: name ?? id,icon: (@@ -423,7 +426,7 @@ function EarningsTableControls() {className="size-4"/>),- right: nFormatter(_count, { full: true }),+ right: currencyFormatter(earnings),})) ?? null,},]),@@ -431,11 +434,13 @@ function EarningsTableControls() {key: "type",icon: Sliders,label: "Type",- options: Object.values(CommissionType).map((type) => ({- value: type,- label: capitalize(type) as string,- icon: <CommissionTypeIcon type={type} />,- })),+ options:+ types?.map(({ type, earnings }) => ({+ value: type,+ label: capitalize(type) as string,+ icon: <CommissionTypeIcon type={type} />,+ right: currencyFormatter(earnings),+ })) ?? null,},{key: "linkId",@@ -445,7 +450,7 @@ function EarningsTableControls() {return <LinkIcon url={props.option?.data?.url} />;},options:- links?.map(({ id, domain, key, url }) => ({+ links?.map(({ id, domain, key, url, earnings }) => ({value: id,label: linkConstructor({domain,@@ -453,24 +458,31 @@ function EarningsTableControls() {pretty: true,}),data: { url },+ right: currencyFormatter(earnings),})) ?? null,},- {- key: "customerId",- icon: User,- label: "Customer",- options:- customers?.map(({ id, email }) => ({- value: id,- label: email || id,- })) ?? null,- },+ // the Customer filter is only on the Earnings page of one program+ ...(programSlug+ ? [+ {+ key: "customerId",+ icon: User,+ label: "Customer",+ options:+ customers?.map(({ id, email, earnings }) => ({+ value: id,+ label: email || id,+ right: currencyFormatter(earnings),+ })) ?? null,+ },+ ]+ : []),{key: "status",icon: CircleDotted,label: "Status",options:- statuses?.map(({ status, _count }) => {+ statuses?.map(({ status, earnings }) => {const Icon = CommissionStatusBadges[status].icon;return {value: status,@@ -483,12 +495,12 @@ function EarningsTableControls() {)}/>),- right: nFormatter(_count, { full: true }),+ right: currencyFormatter(earnings),};}) ?? null,},],- [programSlug, programs, links, customers, statuses],+ [programSlug, programs, types, links, customers, statuses],);const activeFilters = useMemo(() => {@@ -500,7 +512,9 @@ function EarningsTableControls() {: []),...(type ? [{ key: "type", value: type }] : []),...(linkId ? [{ key: "linkId", value: linkId }] : []),- ...(customerId ? [{ key: "customerId", value: customerId }] : []),+ ...(customerId && programSlug+ ? [{ key: "customerId", value: customerId }]+ : []),...(status ? [{ key: "status", value: status }] : []),...(payoutId ? [{ key: "payoutId", value: payoutId }] : []),];diff --git a/apps/web/ui/partners/earnings/use-partner-earnings-filters.ts b/apps/web/ui/partners/earnings/use-partner-earnings-filters.tsindex 2a11cb2dcf..11a26d3449 100644--- a/apps/web/ui/partners/earnings/use-partner-earnings-filters.ts+++ b/apps/web/ui/partners/earnings/use-partner-earnings-filters.ts@@ -25,7 +25,8 @@ export function usePartnerEarningsFilters() {programIdOrSlug: programSlug ?? programId,type,linkId,- customerId,+ // the All programs page has no Customer filter+ customerId: programSlug ? customerId : undefined,status,payoutId,}),