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 constructRewardAmount to accept a reward object, simplifying reward calculations across various components.

Kiran K638e7aca503a
diff --git a/apps/web/app/(ee)/app.dub.co/embed/referrals/faq.tsx b/apps/web/app/(ee)/app.dub.co/embed/referrals/faq.tsx
index 6148d92d80..781d506545 100644
--- a/apps/web/app/(ee)/app.dub.co/embed/referrals/faq.tsx
+++ b/apps/web/app/(ee)/app.dub.co/embed/referrals/faq.tsx
@@ -20,17 +20,7 @@ export function ReferralsEmbedFAQ({
reward: RewardProps | null;
}) {
const rewardDescription = reward
- ? `For each new customer you refer, you'll earn a ${constructRewardAmount({
- ...(reward.modifiers
- ? {
- amounts: [
- reward.amount,
- ...reward.modifiers.map(({ amount }) => amount),
- ],
- }
- : { amount: reward.amount }),
- type: reward.type,
- })} commission on their subscription${
+ ? `For each new customer you refer, you'll earn a ${constructRewardAmount({ reward })} commission on their subscription${
reward.maxDuration === null
? " for their lifetime"
: reward.maxDuration && reward.maxDuration > 1
diff --git a/apps/web/app/api/og/program/route.tsx b/apps/web/app/api/og/program/route.tsx
index f92c1bf8b3..eead44f394 100644
--- a/apps/web/app/api/og/program/route.tsx
+++ b/apps/web/app/api/og/program/route.tsx
@@ -147,18 +147,8 @@ export async function GET(req: NextRequest) {
tw="font-semibold mr-1"
style={{ fontFamily: "Inter Semibold" }}
>
- {constructRewardAmount({
- ...(rewards[0].modifiers
- ? {
- amounts: [
- rewards[0].amount,
- ...rewards[0].modifiers.map(({ amount }) => amount),
- ],
- }
- : { amount: rewards[0].amount }),
- type: rewards[0].type,
- })}{" "}
- per {rewards[0].event}
+ {constructRewardAmount({ reward: rewards[0] })} per{" "}
+ {rewards[0].event}
</strong>
{rewards[0].maxDuration === null ? (
"for the customer's lifetime"
diff --git a/apps/web/lib/api/sales/construct-reward-amount.ts b/apps/web/lib/api/sales/construct-reward-amount.ts
index 1906cbfcb6..7d4928d8ea 100644
--- a/apps/web/lib/api/sales/construct-reward-amount.ts
+++ b/apps/web/lib/api/sales/construct-reward-amount.ts
@@ -1,28 +1,52 @@
-import { RewardStructure } from "@dub/prisma/client";
+import { RewardProps } from "@/lib/types";
+import { rewardConditionsArraySchema } from "@/lib/zod/schemas/rewards";
import { currencyFormatter } from "@dub/utils";
export const constructRewardAmount = ({
- type,
- amount: amountProp,
- amounts,
+ reward,
}: {
- type: RewardStructure;
-} & (
- | { amount: number; amounts?: never }
- | { amount?: never; amounts: number[] }
-)) => {
- // Range of amounts
- if (amounts && amounts.length > 1) {
- const min = Math.min(...amounts);
- const max = Math.max(...amounts);
- return type === "percentage"
- ? `${min}% - ${max}%`
- : `${formatCurrency(min)} - ${formatCurrency(max)}`;
+ reward: Pick<RewardProps, "amount" | "type" | "maxDuration" | "modifiers">;
+}) => {
+ // If there are modifiers, we need to check if they match the primary reward
+ if (reward.modifiers) {
+ const parsedModifiers = rewardConditionsArraySchema.safeParse(
+ reward.modifiers,
+ );
+
+ if (parsedModifiers.success) {
+ const modifiers = parsedModifiers.data;
+
+ // If the type AND maxDuration matches the primary, show a range
+ if (
+ modifiers.every(
+ (modifier) =>
+ modifier.type === reward.type &&
+ modifier.maxDuration === reward.maxDuration,
+ )
+ ) {
+ const min = Math.min(
+ reward.amount,
+ ...modifiers.map((modifier) => modifier.amount),
+ );
+
+ const max = Math.max(
+ reward.amount,
+ ...modifiers.map((modifier) => modifier.amount),
+ );
+
+ return reward.type === "percentage"
+ ? `${min}% - ${max}%`
+ : `${formatCurrency(min)} - ${formatCurrency(max)}`;
+ }
+ }
}
- // Single amount
- const amount = amountProp ?? amounts?.[0];
- return type === "percentage" ? `${amount}%` : formatCurrency(amount);
+ // Return the primary reward amount if
+ // 1. There are no modifiers OR
+ // 2. type AND timelines doesn't match the primary reward
+ return reward.type === "percentage"
+ ? `${reward.amount}%`
+ : formatCurrency(reward.amount);
};
const formatCurrency = (amount: number) =>
diff --git a/apps/web/ui/partners/format-discount-description.ts b/apps/web/ui/partners/format-discount-description.ts
index 0950942449..36db3fa365 100644
--- a/apps/web/ui/partners/format-discount-description.ts
+++ b/apps/web/ui/partners/format-discount-description.ts
@@ -14,7 +14,10 @@ export function formatDiscountDescription({
return discount.description;
}
- const discountAmount = constructRewardAmount(discount);
+ const discountAmount = constructRewardAmount({
+ reward: discount,
+ });
+
const parts: string[] = [];
parts.push(`New users get ${discountAmount} off `);
diff --git a/apps/web/ui/partners/format-reward-description.ts b/apps/web/ui/partners/format-reward-description.ts
index e3d41b7e76..d8c88d1f8f 100644
--- a/apps/web/ui/partners/format-reward-description.ts
+++ b/apps/web/ui/partners/format-reward-description.ts
@@ -13,7 +13,7 @@ export function formatRewardDescription({
return reward.description;
}
- const rewardAmount = constructRewardAmount(reward);
+ const rewardAmount = constructRewardAmount({ reward });
const parts: string[] = [];
parts.push("Earn");
diff --git a/apps/web/ui/partners/program-reward-description.tsx b/apps/web/ui/partners/program-reward-description.tsx
index 6a9ddc1c57..6d35a45453 100644
--- a/apps/web/ui/partners/program-reward-description.tsx
+++ b/apps/web/ui/partners/program-reward-description.tsx
@@ -27,15 +27,7 @@ export function ProgramRewardDescription({
Earn{" "}
<strong className={cn("font-semibold", amountClassName)}>
{constructRewardAmount({
- ...(reward.modifiers?.length
- ? {
- amounts: [
- reward.amount,
- ...reward.modifiers.map(({ amount }) => amount),
- ],
- }
- : { amount: reward.amount }),
- type: reward.type,
+ reward,
})}{" "}
</strong>
{reward.event === "sale" && reward.maxDuration === 0 ? (
@@ -79,8 +71,7 @@ export function ProgramRewardDescription({
New users get{" "}
<strong className={cn("font-semibold", amountClassName)}>
{constructRewardAmount({
- amount: discount.amount,
- type: discount.type,
+ reward: discount,
})}
</strong>{" "}
off{" "}
diff --git a/apps/web/ui/partners/program-reward-list.tsx b/apps/web/ui/partners/program-reward-list.tsx
index ee117c6783..5cd03d3cc8 100644
--- a/apps/web/ui/partners/program-reward-list.tsx
+++ b/apps/web/ui/partners/program-reward-list.tsx
@@ -31,15 +31,7 @@ export function ProgramRewardList({
{reward.description || (
<>
{constructRewardAmount({
- ...(reward.modifiers
- ? {
- amounts: [
- reward.amount,
- ...reward.modifiers.map(({ amount }) => amount),
- ],
- }
- : { amount: reward.amount }),
- type: reward.type,
+ reward,
})}{" "}
{reward.event === "sale" && reward.maxDuration === 0 ? (
<>for the first sale</>
@@ -82,8 +74,7 @@ export function ProgramRewardList({
{" "}
New users get{" "}
{constructRewardAmount({
- amount: discount.amount,
- type: discount.type,
+ reward: discount,
})}{" "}
off{" "}
{discount.maxDuration === null ? (
diff --git a/apps/web/ui/partners/program-reward-modifiers-tooltip.tsx b/apps/web/ui/partners/program-reward-modifiers-tooltip.tsx
index 874371a9e2..25c6b96d2e 100644
--- a/apps/web/ui/partners/program-reward-modifiers-tooltip.tsx
+++ b/apps/web/ui/partners/program-reward-modifiers-tooltip.tsx
@@ -6,6 +6,7 @@ import {
} from "@/lib/zod/schemas/rewards";
import { ArrowTurnRight2, InfoTooltip } from "@dub/ui";
import { capitalize, cn, pluralize, truncate } from "@dub/utils";
+import { RewardStructure } from "@prisma/client";
import { Fragment } from "react";
import { z } from "zod";
@@ -31,8 +32,7 @@ export function ProgramRewardModifiersTooltip({
<strong className="font-semibold">
<span className="text-content-default">
{constructRewardAmount({
- amount: reward.amount,
- type: reward.type,
+ reward,
})}
</span>{" "}
per {reward.event}
@@ -51,39 +51,45 @@ export function ProgramRewardModifiersTooltip({
{(
reward.modifiers as z.infer<typeof rewardConditionsArraySchema>
- ).map(({ amount, type, operator, conditions }, idx) => (
- <Fragment key={idx}>
- <div className="mt-1 flex items-start gap-1.5">
- <ArrowTurnRight2 className="mt-0.5 size-3 shrink-0" />
- <div className="min-w-0">
- <strong className="text-content-default font-semibold">
- {constructRewardAmount({
- amount,
- type,
- })}
- </strong>
- <ul className="overflow-hidden pl-1 text-xs text-neutral-600">
- {conditions.map((condition, idx) => (
- <li key={idx} className="flex items-center gap-1">
- <span className="shrink-0 text-lg leading-none">
- &bull;
- </span>
- <span className="min-w-0 truncate">
- {idx === 0
- ? "If"
- : capitalize(operator.toLowerCase())}
- {` ${condition.entity}`}
- {` ${condition.attribute}`}
- {` ${CONDITION_OPERATOR_LABELS[condition.operator]}`}
- {` ${condition.value && truncate(Array.isArray(condition.value) ? condition.value.join(", ") : condition.value.toString(), 16)}`}
- </span>
- </li>
- ))}
- </ul>
+ ).map(
+ ({ amount, type, operator, conditions, maxDuration }, idx) => (
+ <Fragment key={idx}>
+ <div className="mt-1 flex items-start gap-1.5">
+ <ArrowTurnRight2 className="mt-0.5 size-3 shrink-0" />
+ <div className="min-w-0">
+ <strong className="text-content-default font-semibold">
+ {constructRewardAmount({
+ reward: {
+ amount,
+ type: type as RewardStructure,
+ maxDuration,
+ modifiers: undefined,
+ },
+ })}
+ </strong>
+ <ul className="overflow-hidden pl-1 text-xs text-neutral-600">
+ {conditions.map((condition, idx) => (
+ <li key={idx} className="flex items-center gap-1">
+ <span className="shrink-0 text-lg leading-none">
+ &bull;
+ </span>
+ <span className="min-w-0 truncate">
+ {idx === 0
+ ? "If"
+ : capitalize(operator.toLowerCase())}
+ {` ${condition.entity}`}
+ {` ${condition.attribute}`}
+ {` ${CONDITION_OPERATOR_LABELS[condition.operator]}`}
+ {` ${condition.value && truncate(Array.isArray(condition.value) ? condition.value.join(", ") : condition.value.toString(), 16)}`}
+ </span>
+ </li>
+ ))}
+ </ul>
+ </div>
</div>
- </div>
- </Fragment>
- ))}
+ </Fragment>
+ ),
+ )}
</div>
}
/>
diff --git a/apps/web/ui/partners/rewards/add-edit-reward-sheet.tsx b/apps/web/ui/partners/rewards/add-edit-reward-sheet.tsx
index 1e4ccdb070..352843124a 100644
--- a/apps/web/ui/partners/rewards/add-edit-reward-sheet.tsx
+++ b/apps/web/ui/partners/rewards/add-edit-reward-sheet.tsx
@@ -293,8 +293,12 @@ function RewardSheetContent({
text={
amount
? constructRewardAmount({
- amount: type === "flat" ? amount * 100 : amount,
- type,
+ reward: {
+ amount,
+ type,
+ maxDuration,
+ modifiers,
+ },
})
: "amount"
}
diff --git a/apps/web/ui/partners/rewards/rewards-logic.tsx b/apps/web/ui/partners/rewards/rewards-logic.tsx
index ce143873d2..98b4e5448d 100644
--- a/apps/web/ui/partners/rewards/rewards-logic.tsx
+++ b/apps/web/ui/partners/rewards/rewards-logic.tsx
@@ -565,8 +565,12 @@ function ResultTerms({ modifierIndex }: { modifierIndex: number }) {
text={
amount
? constructRewardAmount({
- amount: displayType === "flat" ? amount * 100 : amount,
- type: displayType || "flat",
+ reward: {
+ amount,
+ type: displayType,
+ maxDuration,
+ modifiers: undefined,
+ },
})
: "amount"
}