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.

Support optional type and maxDuration in reward conditions

Kiran K4550829207f8
diff --git a/apps/web/lib/partners/determine-partner-reward.ts b/apps/web/lib/partners/determine-partner-reward.ts
index 328e3b0a19..370b1852ed 100644
--- a/apps/web/lib/partners/determine-partner-reward.ts
+++ b/apps/web/lib/partners/determine-partner-reward.ts
@@ -64,12 +64,18 @@ export const determinePartnerReward = async ({
partnerReward = {
...partnerReward,
amount: matchedCondition.amount,
- type: matchedCondition.type,
+ type: matchedCondition.type || partnerReward.type,
+ maxDuration:
+ matchedCondition.maxDuration !== undefined
+ ? matchedCondition.maxDuration
+ : partnerReward.maxDuration,
};
}
}
}
+ console.log(partnerReward);
+
if (partnerReward.amount === 0) {
return null;
}
diff --git a/apps/web/lib/partners/evaluate-reward-conditions.ts b/apps/web/lib/partners/evaluate-reward-conditions.ts
index 2a34663ebc..a38f9f7092 100644
--- a/apps/web/lib/partners/evaluate-reward-conditions.ts
+++ b/apps/web/lib/partners/evaluate-reward-conditions.ts
@@ -1,4 +1,3 @@
-import { Reward } from "@prisma/client";
import {
RewardCondition,
RewardConditionsArray,
@@ -11,7 +10,7 @@ export const evaluateRewardConditions = ({
}: {
conditions: RewardConditionsArray;
context: RewardContext;
-}): Pick<Reward, "amount" | "type"> | null => {
+}) => {
if (!conditions || !context) {
return null;
}
@@ -50,6 +49,7 @@ export const evaluateRewardConditions = ({
return {
amount: conditionGroup.amount,
type: conditionGroup.type,
+ maxDuration: conditionGroup.maxDuration,
};
}
}
diff --git a/apps/web/lib/zod/schemas/rewards.ts b/apps/web/lib/zod/schemas/rewards.ts
index 00613ec4ed..efc3f8b2b6 100644
--- a/apps/web/lib/zod/schemas/rewards.ts
+++ b/apps/web/lib/zod/schemas/rewards.ts
@@ -58,7 +58,8 @@ export const rewardConditionsSchema = z.object({
operator: z.enum(["AND", "OR"]).default("AND"),
conditions: z.array(rewardConditionSchema).min(1),
amount: z.number().int().min(0),
- type: z.nativeEnum(RewardStructure),
+ type: z.nativeEnum(RewardStructure).optional(),
+ maxDuration: maxDurationSchema,
});
export const rewardConditionsArraySchema = z
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 94477fa45e..1e4ccdb070 100644
--- a/apps/web/ui/partners/rewards/add-edit-reward-sheet.tsx
+++ b/apps/web/ui/partners/rewards/add-edit-reward-sheet.tsx
@@ -104,6 +104,7 @@ function RewardSheetContent({
modifiers: defaultValuesSource?.modifiers?.map((m) => ({
...m,
amount: m.type === "flat" ? m.amount / 100 : m.amount,
+ maxDuration: m.maxDuration === null ? Infinity : m.maxDuration,
})),
},
});
@@ -180,16 +181,17 @@ function RewardSheetContent({
}
let modifiers: RewardConditionsArray | null = null;
+
if (data.modifiers?.length) {
try {
modifiers = rewardConditionsArraySchema.parse(
data.modifiers.map((m) => ({
...m,
amount: m.type === "flat" ? m.amount * 100 : m.amount,
+ maxDuration: m.maxDuration === Infinity ? null : m.maxDuration,
})),
);
} catch (error) {
- console.error(error);
setError("root.logic", { message: "Invalid reward condition" });
toast.error(
"Invalid reward condition. Please fix the errors and try again.",
diff --git a/apps/web/ui/partners/rewards/rewards-logic.tsx b/apps/web/ui/partners/rewards/rewards-logic.tsx
index a4aa81aadd..ee33765cb4 100644
--- a/apps/web/ui/partners/rewards/rewards-logic.tsx
+++ b/apps/web/ui/partners/rewards/rewards-logic.tsx
@@ -4,6 +4,7 @@ import { constructRewardAmount } from "@/lib/api/sales/construct-reward-amount";
import { handleMoneyInputChange, handleMoneyKeyDown } from "@/lib/form-utils";
import { getPlanCapabilities } from "@/lib/plan-capabilities";
import useWorkspace from "@/lib/swr/use-workspace";
+import { RECURRING_MAX_DURATIONS } from "@/lib/zod/schemas/misc";
import {
CONDITION_ATTRIBUTES,
CONDITION_CUSTOMER_ATTRIBUTES,
@@ -104,6 +105,7 @@ export function RewardsLogic({
conditions: [{}],
amount: getValues("amount") || 0,
type: getValues("type"),
+ maxDuration: getValues("maxDuration"),
})
}
variant={isDefaultReward ? "primary" : "secondary"}
@@ -497,15 +499,23 @@ function ResultTerms({ modifierIndex }: { modifierIndex: number }) {
const modifierKey = `modifiers.${modifierIndex}` as const;
const { control, setValue } = useAddEditRewardForm();
- const [amount, type, maxDuration, event] = useWatch({
- control,
- name: [
- `${modifierKey}.amount`,
- `${modifierKey}.type`,
- "maxDuration",
- "event",
- ],
- });
+ const [amount, type, maxDuration, event, parentType, parentMaxDuration] =
+ useWatch({
+ control,
+ name: [
+ `${modifierKey}.amount`,
+ `${modifierKey}.type`,
+ `${modifierKey}.maxDuration`,
+ "event",
+ "type",
+ "maxDuration",
+ ],
+ });
+
+ // Use parent values as fallbacks if modifier doesn't have type or maxDuration
+ const displayType = type || parentType;
+ const displayMaxDuration =
+ maxDuration !== undefined ? maxDuration : parentMaxDuration;
return (
<span className="leading-relaxed">
@@ -513,7 +523,7 @@ function ResultTerms({ modifierIndex }: { modifierIndex: number }) {
{event === "sale" && (
<>
a{" "}
- <InlineBadgePopover text={capitalize(type)}>
+ <InlineBadgePopover text={capitalize(displayType)}>
<InlineBadgePopoverMenu
selectedValue={type}
onSelect={(value) =>
@@ -524,15 +534,15 @@ function ResultTerms({ modifierIndex }: { modifierIndex: number }) {
items={REWARD_TYPES}
/>
</InlineBadgePopover>{" "}
- {type === "percentage" && "of "}
+ {displayType === "percentage" && "of "}
</>
)}
<InlineBadgePopover
text={
amount
? constructRewardAmount({
- amount: type === "flat" ? amount * 100 : amount,
- type,
+ amount: displayType === "flat" ? amount * 100 : amount,
+ type: displayType || "flat",
})
: "amount"
}
@@ -544,11 +554,46 @@ function ResultTerms({ modifierIndex }: { modifierIndex: number }) {
{event === "sale" && (
<>
{" "}
- {maxDuration === 0
- ? "one time"
- : maxDuration === Infinity
- ? "for the customer's lifetime"
- : `for ${maxDuration} ${pluralize("month", Number(maxDuration))}`}
+ <InlineBadgePopover
+ text={
+ displayMaxDuration === 0
+ ? "one time"
+ : displayMaxDuration === Infinity
+ ? "for the customer's lifetime"
+ : `for ${displayMaxDuration} ${pluralize("month", Number(displayMaxDuration))}`
+ }
+ >
+ <InlineBadgePopoverMenu
+ selectedValue={
+ displayMaxDuration === Infinity
+ ? "Infinity"
+ : displayMaxDuration?.toString()
+ }
+ onSelect={(value) =>
+ setValue(
+ `${modifierKey}.maxDuration`,
+ value === "Infinity" ? Infinity : Number(value),
+ {
+ shouldDirty: true,
+ },
+ )
+ }
+ items={[
+ {
+ text: "one time",
+ value: "0",
+ },
+ ...RECURRING_MAX_DURATIONS.filter((v) => v !== 0).map((v) => ({
+ text: `for ${v} ${pluralize("month", Number(v))}`,
+ value: v.toString(),
+ })),
+ {
+ text: "for the customer's lifetime",
+ value: "Infinity",
+ },
+ ]}
+ />
+ </InlineBadgePopover>
</>
)}
</span>