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.

display the reward with partners count

Kiran K4a1153369d38
diff --git a/apps/web/app/api/programs/[programId]/rewards/route.ts b/apps/web/app/api/programs/[programId]/rewards/route.ts
index 88dc48d48d..93662f268f 100644
--- a/apps/web/app/api/programs/[programId]/rewards/route.ts
+++ b/apps/web/app/api/programs/[programId]/rewards/route.ts
@@ -18,7 +18,21 @@ export const GET = withWorkspace(async ({ workspace, params }) => {
where: {
programId,
},
+ include: {
+ _count: {
+ select: {
+ partners: true,
+ },
+ },
+ },
});
- return NextResponse.json(z.array(rewardSchema).parse(rewards));
+ const rewardsWithPartnersCount = rewards.map((reward) => ({
+ ...reward,
+ partnersCount: reward._count.partners,
+ }));
+
+ return NextResponse.json(
+ z.array(rewardSchema).parse(rewardsWithPartnersCount),
+ );
});
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/settings/rewards/reward-settings.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/settings/rewards/reward-settings.tsx
index 806e0c3b7a..ef93d0b8b9 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/settings/rewards/reward-settings.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/settings/rewards/reward-settings.tsx
@@ -2,8 +2,9 @@
import useProgram from "@/lib/swr/use-program";
import useRewards from "@/lib/swr/use-rewards";
+import { Reward } from "@/lib/types";
import { useRewardSheet } from "@/ui/partners/add-edit-reward-sheet";
-import { EventType, Reward as RewardType } from "@dub/prisma/client";
+import { EventType } from "@dub/prisma/client";
import { Badge, Button, IconMenu, MoneyBill, Popover } from "@dub/ui";
import { BoltFill, CurrencyDollar, Users2 } from "@dub/ui/icons";
import { Gift } from "lucide-react";
@@ -107,7 +108,7 @@ const AdditionalRewards = () => {
);
};
-const Reward = ({ reward }: { reward: RewardType }) => {
+const Reward = ({ reward }: { reward: Reward }) => {
return (
<div className="flex items-center gap-4 rounded-lg border border-neutral-200 p-4">
<div className="flex size-10 items-center justify-center rounded-full border border-neutral-200 bg-white">
@@ -116,27 +117,51 @@ const Reward = ({ reward }: { reward: RewardType }) => {
<div className="flex flex-1 items-center justify-between">
<div className="flex items-center gap-2">
<span className="text-sm font-normal">
- Earn{" "}
- <span className="text-blue-500">
- {reward.type === "percentage"
- ? `${reward.amount}%`
- : `$${(reward.amount / 100).toFixed(2)}`}
- </span>{" "}
- for each {reward.event.toLowerCase()}
- {reward.maxDuration && (
- <>
- , and again for {reward.maxDuration}{" "}
- {reward.maxDuration === 0 ? "forever" : "months"}
- </>
- )}
+ <RewardDescription reward={reward} />
</span>
</div>
- <Badge variant="gray">All partners</Badge>
+ {reward.partnersCount > 0 ? (
+ <Badge variant="green">{reward.partnersCount} partners</Badge>
+ ) : (
+ <Badge variant="gray">All partners</Badge>
+ )}
</div>
</div>
);
};
+const RewardDescription = ({ reward }: { reward: Reward }) => {
+ const amount =
+ reward.type === "percentage"
+ ? `${reward.amount}%`
+ : `$${(reward.amount / 100).toFixed(2)}`;
+
+ const eventText = {
+ click: "click",
+ lead: "signup",
+ sale: "sale",
+ }[reward.event];
+
+ const getRecurringText = () => {
+ if (reward.maxDuration === null) {
+ return ", and again for every conversion of the customers lifetime";
+ }
+ if (reward.maxDuration === 0) {
+ return ""; // No recurring text for one-time rewards
+ }
+ return `, and again for every conversion of ${reward.maxDuration} months`;
+ };
+
+ return (
+ <>
+ Earn <span className="text-blue-500">{amount}</span> for each {eventText}
+ {reward.event === "sale" && (
+ <span className="text-neutral-900">{getRecurringText()}</span>
+ )}
+ </>
+ );
+};
+
const CreateRewardButton = () => {
const [openPopover, setOpenPopover] = useState(false);
const [selectedEvent, setSelectedEvent] = useState<EventType | null>(null);
diff --git a/apps/web/lib/actions/partners/create-reward.ts b/apps/web/lib/actions/partners/create-reward.ts
index 6ea10ee0e5..b806d55258 100644
--- a/apps/web/lib/actions/partners/create-reward.ts
+++ b/apps/web/lib/actions/partners/create-reward.ts
@@ -4,6 +4,7 @@ import { getProgramOrThrow } from "@/lib/api/programs/get-program-or-throw";
import { createId } from "@/lib/api/utils";
import { createRewardSchema } from "@/lib/zod/schemas/rewards";
import { prisma } from "@dub/prisma";
+import { EventType } from "@dub/prisma/client";
import { z } from "zod";
import { authActionClient } from "../safe-action";
@@ -46,15 +47,6 @@ export const createRewardAction = authActionClient
}
}
- const hasDefaultReward = !!program.defaultRewardId;
-
- // if (event === "sale") {
-
- // }
-
- // TODO:
- // Partners can't be more than one reward of the same type.
-
const reward = await prisma.reward.create({
data: {
id: createId({ prefix: "rew_" }),
@@ -75,7 +67,8 @@ export const createRewardAction = authActionClient
},
});
- if (!hasDefaultReward) {
+ // set the default reward if it doesn't exist
+ if (!program.defaultRewardId && event === EventType.sale) {
await prisma.program.update({
where: {
id: programId,
diff --git a/apps/web/lib/swr/use-rewards.ts b/apps/web/lib/swr/use-rewards.ts
index 93cf9fdf7c..0acf5933d6 100644
--- a/apps/web/lib/swr/use-rewards.ts
+++ b/apps/web/lib/swr/use-rewards.ts
@@ -1,8 +1,8 @@
-import { Reward } from "@dub/prisma/client";
import { fetcher } from "@dub/utils";
import { useParams } from "next/navigation";
import useSWR from "swr";
import useWorkspace from "./use-workspace";
+import { Reward } from "../types";
export default function useRewards() {
const { programId } = useParams();
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index 78866ea702..88298f836d 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -66,6 +66,7 @@ import {
webhookEventSchemaTB,
WebhookSchema,
} from "./zod/schemas/webhooks";
+import { rewardSchema } from "./zod/schemas/rewards";
export type LinkProps = Link;
@@ -429,3 +430,5 @@ export type FolderSummary = Pick<
Folder,
"id" | "name" | "accessLevel" | "linkCount"
>;
+
+export type Reward = z.infer<typeof rewardSchema>;
diff --git a/apps/web/lib/zod/schemas/rewards.ts b/apps/web/lib/zod/schemas/rewards.ts
index 93ebb15fba..d07d47e66a 100644
--- a/apps/web/lib/zod/schemas/rewards.ts
+++ b/apps/web/lib/zod/schemas/rewards.ts
@@ -17,6 +17,7 @@ export const rewardSchema = z.object({
type: z.nativeEnum(CommissionType),
amount: z.number(),
maxDuration: z.number().nullish(),
+ partnersCount: z.number(),
createdAt: z.date(),
updatedAt: z.date(),
});
@@ -30,22 +31,10 @@ export const createOrUpdateRewardSchema = z.object({
});
export const createRewardSchema = createOrUpdateRewardSchema.superRefine(
- (data, ctx) => {
+ (data) => {
if (data.event !== EventType.sale) {
- if (data.maxDuration) {
- ctx.addIssue({
- code: z.ZodIssueCode.custom,
- message: "Duration is only allowed for sale rewards.",
- path: ["maxDuration"],
- });
- }
-
- if (data.type !== "flat") {
- ctx.addIssue({
- code: z.ZodIssueCode.custom,
- message: `Type must be flat for ${data.event} rewards.`,
- });
- }
+ data.maxDuration = "0";
+ data.type = "flat";
}
},
);
diff --git a/packages/ui/src/badge.tsx b/packages/ui/src/badge.tsx
index 82a786bc1b..b1e1ae843c 100644
--- a/packages/ui/src/badge.tsx
+++ b/packages/ui/src/badge.tsx
@@ -9,6 +9,7 @@ const badgeVariants = cva(
default: "border-neutral-400 text-neutral-500",
violet: "border-violet-600 bg-violet-600 text-white",
blue: "border-blue-500 bg-blue-500 text-white",
+ green: "border-green-100 bg-green-100 text-green-900",
sky: "border-sky-900 bg-sky-900 text-white",
black: "border-black bg-black text-white",
gray: "border-neutral-200 bg-neutral-100 text-neutral-800",