Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {
},
sale: {
productId,
amount: saleData.amount,
},
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ export async function invoicePaid(event: Stripe.Event) {
},
sale: {
productId: invoice.lines.data[0]?.pricing?.price_details?.product,
amount: saleData.amount,
},
},
});
Expand Down
1 change: 1 addition & 0 deletions apps/web/lib/api/conversions/track-sale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ const _trackSale = async ({
},
sale: {
productId: metadata?.productId as string,
amount: saleData.amount,
},
},
});
Expand Down
3 changes: 3 additions & 0 deletions apps/web/lib/integrations/shopify/create-sale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ export async function createShopifySale({
customer: {
country: customer.country,
},
sale: {
amount: saleData.amount,
},
},
});
webhookPartner = createdCommission?.webhookPartner;
Expand Down
7 changes: 6 additions & 1 deletion apps/web/lib/zod/schemas/rewards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const CONDITION_ENTITIES = ["customer", "sale", "partner"] as const;

export const CONDITION_CUSTOMER_ATTRIBUTES = ["country"] as const;

export const CONDITION_SALE_ATTRIBUTES = ["productId"] as const;
export const CONDITION_SALE_ATTRIBUTES = ["productId", "amount"] as const;

export const CONDITION_PARTNER_ATTRIBUTES = [
"totalClicks",
Expand Down Expand Up @@ -53,6 +53,9 @@ export const ENTITY_ATTRIBUTE_TYPES: Partial<
totalSaleAmount: "currency",
totalCommissions: "currency",
},
sale: {
amount: "currency",
},
};

export const CONDITION_OPERATORS = [
Expand Down Expand Up @@ -84,6 +87,7 @@ export const NUMBER_CONDITION_OPERATORS: (typeof CONDITION_OPERATORS)[number][]
export const ATTRIBUTE_LABELS = {
country: "Country",
productId: "Product ID",
amount: "Amount",
totalClicks: "Total clicks",
totalLeads: "Total leads",
totalConversions: "Total conversions",
Expand Down Expand Up @@ -199,6 +203,7 @@ export const rewardContextSchema = z.object({
sale: z
.object({
productId: z.string().nullish(),
amount: z.number().nullish(),
})
.optional(),

Expand Down
53 changes: 53 additions & 0 deletions apps/web/tests/tracks/track-sale.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,57 @@ describe("POST /track/sale", async () => {
},
});
});

test("track sales with different amounts (verifies sale amount is passed via context for reward conditions)", async () => {
// Test with a small sale amount
const smallSaleInvoiceId = `INV_${randomId()}`;
const smallSaleAmount = 10000; // $100.00

const response1 = await http.post<TrackSaleResponse>({
path: "/track/sale",
body: {
...sale,
amount: smallSaleAmount,
customerExternalId: E2E_CUSTOMER_EXTERNAL_ID_2,
invoiceId: smallSaleInvoiceId,
},
});

expect(response1.status).toEqual(200);
expect(response1.data.sale?.amount).toEqual(smallSaleAmount);

// Test with a large sale amount
const largeSaleInvoiceId = `INV_${randomId()}`;
const largeSaleAmount = 20000; // $200.00

const response2 = await http.post<TrackSaleResponse>({
path: "/track/sale",
body: {
...sale,
amount: largeSaleAmount,
customerExternalId: E2E_CUSTOMER_EXTERNAL_ID_2,
invoiceId: largeSaleInvoiceId,
},
});

expect(response2.status).toEqual(200);
expect(response2.data.sale?.amount).toEqual(largeSaleAmount);

// Pause for 2 seconds for data to be fully processed
await new Promise((resolve) => setTimeout(resolve, 2000));

await verifyCommission(
http,
smallSaleInvoiceId,
response1.data.sale?.amount!,
E2E_REWARD.amount,
);

await verifyCommission(
http,
largeSaleInvoiceId,
response2.data.sale?.amount!,
E2E_REWARD.modifiers[1].amount,
);
});
});
12 changes: 12 additions & 0 deletions apps/web/tests/utils/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ export const E2E_REWARD = {
},
],
},
{
operator: "AND",
amount: 5000,
conditions: [
{
entity: "sale",
attribute: "amount",
operator: "greater_than",
value: 15000,
},
],
},
],
};

Expand Down