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.

resolve original sale reward maxDuration from modifiers

Pedro Ladeira392bc1299079
diff --git a/apps/web/app/(ee)/api/workflows/create-partner-commission/route.ts b/apps/web/app/(ee)/api/workflows/create-partner-commission/route.ts
index 1ebd527b18..4f349b61d9 100644
--- a/apps/web/app/(ee)/api/workflows/create-partner-commission/route.ts
+++ b/apps/web/app/(ee)/api/workflows/create-partner-commission/route.ts
@@ -11,7 +11,10 @@ import { calculateSaleEarnings } from "@/lib/api/sales/calculate-sale-earnings";
import { executeWorkflows } from "@/lib/api/workflows/execute-workflows";
import { logger } from "@/lib/axiom/server";
import { constructWebhookPartner } from "@/lib/partners/constuct-webhook-partner";
-import { determinePartnerRewards } from "@/lib/partners/determine-partner-reward";
+import {
+ determinePartnerRewards,
+ getRewardMaxDurationForContext,
+} from "@/lib/partners/determine-partner-reward";
import { getRewardAmount } from "@/lib/partners/get-reward-amount";
import { getPlanCapabilities } from "@/lib/plan-capabilities";
import { sendPartnerPostback } from "@/lib/postback/send-partner-postback";
@@ -322,16 +325,24 @@ async function stepCreateCommission(
select: {
id: true,
maxDuration: true,
+ modifiers: true,
},
});
+ const originalMaxDuration = originalReward
+ ? getRewardMaxDurationForContext({
+ reward: originalReward,
+ context,
+ })
+ : null;
+
if (
- typeof originalReward?.maxDuration === "number" &&
- originalReward.maxDuration === 0
+ typeof originalMaxDuration === "number" &&
+ originalMaxDuration === 0
) {
return logAndReturn({
commission: null,
- outputLog: `Partner ${partnerId} is only eligible for first-sale commissions based on the original reward ${originalReward.id}, skipping commission creation...`,
+ outputLog: `Partner ${partnerId} is only eligible for first-sale commissions based on the original reward ${originalReward?.id}, skipping commission creation...`,
});
}
}
diff --git a/apps/web/lib/partners/determine-partner-reward.ts b/apps/web/lib/partners/determine-partner-reward.ts
index bab7398959..7d9de8e24d 100644
--- a/apps/web/lib/partners/determine-partner-reward.ts
+++ b/apps/web/lib/partners/determine-partner-reward.ts
@@ -33,6 +33,35 @@ interface ProductReward {
};
}
+export const getRewardMaxDurationForContext = ({
+ reward,
+ context,
+}: {
+ reward: Pick<Reward, "maxDuration" | "modifiers">;
+ context?: RewardContext;
+}): number | null => {
+ if (!reward.modifiers || !context) {
+ return reward.maxDuration;
+ }
+
+ const modifiers = rewardConditionsArraySchema.safeParse(reward.modifiers);
+
+ if (!modifiers.success) {
+ return reward.maxDuration;
+ }
+
+ const matchedCondition = evaluateRewardConditions({
+ conditions: modifiers.data,
+ context,
+ });
+
+ if (matchedCondition && matchedCondition.maxDuration !== undefined) {
+ return matchedCondition.maxDuration;
+ }
+
+ return reward.maxDuration;
+};
+
export const determinePartnerReward = ({
event,
programEnrollment,
diff --git a/apps/web/tests/rewards/get-reward-max-duration-for-context.test.ts b/apps/web/tests/rewards/get-reward-max-duration-for-context.test.ts
new file mode 100644
index 0000000000..3c1b782b55
--- /dev/null
+++ b/apps/web/tests/rewards/get-reward-max-duration-for-context.test.ts
@@ -0,0 +1,147 @@
+import { getRewardMaxDurationForContext } from "@/lib/partners/determine-partner-reward";
+import { describe, expect, test, vi } from "vitest";
+
+vi.mock("server-only", () => ({}));
+
+const RECURRING_PRODUCT_ID = "prod_recurring";
+
+const modifiers = [
+ {
+ id: "amount-below-threshold",
+ type: "flat" as const,
+ operator: "AND" as const,
+ conditions: [
+ {
+ value: 2000,
+ entity: "sale" as const,
+ operator: "less_than" as const,
+ attribute: "amount" as const,
+ },
+ ],
+ maxDuration: 0,
+ amountInCents: 0,
+ },
+ {
+ id: "recurring-product",
+ type: "percentage" as const,
+ operator: "AND" as const,
+ conditions: [
+ {
+ value: RECURRING_PRODUCT_ID,
+ entity: "sale" as const,
+ operator: "equals_to" as const,
+ attribute: "productId" as const,
+ },
+ ],
+ maxDuration: null,
+ amountInPercentage: 40,
+ },
+];
+
+const oneTimeReward = {
+ maxDuration: 0,
+ modifiers: null,
+};
+
+const rewardWithModifiers = {
+ maxDuration: 0,
+ modifiers,
+};
+
+function shouldSkipForOriginalOneTimeReward(maxDuration: number | null) {
+ return typeof maxDuration === "number" && maxDuration === 0;
+}
+
+describe("getRewardMaxDurationForContext", () => {
+ test("returns row maxDuration when there are no modifiers", () => {
+ expect(
+ getRewardMaxDurationForContext({
+ reward: oneTimeReward,
+ context: { sale: { amount: 3000 } },
+ }),
+ ).toBe(0);
+ });
+
+ test("returns row maxDuration when context is missing", () => {
+ expect(
+ getRewardMaxDurationForContext({
+ reward: rewardWithModifiers,
+ }),
+ ).toBe(0);
+ });
+
+ test("returns lifetime maxDuration when a matching modifier overrides it", () => {
+ expect(
+ getRewardMaxDurationForContext({
+ reward: rewardWithModifiers,
+ context: {
+ sale: {
+ amount: 847,
+ productId: RECURRING_PRODUCT_ID,
+ },
+ },
+ }),
+ ).toBeNull();
+ });
+
+ test("returns row maxDuration when no modifier matches", () => {
+ expect(
+ getRewardMaxDurationForContext({
+ reward: rewardWithModifiers,
+ context: {
+ sale: { amount: 3000 },
+ },
+ }),
+ ).toBe(0);
+ });
+
+ test("returns the matched one-time modifier for a sale below the amount threshold", () => {
+ expect(
+ getRewardMaxDurationForContext({
+ reward: rewardWithModifiers,
+ context: {
+ sale: { amount: 500 },
+ },
+ }),
+ ).toBe(0);
+ });
+});
+
+describe("original-reward skip after a reward ID change", () => {
+ test("plain one-time original reward still skips a later sale", () => {
+ const originalMaxDuration = getRewardMaxDurationForContext({
+ reward: oneTimeReward,
+ context: { sale: { amount: 1000 } },
+ });
+
+ expect(originalMaxDuration).toBe(0);
+ expect(shouldSkipForOriginalOneTimeReward(originalMaxDuration)).toBe(true);
+ });
+
+ test("does not skip when the original reward resolves to a lifetime maxDuration", () => {
+ const originalMaxDuration = getRewardMaxDurationForContext({
+ reward: rewardWithModifiers,
+ context: {
+ sale: {
+ amount: 847,
+ productId: RECURRING_PRODUCT_ID,
+ },
+ },
+ });
+
+ expect(originalMaxDuration).toBeNull();
+ expect(shouldSkipForOriginalOneTimeReward(originalMaxDuration)).toBe(false);
+ });
+
+ test("still skips when the original reward resolves to a one-time maxDuration", () => {
+ const originalMaxDuration = getRewardMaxDurationForContext({
+ reward: rewardWithModifiers,
+ context: {
+ sale: { amount: 3000 },
+ },
+ });
+
+ expect(originalMaxDuration).toBe(0);
+ expect(shouldSkipForOriginalOneTimeReward(originalMaxDuration)).toBe(true);
+ });
+});