Skip to content

Commit bbab603

Browse files
authored
Merge branch 'main' into feat/advanced-filters
2 parents f7226c7 + deb1b91 commit bbab603

File tree

3 files changed

+97
-56
lines changed

3 files changed

+97
-56
lines changed

apps/web/lib/zod/schemas/rewards.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export const REWARD_CONDITIONS: Record<
111111
},
112112
{
113113
id: "submitted",
114-
label: "qualified lead",
114+
label: "submitted referral",
115115
},
116116
{
117117
id: "trial",

apps/web/tests/rewards/reward-conditions.test.ts

Lines changed: 79 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,68 +1662,96 @@ describe("evaluateRewardConditions", () => {
16621662
});
16631663

16641664
describe("subscription duration conditions", () => {
1665-
describe("customer.subscriptionDurationMonths", () => {
1666-
test("should match when subscription duration meets greater_than_or_equal condition", () => {
1667-
const conditions = [
1665+
const lessThanOrEqualCondition = [
1666+
{
1667+
operator: "AND" as const,
1668+
type: "flat" as const,
1669+
amountInCents: 5000,
1670+
conditions: [
16681671
{
1669-
operator: "AND" as const,
1670-
type: "flat" as const,
1671-
amountInCents: 5000,
1672-
conditions: [
1673-
{
1674-
entity: "customer" as const,
1675-
attribute: "subscriptionDurationMonths" as const,
1676-
operator: "greater_than_or_equal" as const,
1677-
value: 12,
1678-
},
1679-
],
1672+
entity: "customer" as const,
1673+
attribute: "subscriptionDurationMonths" as const,
1674+
operator: "less_than_or_equal" as const,
1675+
value: 12,
16801676
},
1681-
];
1682-
1683-
const context: RewardContext = {
1684-
customer: {
1685-
subscriptionDurationMonths: 12,
1677+
],
1678+
},
1679+
];
1680+
1681+
const greaterThanCondition = [
1682+
{
1683+
operator: "AND" as const,
1684+
type: "flat" as const,
1685+
amountInCents: 5000,
1686+
conditions: [
1687+
{
1688+
entity: "customer" as const,
1689+
attribute: "subscriptionDurationMonths" as const,
1690+
operator: "greater_than" as const,
1691+
value: 12,
16861692
},
1687-
};
1693+
],
1694+
},
1695+
];
16881696

1689-
const result = evaluateRewardConditions({
1690-
conditions,
1691-
context,
1692-
});
1697+
test("should match when subscription duration meets less_than_or_equal condition", () => {
1698+
const context: RewardContext = {
1699+
customer: {
1700+
subscriptionDurationMonths: 12,
1701+
},
1702+
};
16931703

1694-
expect(result).toEqual(conditions[0]);
1704+
const result = evaluateRewardConditions({
1705+
conditions: lessThanOrEqualCondition,
1706+
context,
16951707
});
16961708

1697-
test("should not match when subscription duration is less than condition value", () => {
1698-
const conditions = [
1699-
{
1700-
operator: "AND" as const,
1701-
type: "flat" as const,
1702-
amountInCents: 5000,
1703-
conditions: [
1704-
{
1705-
entity: "customer" as const,
1706-
attribute: "subscriptionDurationMonths" as const,
1707-
operator: "greater_than_or_equal" as const,
1708-
value: 12,
1709-
},
1710-
],
1711-
},
1712-
];
1709+
expect(result).toEqual(lessThanOrEqualCondition[0]);
1710+
});
17131711

1714-
const context: RewardContext = {
1715-
customer: {
1716-
subscriptionDurationMonths: 6,
1717-
},
1718-
};
1712+
test("should not match when subscription duration is more than less_than_or_equal condition value", () => {
1713+
const context: RewardContext = {
1714+
customer: {
1715+
subscriptionDurationMonths: 16,
1716+
},
1717+
};
17191718

1720-
const result = evaluateRewardConditions({
1721-
conditions,
1722-
context,
1723-
});
1719+
const result = evaluateRewardConditions({
1720+
conditions: lessThanOrEqualCondition,
1721+
context,
1722+
});
17241723

1725-
expect(result).toBe(null);
1724+
expect(result).toBe(null);
1725+
});
1726+
1727+
test("should match when subscription duration meets greater_than condition", () => {
1728+
const context: RewardContext = {
1729+
customer: {
1730+
subscriptionDurationMonths: 16,
1731+
},
1732+
};
1733+
1734+
const result = evaluateRewardConditions({
1735+
conditions: greaterThanCondition,
1736+
context,
17261737
});
1738+
1739+
expect(result).toEqual(greaterThanCondition[0]);
1740+
});
1741+
1742+
test("should not match when subscription duration is less than greater_than condition value", () => {
1743+
const context: RewardContext = {
1744+
customer: {
1745+
subscriptionDurationMonths: 6,
1746+
},
1747+
};
1748+
1749+
const result = evaluateRewardConditions({
1750+
conditions: greaterThanCondition,
1751+
context,
1752+
});
1753+
1754+
expect(result).toBe(null);
17271755
});
17281756
});
17291757
});

apps/web/ui/partners/rewards/rewards-logic.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import { constructRewardAmount } from "@/lib/api/sales/construct-reward-amount";
44
import { getPlanCapabilities } from "@/lib/plan-capabilities";
5+
import { REFERRAL_ENABLED_PROGRAM_IDS } from "@/lib/referrals/constants";
6+
import useProgram from "@/lib/swr/use-program";
57
import useWorkspace from "@/lib/swr/use-workspace";
68
import { RECURRING_MAX_DURATIONS } from "@/lib/zod/schemas/misc";
79
import {
@@ -273,6 +275,7 @@ function ConditionLogic({
273275
conditionIndex: number;
274276
onRemove?: () => void;
275277
}) {
278+
const { program } = useProgram();
276279
const modifierKey = `modifiers.${modifierIndex}` as const;
277280
const conditionKey = `${modifierKey}.conditions.${conditionIndex}` as const;
278281

@@ -511,10 +514,20 @@ function ConditionLogic({
511514
(condition.value as string[] | undefined) ??
512515
(isArrayValue ? [] : undefined)
513516
}
514-
items={attribute.options.map(({ id, label }) => ({
515-
text: label,
516-
value: id,
517-
}))}
517+
items={attribute.options
518+
.filter(
519+
(attribute) =>
520+
isCustomerSourceCondition &&
521+
(attribute.id !== "submitted" ||
522+
(program &&
523+
REFERRAL_ENABLED_PROGRAM_IDS.includes(
524+
program.id,
525+
))),
526+
)
527+
.map(({ id, label }) => ({
528+
text: label,
529+
value: id,
530+
}))}
518531
onSelect={(value) => {
519532
setValue(conditionKey, {
520533
...condition,

0 commit comments

Comments
 (0)