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 reward condition evaluation logic and improve test cases.

Kiran Kf1dbe5b299c0
diff --git a/apps/web/lib/partners/create-partner-commission.ts b/apps/web/lib/partners/create-partner-commission.ts
index 598d78fda2..a679ec5a80 100644
--- a/apps/web/lib/partners/create-partner-commission.ts
+++ b/apps/web/lib/partners/create-partner-commission.ts
@@ -12,10 +12,11 @@ import { createId } from "../api/create-id";
import { syncTotalCommissions } from "../api/partners/sync-total-commissions";
import { calculateSaleEarnings } from "../api/sales/calculate-sale-earnings";
import { Session } from "../auth";
-import { RewardContext, RewardProps } from "../types";
+import { RewardProps } from "../types";
import { sendWorkspaceWebhook } from "../webhook/publish";
import { CommissionEnrichedSchema } from "../zod/schemas/commissions";
import { determinePartnerReward } from "./determine-partner-reward";
+import { RewardContext } from "./evaluate-reward-conditions";
export const createPartnerCommission = async ({
reward,
diff --git a/apps/web/lib/partners/determine-partner-reward.ts b/apps/web/lib/partners/determine-partner-reward.ts
index f5b5576277..6f5f17e5ff 100644
--- a/apps/web/lib/partners/determine-partner-reward.ts
+++ b/apps/web/lib/partners/determine-partner-reward.ts
@@ -62,6 +62,11 @@ export const determinePartnerReward = async ({
if (matchingCondition) {
partnerReward.amount = matchingCondition.amount;
+
+ console.log("Matching condition found", {
+ matchingCondition: JSON.stringify(matchingCondition, null, 2),
+ context: JSON.stringify(context, null, 2),
+ });
}
}
}
diff --git a/apps/web/lib/partners/evaluate-reward-conditions.ts b/apps/web/lib/partners/evaluate-reward-conditions.ts
index af808bf743..17cc8765ec 100644
--- a/apps/web/lib/partners/evaluate-reward-conditions.ts
+++ b/apps/web/lib/partners/evaluate-reward-conditions.ts
@@ -90,12 +90,6 @@ export const evaluateRewardConditions = ({
conditionsMet = conditionResults.some((result) => result);
}
- console.log("conditionGroup", {
- conditionGroup,
- conditionResults,
- conditionsMet,
- });
-
// If this condition group matches, return it
if (conditionsMet) {
return conditionGroup;
diff --git a/apps/web/scripts/reward-conditions.ts b/apps/web/scripts/reward-conditions.ts
index bae82aefcc..8de2f0795e 100644
--- a/apps/web/scripts/reward-conditions.ts
+++ b/apps/web/scripts/reward-conditions.ts
@@ -6,32 +6,31 @@ async function main() {
const conditions = [
{
operator: "AND",
- amount: 200,
+ amount: 100,
conditions: [
{
entity: "customer",
attribute: "country",
operator: "equals_to",
- value: "US",
- },
- {
- entity: "sale",
- attribute: "productId",
- operator: "equals_to",
- value: "basic",
+ value: "IN",
},
],
},
-
{
operator: "AND",
- amount: 100,
+ amount: 200,
conditions: [
{
entity: "customer",
attribute: "country",
operator: "equals_to",
- value: "IN",
+ value: "US",
+ },
+ {
+ entity: "sale",
+ attribute: "productId",
+ operator: "equals_to",
+ value: "basic",
},
],
},
@@ -39,7 +38,7 @@ async function main() {
await prisma.reward.update({
where: {
- id: "rw_1K06XABVAY518MGAA70VGPY1W",
+ id: "rw_1K0E4ND6323QZGBVA4HVHEPW9",
},
data: {
modifiers: conditions,
diff --git a/apps/web/tests/partners/evaluate-reward-rules.test.ts b/apps/web/tests/partners/evaluate-reward-rules.test.ts
index a916d78013..12d2919621 100644
--- a/apps/web/tests/partners/evaluate-reward-rules.test.ts
+++ b/apps/web/tests/partners/evaluate-reward-rules.test.ts
@@ -1,11 +1,10 @@
-import { evaluateRewardConditions } from "@/lib/partners/evaluate-reward-conditions";
-import { RewardContext } from "@/lib/types";
+import { evaluateRewardConditions, RewardContext } from "@/lib/partners/evaluate-reward-conditions";
import { describe, expect, test } from "vitest";
describe("evaluateRewardConditions", () => {
describe("AND operator", () => {
- test("should return true when all conditions are met", () => {
- const conditions = {
+ test("should return matching condition when all conditions are met", () => {
+ const conditions = [{
operator: "AND" as const,
amount: 5000,
conditions: [
@@ -22,7 +21,7 @@ describe("evaluateRewardConditions", () => {
value: "premium",
},
],
- };
+ }];
const context: RewardContext = {
customer: {
@@ -38,11 +37,11 @@ describe("evaluateRewardConditions", () => {
context,
});
- expect(result).toBe(true);
+ expect(result).toEqual(conditions[0]);
});
- test("should return false when one condition is not met", () => {
- const conditions = {
+ test("should return null when one condition is not met", () => {
+ const conditions = [{
operator: "AND" as const,
amount: 5000,
conditions: [
@@ -59,7 +58,7 @@ describe("evaluateRewardConditions", () => {
value: "premium",
},
],
- };
+ }];
const context: RewardContext = {
customer: {
@@ -75,11 +74,11 @@ describe("evaluateRewardConditions", () => {
context,
});
- expect(result).toBe(false);
+ expect(result).toBe(null);
});
- test("should return false when all conditions are not met", () => {
- const conditions = {
+ test("should return null when all conditions are not met", () => {
+ const conditions = [{
operator: "AND" as const,
amount: 5000,
conditions: [
@@ -96,7 +95,7 @@ describe("evaluateRewardConditions", () => {
value: "premium",
},
],
- };
+ }];
const context: RewardContext = {
customer: {
@@ -112,13 +111,13 @@ describe("evaluateRewardConditions", () => {
context,
});
- expect(result).toBe(false);
+ expect(result).toBe(null);
});
});
describe("OR operator", () => {
- test("should return true when one condition is met", () => {
- const conditions = {
+ test("should return matching condition when one condition is met", () => {
+ const conditions = [{
operator: "OR" as const,
amount: 5000,
conditions: [
@@ -135,7 +134,7 @@ describe("evaluateRewardConditions", () => {
value: "premium",
},
],
- };
+ }];
const context: RewardContext = {
customer: {
@@ -151,11 +150,11 @@ describe("evaluateRewardConditions", () => {
context,
});
- expect(result).toBe(true);
+ expect(result).toEqual(conditions[0]);
});
- test("should return true when all conditions are met", () => {
- const conditions = {
+ test("should return matching condition when all conditions are met", () => {
+ const conditions = [{
operator: "OR" as const,
amount: 5000,
conditions: [
@@ -172,7 +171,7 @@ describe("evaluateRewardConditions", () => {
value: "premium",
},
],
- };
+ }];
const context: RewardContext = {
customer: {
@@ -188,11 +187,11 @@ describe("evaluateRewardConditions", () => {
context,
});
- expect(result).toBe(true);
+ expect(result).toEqual(conditions[0]);
});
- test("should return false when no conditions are met", () => {
- const conditions = {
+ test("should return null when no conditions are met", () => {
+ const conditions = [{
operator: "OR" as const,
amount: 5000,
conditions: [
@@ -209,7 +208,7 @@ describe("evaluateRewardConditions", () => {
value: "premium",
},
],
- };
+ }];
const context: RewardContext = {
customer: {
@@ -225,14 +224,112 @@ describe("evaluateRewardConditions", () => {
context,
});
- expect(result).toBe(false);
+ expect(result).toBe(null);
+ });
+ });
+
+ describe("multiple condition groups", () => {
+ test("should return first matching condition group", () => {
+ const conditions = [
+ {
+ operator: "AND" as const,
+ amount: 1000,
+ conditions: [
+ {
+ entity: "customer" as const,
+ attribute: "country" as const,
+ operator: "equals_to" as const,
+ value: "US",
+ },
+ ],
+ },
+ {
+ operator: "AND" as const,
+ amount: 2000,
+ conditions: [
+ {
+ entity: "customer" as const,
+ attribute: "country" as const,
+ operator: "equals_to" as const,
+ value: "CA",
+ },
+ ],
+ },
+ {
+ operator: "AND" as const,
+ amount: 3000,
+ conditions: [
+ {
+ entity: "customer" as const,
+ attribute: "country" as const,
+ operator: "equals_to" as const,
+ value: "UK",
+ },
+ ],
+ },
+ ];
+
+ const context: RewardContext = {
+ customer: {
+ country: "CA", // This should match the second condition group
+ },
+ };
+
+ const result = evaluateRewardConditions({
+ conditions,
+ context,
+ });
+
+ expect(result).toEqual(conditions[1]); // Should return the second condition group
+ });
+
+ test("should return null when no condition groups match", () => {
+ const conditions = [
+ {
+ operator: "AND" as const,
+ amount: 1000,
+ conditions: [
+ {
+ entity: "customer" as const,
+ attribute: "country" as const,
+ operator: "equals_to" as const,
+ value: "US",
+ },
+ ],
+ },
+ {
+ operator: "AND" as const,
+ amount: 2000,
+ conditions: [
+ {
+ entity: "customer" as const,
+ attribute: "country" as const,
+ operator: "equals_to" as const,
+ value: "CA",
+ },
+ ],
+ },
+ ];
+
+ const context: RewardContext = {
+ customer: {
+ country: "UK", // Doesn't match any condition group
+ },
+ };
+
+ const result = evaluateRewardConditions({
+ conditions,
+ context,
+ });
+
+ expect(result).toBe(null);
});
});
describe("condition operators", () => {
describe("equals_to", () => {
test("should match exact string values", () => {
- const conditions = {
+ const conditions = [{
operator: "AND" as const,
amount: 5000,
conditions: [
@@ -243,7 +340,7 @@ describe("evaluateRewardConditions", () => {
value: "US",
},
],
- };
+ }];
const context: RewardContext = {
customer: {
@@ -256,11 +353,11 @@ describe("evaluateRewardConditions", () => {
context,
});
- expect(result).toBe(true);
+ expect(result).toEqual(conditions[0]);
});
test("should not match different string values", () => {
- const conditions = {
+ const conditions = [{
operator: "AND" as const,
amount: 5000,
conditions: [
@@ -271,7 +368,7 @@ describe("evaluateRewardConditions", () => {
value: "US",
},
],
- };
+ }];
const context: RewardContext = {
customer: {
@@ -284,13 +381,13 @@ describe("evaluateRewardConditions", () => {
context,
});
- expect(result).toBe(false);
+ expect(result).toBe(null);
});
});
describe("not_equals", () => {
test("should match when values are different", () => {
- const conditions = {
+ const conditions = [{
operator: "AND" as const,
amount: 5000,
conditions: [
@@ -301,7 +398,7 @@ describe("evaluateRewardConditions", () => {
value: "US",
},
],
- };
+ }];
const context: RewardContext = {
customer: {
@@ -314,11 +411,11 @@ describe("evaluateRewardConditions", () => {
context,
});
- expect(result).toBe(true);
+ expect(result).toEqual(conditions[0]);
});
test("should not match when values are the same", () => {
- const conditions = {
+ const conditions = [{
operator: "AND" as const,
amount: 5000,
conditions: [
@@ -329,7 +426,7 @@ describe("evaluateRewardConditions", () => {
value: "US",
},
],
- };
+ }];
const context: RewardContext = {
customer: {
@@ -342,13 +439,13 @@ describe("evaluateRewardConditions", () => {
context,
});
- expect(result).toBe(false);
+ expect(result).toBe(null);
});
});
describe("in", () => {
test("should match when value is in array", () => {
- const conditions = {
+ const conditions = [{
operator: "AND" as const,
amount: 5000,
conditions: [
@@ -359,11 +456,11 @@ describe("evaluateRewardConditions", () => {
value: ["US", "CA", "UK"],
},
],
- };
+ }];
const context: RewardContext = {
customer: {
- country: "US",
+ country: "CA",
},
};
@@ -372,11 +469,11 @@ describe("evaluateRewardConditions", () => {
context,
});
- expect(result).toBe(true);
+ expect(result).toEqual(conditions[0]);
});
test("should not match when value is not in array", () => {
- const conditions = {
+ const conditions = [{
operator: "AND" as const,
amount: 5000,
conditions: [
@@ -387,7 +484,7 @@ describe("evaluateRewardConditions", () => {
value: ["US", "CA", "UK"],
},
],
- };
+ }];
const context: RewardContext = {
customer: {
@@ -400,13 +497,13 @@ describe("evaluateRewardConditions", () => {
context,
});
- expect(result).toBe(false);
+ expect(result).toBe(null);
});
});
describe("not_in", () => {
test("should match when value is not in array", () => {
- const conditions = {
+ const conditions = [{
operator: "AND" as const,
amount: 5000,
conditions: [
@@ -417,7 +514,7 @@ describe("evaluateRewardConditions", () => {
value: ["US", "CA", "UK"],
},
],
- };
+ }];
const context: RewardContext = {
customer: {
@@ -430,11 +527,11 @@ describe("evaluateRewardConditions", () => {
context,
});
- expect(result).toBe(true);
+ expect(result).toEqual(conditions[0]);
});
test("should not match when value is in array", () => {
- const conditions = {
+ const conditions = [{
operator: "AND" as const,
amount: 5000,
conditions: [
@@ -445,7 +542,7 @@ describe("evaluateRewardConditions", () => {
value: ["US", "CA", "UK"],
},
],
- };
+ }];
const context: RewardContext = {
customer: {
@@ -458,13 +555,13 @@ describe("evaluateRewardConditions", () => {
context,
});
- expect(result).toBe(false);
+ expect(result).toBe(null);
});
});
describe("starts_with", () => {
test("should match when string starts with value", () => {
- const conditions = {
+ const conditions = [{
operator: "AND" as const,
amount: 5000,
conditions: [
@@ -475,11 +572,11 @@ describe("evaluateRewardConditions", () => {
value: "premium",
},
],
- };
+ }];
const context: RewardContext = {
sale: {
- productId: "premium-2024",
+ productId: "premium-plus",
},
};
@@ -488,11 +585,11 @@ describe("evaluateRewardConditions", () => {
context,
});
- expect(result).toBe(true);
+ expect(result).toEqual(conditions[0]);
});
test("should not match when string does not start with value", () => {
- const conditions = {
+ const conditions = [{
operator: "AND" as const,
amount: 5000,
conditions: [
@@ -503,11 +600,11 @@ describe("evaluateRewardConditions", () => {
value: "premium",
},
],
- };
+ }];
const context: RewardContext = {
sale: {
- productId: "basic-premium",
+ productId: "basic-plus",
},
};
@@ -516,13 +613,13 @@ describe("evaluateRewardConditions", () => {
context,
});
- expect(result).toBe(false);
+ expect(result).toBe(null);
});
});
describe("ends_with", () => {
test("should match when string ends with value", () => {
- const conditions = {
+ const conditions = [{
operator: "AND" as const,
amount: 5000,
conditions: [
@@ -530,14 +627,14 @@ describe("evaluateRewardConditions", () => {
entity: "sale" as const,
attribute: "productId" as const,
operator: "ends_with" as const,
- value: "2024",
+ value: "plus",
},
],
- };
+ }];
const context: RewardContext = {
sale: {
- productId: "premium-2024",
+ productId: "premium-plus",
},
};
@@ -546,11 +643,11 @@ describe("evaluateRewardConditions", () => {
context,
});
- expect(result).toBe(true);
+ expect(result).toEqual(conditions[0]);
});
test("should not match when string does not end with value", () => {
- const conditions = {
+ const conditions = [{
operator: "AND" as const,
amount: 5000,
conditions: [
@@ -558,14 +655,14 @@ describe("evaluateRewardConditions", () => {
entity: "sale" as const,
attribute: "productId" as const,
operator: "ends_with" as const,
- value: "2024",
+ value: "plus",
},
],
- };
+ }];
const context: RewardContext = {
sale: {
- productId: "2024-premium",
+ productId: "premium-basic",
},
};
@@ -574,13 +671,15 @@ describe("evaluateRewardConditions", () => {
context,
});
- expect(result).toBe(false);
+ expect(result).toBe(null);
});
});
});
describe("edge cases", () => {
- test("should return false when conditions is null", () => {
+ test("should return null when conditions array is empty", () => {
+ const conditions: any[] = [];
+
const context: RewardContext = {
customer: {
country: "US",
@@ -588,15 +687,15 @@ describe("evaluateRewardConditions", () => {
};
const result = evaluateRewardConditions({
- conditions: null as any,
+ conditions,
context,
});
- expect(result).toBe(false);
+ expect(result).toBe(null);
});
- test("should return false when context is null", () => {
- const conditions = {
+ test("should return null when context is null", () => {
+ const conditions = [{
operator: "AND" as const,
amount: 5000,
conditions: [
@@ -607,18 +706,18 @@ describe("evaluateRewardConditions", () => {
value: "US",
},
],
- };
+ }];
const result = evaluateRewardConditions({
conditions,
context: null as any,
});
- expect(result).toBe(false);
+ expect(result).toBe(null);
});
- test("should return false when field value is undefined", () => {
- const conditions = {
+ test("should return null when field value is undefined", () => {
+ const conditions = [{
operator: "AND" as const,
amount: 5000,
conditions: [
@@ -629,139 +728,11 @@ describe("evaluateRewardConditions", () => {
value: "US",
},
],
- };
+ }];
const context: RewardContext = {
customer: {
- country: null, // Field value is null/undefined
- },
- };
-
- const result = evaluateRewardConditions({
- conditions,
- context,
- });
-
- expect(result).toBe(false);
- });
-
- test("should return false when customer object is missing", () => {
- const conditions = {
- operator: "AND" as const,
- amount: 5000,
- conditions: [
- {
- entity: "customer" as const,
- attribute: "country" as const,
- operator: "equals_to" as const,
- value: "US",
- },
- ],
- };
-
- const context: RewardContext = {
- // customer object is missing
- };
-
- const result = evaluateRewardConditions({
- conditions,
- context,
- });
-
- expect(result).toBe(false);
- });
-
- test("should return false when sale object is missing", () => {
- const conditions = {
- operator: "AND" as const,
- amount: 5000,
- conditions: [
- {
- entity: "sale" as const,
- attribute: "productId" as const,
- operator: "equals_to" as const,
- value: "premium",
- },
- ],
- };
-
- const context: RewardContext = {
- // sale object is missing
- };
-
- const result = evaluateRewardConditions({
- conditions,
- context,
- });
-
- expect(result).toBe(false);
- });
- });
-
- describe("complex scenarios", () => {
- test("should handle multiple conditions with mixed operators", () => {
- const conditions = {
- operator: "AND" as const,
- amount: 5000,
- conditions: [
- {
- entity: "customer" as const,
- attribute: "country" as const,
- operator: "in" as const,
- value: ["US", "CA", "UK"],
- },
- {
- entity: "sale" as const,
- attribute: "productId" as const,
- operator: "starts_with" as const,
- value: "premium",
- },
- ],
- };
-
- const context: RewardContext = {
- customer: {
- country: "US",
- },
- sale: {
- productId: "premium-2024",
- },
- };
-
- const result = evaluateRewardConditions({
- conditions,
- context,
- });
-
- expect(result).toBe(true);
- });
-
- test("should handle OR with multiple conditions where only one is true", () => {
- const conditions = {
- operator: "OR" as const,
- amount: 5000,
- conditions: [
- {
- entity: "customer" as const,
- attribute: "country" as const,
- operator: "equals_to" as const,
- value: "FR", // This will be false
- },
- {
- entity: "sale" as const,
- attribute: "productId" as const,
- operator: "equals_to" as const,
- value: "premium", // This will be true
- },
- ],
- };
-
- const context: RewardContext = {
- customer: {
- country: "US", // Different from first condition
- },
- sale: {
- productId: "premium", // Matches second condition
+ // country is undefined
},
};
@@ -770,7 +741,7 @@ describe("evaluateRewardConditions", () => {
context,
});
- expect(result).toBe(true);
+ expect(result).toBe(null);
});
});
});