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.

Ability to set a different first commission amount for recurring sales (#3823)

Kiran K972617474881
diff --git a/apps/web/lib/partners/create-partner-commission.ts b/apps/web/lib/partners/create-partner-commission.ts
index 5d191217d1..b3daed102d 100644
--- a/apps/web/lib/partners/create-partner-commission.ts
+++ b/apps/web/lib/partners/create-partner-commission.ts
@@ -138,6 +138,12 @@ export const createPartnerCommission = async ({
subscriptionStartDate,
subscriptionDurationMonths,
},
+ ...(event === "sale" && {
+ sale: {
+ ...context?.sale,
+ type: firstCommission ? "recurring" : "new",
+ },
+ }),
};
}
diff --git a/apps/web/lib/zod/schemas/rewards.ts b/apps/web/lib/zod/schemas/rewards.ts
index c4040ea37c..aff6e3c75f 100644
--- a/apps/web/lib/zod/schemas/rewards.ts
+++ b/apps/web/lib/zod/schemas/rewards.ts
@@ -180,6 +180,21 @@ export const REWARD_CONDITIONS: Record<
label: "Amount",
type: "currency",
},
+ {
+ id: "type",
+ label: "Type",
+ type: "string",
+ options: [
+ {
+ id: "new",
+ label: "new",
+ },
+ {
+ id: "recurring",
+ label: "recurring",
+ },
+ ],
+ },
],
},
],
@@ -379,6 +394,7 @@ export const rewardContextSchema = z.object({
.object({
productId: z.string().nullish(),
amount: z.number().nullish(),
+ type: z.enum(["new", "recurring"]).nullish(),
})
.optional(),
diff --git a/apps/web/tests/rewards/sale-reward.test.ts b/apps/web/tests/rewards/sale-reward.test.ts
index 5aba9b755a..c1e82a7d49 100644
--- a/apps/web/tests/rewards/sale-reward.test.ts
+++ b/apps/web/tests/rewards/sale-reward.test.ts
@@ -1,4 +1,4 @@
-import { TrackSaleResponse } from "@/lib/types";
+import { TrackLeadResponse, TrackSaleResponse } from "@/lib/types";
import {
randomCustomer,
randomId,
@@ -15,7 +15,7 @@ import { verifyCommission } from "tests/utils/verify-commission";
import { describe, expect, test } from "vitest";
import { IntegrationHarness } from "../utils/integration";
-describe.concurrent("Sale rewards with conditions", async () => {
+describe("Sale rewards with conditions", async () => {
const h = new IntegrationHarness();
const { http } = await h.init();
@@ -27,150 +27,190 @@ describe.concurrent("Sale rewards with conditions", async () => {
invoiceId: `INV_${randomId()}`,
});
- test("When {Sale} {Product ID} is {regularProductId}", async () => {
- const sale = randomSale("E2E base condition");
+ describe.concurrent("concurrent track/sale tests", () => {
+ test("When {Sale} {Product ID} is {regularProductId}", async () => {
+ const sale = randomSale("E2E base condition");
+
+ const response = await http.post<TrackSaleResponse>({
+ path: "/track/sale",
+ body: {
+ ...sale,
+ customerExternalId: E2E_CUSTOMER_SALE_CONDITIONS_EXTERNAL_ID,
+ metadata: {
+ productId: "regularProductId",
+ },
+ },
+ });
+
+ expect(response.status).toEqual(200);
+
+ await verifyCommission({
+ http,
+ invoiceId: sale.invoiceId,
+ expectedEarnings: E2E_SALE_REWARD.amountInCents,
+ });
+ });
- const response = await http.post<TrackSaleResponse>({
- path: "/track/sale",
- body: {
- ...sale,
- customerExternalId: E2E_CUSTOMER_SALE_CONDITIONS_EXTERNAL_ID,
- metadata: {
- productId: "regularProductId",
+ test("When {Sale} {Product ID} is {premiumProductId}", async () => {
+ const sale = randomSale("E2E sale product ID condition");
+
+ const response = await http.post<TrackSaleResponse>({
+ path: "/track/sale",
+ body: {
+ ...sale,
+ customerExternalId: E2E_CUSTOMER_SALE_CONDITIONS_EXTERNAL_ID,
+ metadata: {
+ productId: "premiumProductId",
+ },
},
- },
+ });
+
+ expect(response.status).toEqual(200);
+
+ await verifyCommission({
+ http,
+ invoiceId: sale.invoiceId,
+ expectedEarnings: E2E_SALE_REWARD.modifiers[0].amountInCents!,
+ });
});
- expect(response.status).toEqual(200);
+ test("When {Sale} {Amount} is greater than {15000}", async () => {
+ const sale = randomSale("E2E sale amount condition");
+
+ const response = await http.post<TrackSaleResponse>({
+ path: "/track/sale",
+ body: {
+ ...sale,
+ amount: 17500,
+ customerExternalId: E2E_CUSTOMER_SALE_CONDITIONS_EXTERNAL_ID,
+ metadata: {
+ productId: "premiumProductId",
+ },
+ },
+ });
- await verifyCommission({
- http,
- invoiceId: sale.invoiceId,
- expectedEarnings: E2E_SALE_REWARD.amountInCents,
+ expect(response.status).toEqual(200);
+
+ await verifyCommission({
+ http,
+ invoiceId: sale.invoiceId,
+ expectedEarnings: E2E_SALE_REWARD.modifiers[1].amountInCents!,
+ });
});
- });
- test("When {Sale} {Product ID} is {premiumProductId}", async () => {
- const sale = randomSale("E2E sale product ID condition");
+ test("when {Customer} {Country} is {SG}", async () => {
+ const sale = randomSale("E2E customer country condition");
- const response = await http.post<TrackSaleResponse>({
- path: "/track/sale",
- body: {
- ...sale,
- customerExternalId: E2E_CUSTOMER_SALE_CONDITIONS_EXTERNAL_ID,
- metadata: {
- productId: "premiumProductId",
+ const trackSaleResponse = await http.post<TrackSaleResponse>({
+ path: "/track/sale",
+ body: {
+ ...sale,
+ customerExternalId: E2E_CUSTOMER_COUNTRY_CONDITIONS_EXTERNAL_ID,
},
- },
- });
+ });
- expect(response.status).toEqual(200);
+ expect(trackSaleResponse.status).toEqual(200);
- await verifyCommission({
- http,
- invoiceId: sale.invoiceId,
- expectedEarnings: E2E_SALE_REWARD.modifiers[0].amountInCents!,
+ await verifyCommission({
+ http,
+ invoiceId: sale.invoiceId,
+ expectedEarnings: E2E_SALE_REWARD.modifiers[2].amountInCents!,
+ });
});
- });
- test("When {Sale} {Amount} is greater than {15000}", async () => {
- const sale = randomSale("E2E sale amount condition");
-
- const response = await http.post<TrackSaleResponse>({
- path: "/track/sale",
- body: {
- ...sale,
- amount: 17500,
- customerExternalId: E2E_CUSTOMER_SALE_CONDITIONS_EXTERNAL_ID,
- metadata: {
- productId: "premiumProductId",
+ test("when {Customer} {Signup Date} is {greater than} {Feb 16, 2026} AND {less than} {Feb 18, 2026}", async () => {
+ const sale = randomSale("E2E customer signup date condition");
+
+ const trackSaleResponse = await http.post<TrackSaleResponse>({
+ path: "/track/sale",
+ body: {
+ ...sale,
+ customerExternalId: E2E_CUSTOMER_SIGNUP_DATE_CONDITIONS_EXTERNAL_ID,
},
- },
- });
+ });
- expect(response.status).toEqual(200);
+ expect(trackSaleResponse.status).toEqual(200);
- await verifyCommission({
- http,
- invoiceId: sale.invoiceId,
- expectedEarnings: E2E_SALE_REWARD.modifiers[1].amountInCents!,
+ await verifyCommission({
+ http,
+ invoiceId: sale.invoiceId,
+ expectedEarnings: E2E_SALE_REWARD.modifiers[4].amountInCents!,
+ });
});
});
- test("when {Customer} {Country} is {SG}", async () => {
- const sale = randomSale("E2E customer country condition");
+ const newCustomer = randomCustomer();
- const trackSaleResponse = await http.post<TrackSaleResponse>({
- path: "/track/sale",
- body: {
- ...sale,
- customerExternalId: E2E_CUSTOMER_COUNTRY_CONDITIONS_EXTERNAL_ID,
- },
- });
+ describe.sequential("sequential track/sale tests", () => {
+ test("when {Sale} {Type} is {new} vs {recurring}", async () => {
+ const clickResponse = await http.post<{ clickId: string }>({
+ path: "/track/click",
+ headers: E2E_TRACK_CLICK_HEADERS,
+ body: {
+ domain: "getacme.link",
+ key: "marvin",
+ },
+ });
+ expect(clickResponse.status).toEqual(200);
+ const trackedClickId = clickResponse.data.clickId;
+ expect(trackedClickId).toStrictEqual(expect.any(String));
+
+ const trackLeadResponse = await http.post<TrackLeadResponse>({
+ path: "/track/lead",
+ body: {
+ clickId: trackedClickId,
+ eventName: "E2E lead for new/recurring sale test",
+ customerExternalId: newCustomer.externalId,
+ customerName: newCustomer.name,
+ customerEmail: newCustomer.email,
+ customerAvatar: newCustomer.avatar,
+ mode: "wait",
+ },
+ });
- expect(trackSaleResponse.status).toEqual(200);
+ expect(trackLeadResponse.status).toEqual(200);
- await verifyCommission({
- http,
- invoiceId: sale.invoiceId,
- expectedEarnings: E2E_SALE_REWARD.modifiers[2].amountInCents!,
- });
- });
+ const sale = randomSale("E2E first sale");
- test("when {Customer} {Subscription Duration} is {less than or equal to} {3}", async () => {
- const clickResponse = await http.post<{ clickId: string }>({
- path: "/track/click",
- headers: E2E_TRACK_CLICK_HEADERS,
- body: {
- domain: "getacme.link",
- key: "marvin",
- },
- });
- expect(clickResponse.status).toEqual(200);
- const trackedClickId = clickResponse.data.clickId;
- expect(trackedClickId).toStrictEqual(expect.any(String));
- const newCustomer = randomCustomer();
- const sale = randomSale("E2E customer subscription duration condition");
-
- // here we use direct sale tracking to save time
- const trackSaleResponse = await http.post<TrackSaleResponse>({
- path: "/track/sale",
- body: {
- ...sale,
- clickId: trackedClickId,
- customerExternalId: newCustomer.externalId,
- customerName: newCustomer.name,
- customerEmail: newCustomer.email,
- customerAvatar: newCustomer.avatar,
- },
- });
+ const trackSaleResponse = await http.post<TrackSaleResponse>({
+ path: "/track/sale",
+ body: {
+ ...sale,
+ customerExternalId: newCustomer.externalId,
+ },
+ });
- expect(trackSaleResponse.status).toEqual(200);
+ expect(trackSaleResponse.status).toEqual(200);
- await verifyCommission({
- http,
- invoiceId: sale.invoiceId,
- expectedEarnings: E2E_SALE_REWARD.modifiers[3].amountInCents!,
- });
- });
+ await new Promise((resolve) => setTimeout(resolve, 3000));
- test("when {Customer} {Signup Date} is {greater than} {Feb 16, 2026} AND {less than} {Feb 18, 2026}", async () => {
- const sale = randomSale("E2E customer signup date condition");
+ await verifyCommission({
+ http,
+ invoiceId: sale.invoiceId,
+ expectedEarnings: E2E_SALE_REWARD.modifiers[5].amountInCents!,
+ });
- const trackSaleResponse = await http.post<TrackSaleResponse>({
- path: "/track/sale",
- body: {
- ...sale,
- customerExternalId: E2E_CUSTOMER_SIGNUP_DATE_CONDITIONS_EXTERNAL_ID,
- },
+ // no need to verify second sale since it will be verified below
+ // in the {Customer} {Subscription Duration} is {less than or equal to} {3} test
});
- expect(trackSaleResponse.status).toEqual(200);
+ test("when {Customer} {Subscription Duration} is {less than or equal to} {3}", async () => {
+ const sale = randomSale("E2E customer subscription duration condition");
+ const trackSaleResponse = await http.post<TrackSaleResponse>({
+ path: "/track/sale",
+ body: {
+ ...sale,
+ customerExternalId: newCustomer.externalId,
+ },
+ });
+
+ expect(trackSaleResponse.status).toEqual(200);
- await verifyCommission({
- http,
- invoiceId: sale.invoiceId,
- expectedEarnings: E2E_SALE_REWARD.modifiers[4].amountInCents!,
+ await verifyCommission({
+ http,
+ invoiceId: sale.invoiceId,
+ expectedEarnings: E2E_SALE_REWARD.modifiers[3].amountInCents!,
+ });
});
});
});
diff --git a/apps/web/tests/utils/resource.ts b/apps/web/tests/utils/resource.ts
index 11c2068577..170d13a364 100644
--- a/apps/web/tests/utils/resource.ts
+++ b/apps/web/tests/utils/resource.ts
@@ -134,6 +134,21 @@ export const E2E_SALE_REWARD = {
maxDuration: null,
amountInCents: 3300,
},
+ {
+ id: "5daf6243-afc0-4770-8bd1-8b2e4e97130d",
+ type: "flat",
+ operator: "AND",
+ conditions: [
+ {
+ value: "new",
+ entity: "sale",
+ operator: "equals_to",
+ attribute: "type",
+ },
+ ],
+ maxDuration: null,
+ amountInCents: 9000,
+ },
],
};
diff --git a/apps/web/ui/partners/rewards/rewards-logic.tsx b/apps/web/ui/partners/rewards/rewards-logic.tsx
index 948ed42fbf..ee1aefd822 100644
--- a/apps/web/ui/partners/rewards/rewards-logic.tsx
+++ b/apps/web/ui/partners/rewards/rewards-logic.tsx
@@ -322,9 +322,25 @@ function ConditionLogic({
// Auto-set operator to "equals_to" for customer.source
const isCustomerSourceCondition =
condition.entity === "customer" && condition.attribute === "source";
+ const isSaleTypeCondition =
+ condition.entity === "sale" && condition.attribute === "type";
+
+ const availableConditionOperators: (typeof CONDITION_OPERATORS)[number][] = [
+ "number",
+ "currency",
+ ].includes(attributeType)
+ ? NUMBER_CONDITION_OPERATORS
+ : attributeType === "enum"
+ ? ENUM_CONDITION_OPERATORS
+ : attributeType === "date"
+ ? DATE_CONDITION_OPERATORS
+ : STRING_CONDITION_OPERATORS;
useEffect(() => {
- if (isCustomerSourceCondition && condition.operator !== "equals_to") {
+ if (
+ (isCustomerSourceCondition || isSaleTypeCondition) &&
+ condition.operator !== "equals_to"
+ ) {
setValue(
conditionKey,
{
@@ -338,6 +354,7 @@ function ConditionLogic({
}
}, [
isCustomerSourceCondition,
+ isSaleTypeCondition,
condition.operator,
condition,
conditionKey,
@@ -417,7 +434,7 @@ function ConditionLogic({
}))}
/>
</InlineBadgePopover>{" "}
- {isCustomerSourceCondition ? (
+ {isCustomerSourceCondition || isSaleTypeCondition ? (
<span className="text-content-emphasis font-medium">is </span>
) : (
<InlineBadgePopover
@@ -459,14 +476,7 @@ function ConditionLogic({
},
)
}
- items={(["number", "currency"].includes(attributeType)
- ? NUMBER_CONDITION_OPERATORS
- : attributeType === "enum"
- ? ENUM_CONDITION_OPERATORS
- : attributeType === "date"
- ? DATE_CONDITION_OPERATORS
- : STRING_CONDITION_OPERATORS
- ).map((operator) => ({
+ items={availableConditionOperators.map((operator) => ({
text: CONDITION_OPERATOR_LABELS[operator],
value: operator,
}))}