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.

Add subscription duration reward modifier

Kiran Kf35b1c83d6c0
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts
index 2a9b17e824..73e3de2fc1 100644
--- a/apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts
+++ b/apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts
@@ -463,6 +463,7 @@ export async function checkoutSessionCompleted(
context: {
customer: {
country: customer.country,
+ firstSaleAt: customer.firstSaleAt,
},
sale: {
productId,
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/invoice-paid.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/invoice-paid.ts
index 2de879db5f..db042db496 100644
--- a/apps/web/app/(ee)/api/stripe/integration/webhook/invoice-paid.ts
+++ b/apps/web/app/(ee)/api/stripe/integration/webhook/invoice-paid.ts
@@ -232,6 +232,7 @@ export async function invoicePaid(event: Stripe.Event, mode: StripeMode) {
context: {
customer: {
country: customer.country,
+ firstSaleAt: customer.firstSaleAt,
},
sale: {
productId: invoice.lines.data[0]?.pricing?.price_details?.product,
diff --git a/apps/web/lib/actions/partners/create-manual-commission.ts b/apps/web/lib/actions/partners/create-manual-commission.ts
index 59ec4ee631..ce4ed1c0d4 100644
--- a/apps/web/lib/actions/partners/create-manual-commission.ts
+++ b/apps/web/lib/actions/partners/create-manual-commission.ts
@@ -292,8 +292,13 @@ export const createManualCommissionAction = authActionClient
createdAt: new Date(saleEventData.timestamp + "Z"), // add the "Z" to the timestamp to make it UTC
user,
context: {
- customer: { country: customer.country },
- sale: { productId },
+ customer: {
+ country: customer.country,
+ firstSaleAt: customer.firstSaleAt,
+ },
+ sale: {
+ productId,
+ },
},
})),
);
diff --git a/apps/web/lib/api/conversions/track-sale.ts b/apps/web/lib/api/conversions/track-sale.ts
index f2c4e8c855..fb12dd1037 100644
--- a/apps/web/lib/api/conversions/track-sale.ts
+++ b/apps/web/lib/api/conversions/track-sale.ts
@@ -552,10 +552,11 @@ const _trackSale = async ({
context: {
customer: {
country: customer.country,
- source: source!,
+ source,
+ firstSaleAt: customer.firstSaleAt,
},
sale: {
- productId: metadata?.productId as string,
+ productId: metadata?.productId,
amount: saleData.amount,
},
},
diff --git a/apps/web/lib/integrations/shopify/create-sale.ts b/apps/web/lib/integrations/shopify/create-sale.ts
index b06d39ad95..4023a534f5 100644
--- a/apps/web/lib/integrations/shopify/create-sale.ts
+++ b/apps/web/lib/integrations/shopify/create-sale.ts
@@ -150,6 +150,7 @@ export async function createShopifySale({
context: {
customer: {
country: customer.country,
+ firstSaleAt: customer.firstSaleAt,
},
sale: {
amount: saleData.amount,
diff --git a/apps/web/lib/partners/determine-partner-reward.ts b/apps/web/lib/partners/determine-partner-reward.ts
index e2f31820b1..bf715809ca 100644
--- a/apps/web/lib/partners/determine-partner-reward.ts
+++ b/apps/web/lib/partners/determine-partner-reward.ts
@@ -1,4 +1,5 @@
import { EventType, Link, Prisma, Reward } from "@dub/prisma/client";
+import { differenceInMonths } from "date-fns";
import { serializeReward } from "../api/partners/serialize-reward";
import { RewardContext } from "../types";
import {
@@ -51,6 +52,13 @@ export const determinePartnerReward = ({
totalCommissions: programEnrollment.totalCommissions,
country: programEnrollment.partner?.country,
},
+ customer: {
+ ...context?.customer,
+ subscriptionDuration: differenceInMonths(
+ new Date(),
+ context?.customer?.firstSaleAt ?? new Date(),
+ ),
+ },
};
if (partnerReward.modifiers && context) {
diff --git a/apps/web/lib/zod/schemas/rewards.ts b/apps/web/lib/zod/schemas/rewards.ts
index 17205b9ba1..c572c68b29 100644
--- a/apps/web/lib/zod/schemas/rewards.ts
+++ b/apps/web/lib/zod/schemas/rewards.ts
@@ -164,6 +164,11 @@ export const REWARD_CONDITIONS: Record<
label: "Amount",
type: "currency",
},
+ {
+ id: "subscriptionDuration",
+ label: "Subscription duration",
+ type: "number",
+ },
],
},
],
@@ -347,6 +352,8 @@ export const rewardContextSchema = z.object({
.object({
country: z.string().nullish(),
source: z.enum(CUSTOMER_SOURCES).default("tracked").nullish(),
+ firstSaleAt: z.date().nullish(),
+ subscriptionDuration: z.number().nullish(),
})
.optional(),
diff --git a/apps/web/tests/rewards/reward-conditions.test.ts b/apps/web/tests/rewards/reward-conditions.test.ts
index f644dad294..b7558ed37e 100644
--- a/apps/web/tests/rewards/reward-conditions.test.ts
+++ b/apps/web/tests/rewards/reward-conditions.test.ts
@@ -1660,4 +1660,70 @@ describe("evaluateRewardConditions", () => {
expect(result).toBeNull(); // Should not match when country is null
});
});
+
+ describe("subscription duration conditions", () => {
+ describe("customer.subscriptionDuration", () => {
+ test("should match when subscription duration meets greater_than_or_equal condition", () => {
+ const conditions = [
+ {
+ operator: "AND" as const,
+ type: "flat" as const,
+ amountInCents: 5000,
+ conditions: [
+ {
+ entity: "customer" as const,
+ attribute: "subscriptionDuration" as const,
+ operator: "greater_than_or_equal" as const,
+ value: 12,
+ },
+ ],
+ },
+ ];
+
+ const context: RewardContext = {
+ customer: {
+ subscriptionDuration: 12,
+ },
+ };
+
+ const result = evaluateRewardConditions({
+ conditions,
+ context,
+ });
+
+ expect(result).toEqual(conditions[0]);
+ });
+
+ test("should not match when subscription duration is less than condition value", () => {
+ const conditions = [
+ {
+ operator: "AND" as const,
+ type: "flat" as const,
+ amountInCents: 5000,
+ conditions: [
+ {
+ entity: "customer" as const,
+ attribute: "subscriptionDuration" as const,
+ operator: "greater_than_or_equal" as const,
+ value: 12,
+ },
+ ],
+ },
+ ];
+
+ const context: RewardContext = {
+ customer: {
+ subscriptionDuration: 6,
+ },
+ };
+
+ const result = evaluateRewardConditions({
+ conditions,
+ context,
+ });
+
+ expect(result).toBe(null);
+ });
+ });
+ });
});