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 `ProgramRewardList` to program owner overview

Tim Wilson7564dc33b16c
diff --git a/apps/web/app/api/programs/[programId]/route.ts b/apps/web/app/api/programs/[programId]/route.ts
index 9f1d857c57..929bb45c5b 100644
--- a/apps/web/app/api/programs/[programId]/route.ts
+++ b/apps/web/app/api/programs/[programId]/route.ts
@@ -12,6 +12,7 @@ export const GET = withWorkspace(async ({ workspace, params }) => {
{
includeDefaultDiscount: true,
includeDefaultReward: true,
+ includeRewards: true,
},
);
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/page-client.tsx
index 5e5d0d00cc..c4062c3aca 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/page-client.tsx
@@ -1,7 +1,7 @@
"use client";
import useProgram from "@/lib/swr/use-program";
-import { ProgramRewardDescription } from "@/ui/partners/program-reward-description";
+import { ProgramRewardList } from "@/ui/partners/program-reward-list";
import { buttonVariants, Grid, useRouterStuff } from "@dub/ui";
import { cn } from "@dub/utils";
import Link from "next/link";
@@ -45,16 +45,18 @@ export default function ProgramOverviewPageClient() {
<div className="relative flex grow flex-col justify-end">
<div className="relative p-5 pt-10">
<div className="absolute inset-0 bg-neutral-800 [mask-image:linear-gradient(to_bottom,transparent,black_30%)]" />
- <p className="relative text-xl text-white">
- {program.rewards?.[0] && (
- <ProgramRewardDescription
- reward={program.rewards?.[0]}
+ {program.rewards?.[0] && (
+ <div className="relative">
+ <h4 className="text-sm font-semibold text-neutral-200">
+ Rewards
+ </h4>
+ <ProgramRewardList
+ rewards={program.rewards}
discount={program.discounts?.[0]}
- amountClassName="text-blue-400 font-medium"
- periodClassName="text-white font-medium"
+ className="mt-2 border-neutral-600 bg-neutral-700 text-neutral-300 [&_li>svg]:text-neutral-50"
/>
- )}
- </p>
+ </div>
+ )}
</div>
</div>
</div>
diff --git a/apps/web/lib/api/programs/get-program-or-throw.ts b/apps/web/lib/api/programs/get-program-or-throw.ts
index b2dc9c0129..261cc43c3b 100644
--- a/apps/web/lib/api/programs/get-program-or-throw.ts
+++ b/apps/web/lib/api/programs/get-program-or-throw.ts
@@ -1,8 +1,8 @@
-import { DiscountProps, ProgramProps, RewardProps } from "@/lib/types";
+import { getTopProgramRewards } from "@/lib/partners/get-top-program-rewards";
+import { DiscountProps, ProgramProps } from "@/lib/types";
import { ProgramSchema } from "@/lib/zod/schemas/programs";
import { prisma } from "@dub/prisma";
import { DubApiError } from "../errors";
-import { getRewardOrThrow } from "../partners/get-reward-or-throw";
export const getProgramOrThrow = async (
{
@@ -15,9 +15,11 @@ export const getProgramOrThrow = async (
{
includeDefaultDiscount = false,
includeDefaultReward = false,
+ includeRewards = false,
}: {
includeDefaultDiscount?: boolean;
includeDefaultReward?: boolean;
+ includeRewards?: boolean;
} = {},
) => {
const program = (await prisma.program.findUnique({
@@ -26,18 +28,25 @@ export const getProgramOrThrow = async (
workspaceId,
},
- ...(includeDefaultDiscount
- ? {
- include: {
- defaultDiscount: true,
+ include: {
+ ...(includeDefaultDiscount && {
+ defaultDiscount: true,
+ }),
+ ...(includeDefaultReward && {
+ defaultReward: true,
+ }),
+ ...(includeRewards && {
+ rewards: {
+ where: {
+ partners: {
+ none: {}, // program-wide rewards only
+ },
},
- }
- : {}),
+ },
+ }),
+ },
})) as (ProgramProps & { defaultDiscount: DiscountProps | null }) | null;
- // TODO:
- // Add a new relation (defaultReward) to fetch the default reward
-
if (!program) {
throw new DubApiError({
code: "not_found",
@@ -45,19 +54,10 @@ export const getProgramOrThrow = async (
});
}
- let defaultReward: RewardProps | null = null;
-
- if (includeDefaultReward && program.defaultRewardId) {
- defaultReward = await getRewardOrThrow({
- rewardId: program.defaultRewardId,
- programId,
- });
- }
-
return ProgramSchema.parse({
...program,
- ...(includeDefaultReward && defaultReward
- ? { rewards: [defaultReward] }
+ ...(includeRewards && program.rewards?.length
+ ? { rewards: getTopProgramRewards(program.rewards as any) }
: {}),
...(includeDefaultDiscount && program.defaultDiscount
? { discounts: [program.defaultDiscount] }
diff --git a/apps/web/lib/fetchers/get-program.ts b/apps/web/lib/fetchers/get-program.ts
index 7a1c64c8d3..4ed2b53146 100644
--- a/apps/web/lib/fetchers/get-program.ts
+++ b/apps/web/lib/fetchers/get-program.ts
@@ -1,5 +1,6 @@
import { prisma } from "@dub/prisma";
import { cache } from "react";
+import { getTopProgramRewards } from "../partners/get-top-program-rewards";
export const getProgram = cache(
async ({
@@ -29,13 +30,8 @@ export const getProgram = cache(
},
});
- if (program && include?.includes("rewards")) {
- program.rewards = ["click", "lead", "sale"]
- .map((event) =>
- program?.rewards.find((reward) => reward.event === event),
- )
- .filter((reward) => reward !== undefined && reward !== null);
- }
+ if (program && include?.includes("rewards"))
+ program.rewards = getTopProgramRewards(program.rewards);
return program;
},
diff --git a/apps/web/lib/partners/get-top-program-rewards.ts b/apps/web/lib/partners/get-top-program-rewards.ts
new file mode 100644
index 0000000000..31210f4952
--- /dev/null
+++ b/apps/web/lib/partners/get-top-program-rewards.ts
@@ -0,0 +1,10 @@
+import { Reward } from "@prisma/client";
+
+/**
+ * Gets the top rewards for a program in order (click, lead, sale)
+ */
+export function getTopProgramRewards(rewards: Reward[]) {
+ return ["click", "lead", "sale"]
+ .map((event) => rewards.find((reward) => reward.event === event))
+ .filter((reward) => reward !== undefined && reward !== null);
+}
diff --git a/apps/web/ui/partners/program-reward-list.tsx b/apps/web/ui/partners/program-reward-list.tsx
index 67b08c0f24..e8eb95018c 100644
--- a/apps/web/ui/partners/program-reward-list.tsx
+++ b/apps/web/ui/partners/program-reward-list.tsx
@@ -19,7 +19,7 @@ export function ProgramRewardList({
return (
<ul
className={cn(
- "flex flex-col gap-4 rounded-md border border-neutral-200 bg-white p-4",
+ "flex flex-col gap-4 rounded-md border border-neutral-200 bg-white p-4 text-neutral-600",
className,
)}
>
@@ -61,8 +61,8 @@ export function ProgramRewardList({
const Item = ({ icon: Icon, children }: PropsWithChildren<{ icon: Icon }>) => {
return (
- <li className="flex items-center gap-2 text-sm text-neutral-600">
- <Icon className="size-4" />
+ <li className="flex items-start gap-2 text-sm leading-tight">
+ <Icon className="size-4 shrink-0 translate-y-px" />
<div>{children}</div>
</li>
);
diff --git a/packages/prisma/schema/program.prisma b/packages/prisma/schema/program.prisma
index 1bfc976a2f..1372f1e66f 100644
--- a/packages/prisma/schema/program.prisma
+++ b/packages/prisma/schema/program.prisma
@@ -58,6 +58,7 @@ model Program {
links Link[]
commissions Commission[]
rewards Reward[]
+ defaultReward Reward? @relation("ProgramDefaultReward", fields: [defaultRewardId], references: [id], onDelete: NoAction, onUpdate: NoAction)
discounts Discount[] @relation("ProgramDiscounts")
defaultDiscount Discount? @relation("ProgramDefaultDiscount", fields: [defaultDiscountId], references: [id], onDelete: NoAction, onUpdate: NoAction)
diff --git a/packages/prisma/schema/reward.prisma b/packages/prisma/schema/reward.prisma
index a7d891171d..7f8a22e84d 100644
--- a/packages/prisma/schema/reward.prisma
+++ b/packages/prisma/schema/reward.prisma
@@ -11,8 +11,9 @@ model Reward {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
- program Program @relation(fields: [programId], references: [id], onDelete: Cascade)
- partners PartnerReward[]
+ defaultForProgram Program? @relation("ProgramDefaultReward")
+ program Program @relation(fields: [programId], references: [id], onDelete: Cascade)
+ partners PartnerReward[]
@@index(programId)
@@index(event) // used in /api/cron/aggregate-clicks