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 more reward conditions e2e tests

Steven Tey0dd91d18c8da
diff --git a/apps/web/tests/rewards/sale-reward.test.ts b/apps/web/tests/rewards/sale-reward.test.ts
index 27bdf0ced1..9464534b50 100644
--- a/apps/web/tests/rewards/sale-reward.test.ts
+++ b/apps/web/tests/rewards/sale-reward.test.ts
@@ -1,65 +1,134 @@
import { TrackSaleResponse } from "@/lib/types";
-import { randomId } from "tests/utils/helpers";
-import { E2E_CUSTOMERS, E2E_SALE_REWARD } from "tests/utils/resource";
+import { randomId, randomSaleAmount } from "tests/utils/helpers";
+import {
+ E2E_CUSTOMER_EXTERNAL_ID_2,
+ E2E_CUSTOMER_ID,
+ E2E_CUSTOMER_SG_EXTERNAL_ID,
+ E2E_SALE_REWARD,
+} from "tests/utils/resource";
import { verifyCommission } from "tests/utils/verify-commission";
import { describe, expect, test } from "vitest";
import { IntegrationHarness } from "../utils/integration";
-describe.concurrent("Sale rewards", async () => {
+describe.concurrent("Sale rewards with conditions", async () => {
const h = new IntegrationHarness();
const { http } = await h.init();
+ const randomSale = () => ({
+ eventName: "Subscription",
+ currency: "usd",
+ paymentProcessor: "stripe",
+ amount: randomSaleAmount(),
+ invoiceId: `INV_${randomId()}`,
+ });
+
+ test("When {Sale} {Product ID} is {regularProductId}", async () => {
+ const sale = randomSale();
+
+ const response = await http.post<TrackSaleResponse>({
+ path: "/track/sale",
+ body: {
+ ...sale,
+ customerExternalId: E2E_CUSTOMER_EXTERNAL_ID_2,
+ metadata: {
+ productId: "regularProductId",
+ },
+ },
+ });
+
+ expect(response.status).toEqual(200);
+
+ await verifyCommission({
+ http,
+ invoiceId: sale.invoiceId,
+ expectedEarnings: E2E_SALE_REWARD.amountInCents,
+ });
+ });
+
+ test("When {Sale} {Product ID} is {premiumProductId}", async () => {
+ const sale = randomSale();
+
+ const response = await http.post<TrackSaleResponse>({
+ path: "/track/sale",
+ body: {
+ ...sale,
+ customerExternalId: E2E_CUSTOMER_EXTERNAL_ID_2,
+ metadata: {
+ productId: "premiumProductId",
+ },
+ },
+ });
+
+ expect(response.status).toEqual(200);
+
+ await verifyCommission({
+ http,
+ invoiceId: sale.invoiceId,
+ expectedEarnings: E2E_SALE_REWARD.modifiers[0].amountInCents!,
+ });
+ });
+
+ test("When {Sale} {Amount} is greater than {15000}", async () => {
+ const sale = randomSale();
+
+ const response = await http.post<TrackSaleResponse>({
+ path: "/track/sale",
+ body: {
+ ...sale,
+ amount: 17500,
+ customerExternalId: E2E_CUSTOMER_EXTERNAL_ID_2,
+ metadata: {
+ productId: "premiumProductId",
+ },
+ },
+ });
+
+ expect(response.status).toEqual(200);
+
+ await verifyCommission({
+ http,
+ invoiceId: sale.invoiceId,
+ expectedEarnings: E2E_SALE_REWARD.modifiers[1].amountInCents!,
+ });
+ });
+
test("when {Customer} {Country} is {SG}", async () => {
- const saleAmount = 10000; // $100 in cents
- const invoiceId = `INV_${randomId()}`;
+ const sale = randomSale();
- // Track the sale
const trackSaleResponse = await http.post<TrackSaleResponse>({
path: "/track/sale",
body: {
- customerExternalId: E2E_CUSTOMERS[0].externalId,
- eventName: "Subscription",
- amount: saleAmount,
- currency: "usd",
- invoiceId,
- paymentProcessor: "stripe",
+ ...sale,
+ customerExternalId: E2E_CUSTOMER_SG_EXTERNAL_ID,
},
});
expect(trackSaleResponse.status).toEqual(200);
- // Verify the commission (10% of sale amount)
await verifyCommission({
http,
- invoiceId,
- expectedEarnings: saleAmount * 0.1,
+ invoiceId: sale.invoiceId,
+ expectedEarnings: E2E_SALE_REWARD.modifiers[2].amountInCents!,
});
});
- test.skip("when {Customer} {Country} is {CA}", async () => {
- const saleAmount = 10000; // $100 in cents
- const invoiceId = `INV_${randomId()}`;
+ test("when {Customer} {Subscription Duration} is greater than {12}", async () => {
+ const sale = randomSale();
- // Track the sale
const trackSaleResponse = await http.post<TrackSaleResponse>({
path: "/track/sale",
body: {
- customerExternalId: E2E_CUSTOMERS[1].externalId,
- eventName: "Subscription",
- amount: saleAmount,
- currency: "usd",
- invoiceId,
- paymentProcessor: "stripe",
+ ...sale,
+ customerExternalId: E2E_CUSTOMER_ID,
},
});
expect(trackSaleResponse.status).toEqual(200);
- // Verify the commission (base reward)
await verifyCommission({
http,
- invoiceId,
- expectedEarnings: E2E_SALE_REWARD.amountInCents,
+ invoiceId: sale.invoiceId,
+ expectedEarnings: E2E_SALE_REWARD.modifiers[3].amountInCents!,
});
});
});
diff --git a/apps/web/tests/tracks/track-sale.test.ts b/apps/web/tests/tracks/track-sale.test.ts
index ea55faf1fd..0e3cd7a238 100644
--- a/apps/web/tests/tracks/track-sale.test.ts
+++ b/apps/web/tests/tracks/track-sale.test.ts
@@ -77,58 +77,6 @@ describe.concurrent("POST /track/sale", async () => {
expectValidSaleResponse(response, sale);
});
- test("track a sale with regular vs premium product ID (should create the right commission)", async () => {
- const regularInvoiceId = `INV_${randomId()}`;
- const premiumInvoiceId = `INV_${randomId()}`;
-
- const [response1, response2] = await Promise.all([
- http.post<TrackSaleResponse>({
- path: "/track/sale",
- body: {
- ...sale,
- amount: 2000,
- customerExternalId: E2E_CUSTOMER_EXTERNAL_ID_2,
- invoiceId: regularInvoiceId,
- metadata: {
- productId: "regularProductId",
- },
- },
- }),
-
- http.post<TrackSaleResponse>({
- path: "/track/sale",
- body: {
- ...sale,
- amount: 3000,
- customerExternalId: E2E_CUSTOMER_EXTERNAL_ID_2,
- invoiceId: premiumInvoiceId,
- metadata: {
- productId: "premiumProductId",
- },
- },
- }),
- ]);
-
- expect(response1.status).toEqual(200);
- expect(response2.status).toEqual(200);
-
- await Promise.all([
- verifyCommission({
- http,
- invoiceId: regularInvoiceId,
- expectedAmount: response1.data.sale?.amount!,
- expectedEarnings: E2E_SALE_REWARD.amountInCents,
- }),
-
- verifyCommission({
- http,
- invoiceId: premiumInvoiceId,
- expectedAmount: response2.data.sale?.amount!,
- expectedEarnings: E2E_SALE_REWARD.modifiers[0].amountInCents!,
- }),
- ]);
- });
-
test("track a sale with an externalId that does not exist (should return null customer and sale)", async () => {
const response = await http.post<TrackSaleResponse>({
path: "/track/sale",
diff --git a/apps/web/tests/utils/resource.ts b/apps/web/tests/utils/resource.ts
index 94685c462e..995d6ffe9c 100644
--- a/apps/web/tests/utils/resource.ts
+++ b/apps/web/tests/utils/resource.ts
@@ -28,6 +28,9 @@ export const E2E_TAG_2 = {
export const E2E_CUSTOMER_ID = "cm25onzuv0001s1bbxchrc0ae";
export const E2E_CUSTOMER_EXTERNAL_ID = "cus_jTrfVKYN3Buc3F80JoqBiY0g";
+export const E2E_CUSTOMER_EXTERNAL_ID_2 = "cus_pqc8qRtofpu6ZqvutyNDGAU2";
+export const E2E_CUSTOMER_SG_EXTERNAL_ID = "cus_LnZbkb8boLsOn1YGLPxZGZMU";
+
export const E2E_WEBHOOK_ID = "wh_MHR7sZXXtZ7keBaNYZ30rQ0v";
// Folders specific
@@ -38,7 +41,6 @@ export const E2E_READ_ONLY_FOLDER_LINK_ID = "link_1KAESR5Z733716RTT4E1RSTW6"; //
export const E2E_NO_ACCESS_FOLDER_LINK_ID = "link_1KAESQ2Z6Q35WDV5NGSEVPFB0"; // A link in no access folder
// Rewards specific
-export const E2E_CUSTOMER_EXTERNAL_ID_2 = "cus_pqc8qRtofpu6ZqvutyNDGAU2";
export const E2E_SALE_REWARD = {
id: "rw_1JYPP77NNDG6TVPAJDKNZREQN",
event: "sale",
@@ -46,6 +48,7 @@ export const E2E_SALE_REWARD = {
amountInCents: 1000,
modifiers: [
{
+ id: "02f54268-bb03-4bc5-b7cb-4353ec3459e3",
type: "flat",
operator: "AND",
conditions: [
@@ -60,6 +63,7 @@ export const E2E_SALE_REWARD = {
amountInCents: 3000,
},
{
+ id: "d91651b8-f14f-4f1a-9749-c523e618f8fb",
type: "flat",
operator: "AND",
conditions: [
@@ -74,32 +78,34 @@ export const E2E_SALE_REWARD = {
amountInCents: 5000,
},
{
- type: "percentage",
+ id: "249b049f-a476-45fb-8ecf-71f989fc2a8a",
+ type: "flat",
operator: "AND",
conditions: [
{
- value: "US",
+ value: "SG",
entity: "customer",
operator: "equals_to",
attribute: "country",
},
],
maxDuration: null,
- amountInPercentage: 10,
+ amountInCents: 6500,
},
{
+ id: "c12b3226-accc-4ed1-a322-09f4310910d3",
type: "flat",
operator: "AND",
conditions: [
{
- value: "CA",
+ value: 12,
entity: "customer",
- operator: "equals_to",
- attribute: "country",
+ operator: "greater_than",
+ attribute: "subscriptionDurationMonths",
},
],
maxDuration: null,
- amountInCents: 50,
+ amountInCents: 500,
},
],
};
@@ -194,18 +200,11 @@ export const E2E_PARTNERS = [
},
] as const;
-export const E2E_CUSTOMERS = [
- {
- id: "cus_1K82FYFF7RANMCGRHRGMWDNEC",
- externalId: "cus_LnZbkb8boLsOn1YGLPxZGZMU",
- country: "SG",
- },
- {
- id: "cus_1K86CG1DZFW8EMSSWXX4AVZFA",
- externalId: "cus_vq3UgXINHS99MIon8vNvAO1n",
- country: "CA",
- },
-] as const;
+export const E2E_CUSTOMER_SG = {
+ id: "cus_1K82FYFF7RANMCGRHRGMWDNEC",
+ externalId: "cus_LnZbkb8boLsOn1YGLPxZGZMU",
+ country: "SG",
+};
export const E2E_FRAUD_PARTNER = {
id: "pn_1K8ND11BZ4XPEX39QX3YMBGY0",