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 mix & match flat fee and recurring commissions for rewards

Kiran K649fdf783e27
diff --git a/apps/web/lib/partners/determine-partner-reward.ts b/apps/web/lib/partners/determine-partner-reward.ts
index 79f414b2f0..328e3b0a19 100644
--- a/apps/web/lib/partners/determine-partner-reward.ts
+++ b/apps/web/lib/partners/determine-partner-reward.ts
@@ -42,7 +42,7 @@ export const determinePartnerReward = async ({
return null;
}
- const partnerReward = partnerEnrollment[rewardIdColumn];
+ let partnerReward = partnerEnrollment[rewardIdColumn];
if (!partnerReward) {
return null;
@@ -55,18 +55,17 @@ export const determinePartnerReward = async ({
// Parse the conditions before evaluating them
if (modifiers.success) {
- const matchingCondition = evaluateRewardConditions({
+ const matchedCondition = evaluateRewardConditions({
conditions: modifiers.data,
context,
});
- if (matchingCondition) {
- partnerReward.amount = matchingCondition.amount;
-
- console.log("Matching condition found", {
- matchingCondition: JSON.stringify(matchingCondition, null, 2),
- context: JSON.stringify(context, null, 2),
- });
+ if (matchedCondition) {
+ partnerReward = {
+ ...partnerReward,
+ amount: matchedCondition.amount,
+ type: matchedCondition.type,
+ };
}
}
}
diff --git a/apps/web/lib/partners/evaluate-reward-conditions.ts b/apps/web/lib/partners/evaluate-reward-conditions.ts
index 605fcc1af4..2a34663ebc 100644
--- a/apps/web/lib/partners/evaluate-reward-conditions.ts
+++ b/apps/web/lib/partners/evaluate-reward-conditions.ts
@@ -1,3 +1,4 @@
+import { Reward } from "@prisma/client";
import {
RewardCondition,
RewardConditionsArray,
@@ -10,7 +11,7 @@ export const evaluateRewardConditions = ({
}: {
conditions: RewardConditionsArray;
context: RewardContext;
-}) => {
+}): Pick<Reward, "amount" | "type"> | null => {
if (!conditions || !context) {
return null;
}
@@ -46,7 +47,10 @@ export const evaluateRewardConditions = ({
// If this condition group matches, return it
if (conditionsMet) {
- return conditionGroup;
+ return {
+ amount: conditionGroup.amount,
+ type: conditionGroup.type,
+ };
}
}
diff --git a/apps/web/lib/zod/schemas/rewards.ts b/apps/web/lib/zod/schemas/rewards.ts
index 6f7c064596..00613ec4ed 100644
--- a/apps/web/lib/zod/schemas/rewards.ts
+++ b/apps/web/lib/zod/schemas/rewards.ts
@@ -58,6 +58,7 @@ 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),
});
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 558b367ded..94477fa45e 100644
--- a/apps/web/ui/partners/rewards/add-edit-reward-sheet.tsx
+++ b/apps/web/ui/partners/rewards/add-edit-reward-sheet.tsx
@@ -18,7 +18,7 @@ import {
rewardConditionsSchema,
} from "@/lib/zod/schemas/rewards";
import { X } from "@/ui/shared/icons";
-import { EventType } from "@dub/prisma/client";
+import { EventType, RewardStructure } from "@dub/prisma/client";
import {
Button,
MoneyBills2,
@@ -50,7 +50,7 @@ import {
} from "./inline-badge-popover";
import { RewardIconSquare } from "./reward-icon-square";
import { RewardPartnersCard } from "./reward-partners-card";
-import { RewardsLogic } from "./rewards-logic";
+import { REWARD_TYPES, RewardsLogic } from "./rewards-logic";
interface RewardSheetProps {
setIsOpen: Dispatch<SetStateAction<boolean>>;
@@ -103,8 +103,7 @@ function RewardSheetContent({
: defaultValuesSource?.amount,
modifiers: defaultValuesSource?.modifiers?.map((m) => ({
...m,
- amount:
- defaultValuesSource?.type === "flat" ? m.amount / 100 : m.amount,
+ amount: m.type === "flat" ? m.amount / 100 : m.amount,
})),
},
});
@@ -186,7 +185,7 @@ function RewardSheetContent({
modifiers = rewardConditionsArraySchema.parse(
data.modifiers.map((m) => ({
...m,
- amount: type === "flat" ? m.amount * 100 : m.amount,
+ amount: m.type === "flat" ? m.amount * 100 : m.amount,
})),
);
} catch (error) {
@@ -278,20 +277,11 @@ function RewardSheetContent({
<InlineBadgePopoverMenu
selectedValue={type}
onSelect={(value) =>
- setValue("type", value as "flat" | "percentage", {
+ setValue("type", value as RewardStructure, {
shouldDirty: true,
})
}
- items={[
- {
- text: "Flat",
- value: "flat",
- },
- {
- text: "Percentage",
- value: "percentage",
- },
- ]}
+ items={REWARD_TYPES}
/>
</InlineBadgePopover>{" "}
{type === "percentage" && "of "}
diff --git a/apps/web/ui/partners/rewards/rewards-logic.tsx b/apps/web/ui/partners/rewards/rewards-logic.tsx
index d837701c35..a4aa81aadd 100644
--- a/apps/web/ui/partners/rewards/rewards-logic.tsx
+++ b/apps/web/ui/partners/rewards/rewards-logic.tsx
@@ -12,7 +12,7 @@ import {
CONDITION_SALE_ATTRIBUTES,
} from "@/lib/zod/schemas/rewards";
import { X } from "@/ui/shared/icons";
-import { EventType } from "@dub/prisma/client";
+import { EventType, RewardStructure } from "@dub/prisma/client";
import {
ArrowTurnRight2,
Button,
@@ -37,6 +37,17 @@ import {
} from "./inline-badge-popover";
import { RewardIconSquare } from "./reward-icon-square";
+export const REWARD_TYPES = [
+ {
+ text: "Flat",
+ value: "flat",
+ },
+ {
+ text: "Percentage",
+ value: "percentage",
+ },
+];
+
export function RewardsLogic({
isDefaultReward,
}: {
@@ -92,6 +103,7 @@ export function RewardsLogic({
operator: "AND",
conditions: [{}],
amount: getValues("amount") || 0,
+ type: getValues("type"),
})
}
variant={isDefaultReward ? "primary" : "secondary"}
@@ -484,15 +496,37 @@ function OperatorDropdown({ modifierIndex }: { modifierIndex: number }) {
function ResultTerms({ modifierIndex }: { modifierIndex: number }) {
const modifierKey = `modifiers.${modifierIndex}` as const;
- const { control, register } = useAddEditRewardForm();
+ const { control, setValue } = useAddEditRewardForm();
const [amount, type, maxDuration, event] = useWatch({
control,
- name: [`${modifierKey}.amount`, "type", "maxDuration", "event"],
+ name: [
+ `${modifierKey}.amount`,
+ `${modifierKey}.type`,
+ "maxDuration",
+ "event",
+ ],
});
return (
<span className="leading-relaxed">
Then pay{" "}
+ {event === "sale" && (
+ <>
+ a{" "}
+ <InlineBadgePopover text={capitalize(type)}>
+ <InlineBadgePopoverMenu
+ selectedValue={type}
+ onSelect={(value) =>
+ setValue(`${modifierKey}.type`, value as RewardStructure, {
+ shouldDirty: true,
+ })
+ }
+ items={REWARD_TYPES}
+ />
+ </InlineBadgePopover>{" "}
+ {type === "percentage" && "of "}
+ </>
+ )}
<InlineBadgePopover
text={
amount
@@ -523,7 +557,7 @@ function ResultTerms({ modifierIndex }: { modifierIndex: number }) {
function AmountInput({ modifierKey }: { modifierKey: `modifiers.${number}` }) {
const { watch, register } = useAddEditRewardForm();
- const type = watch("type");
+ const type = watch(`${modifierKey}.type`);
const { setIsOpen } = useContext(InlineBadgePopoverContext);