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

Skip to content

Conversation

@steven-tey
Copy link
Collaborator

@steven-tey steven-tey commented Oct 21, 2025

CleanShot 2025-10-20 at 18 11 35

Summary by CodeRabbit

  • New Features

    • Partner country is now captured in reward context, enabling country-based reward modifiers.
  • Tests

    • Added unit tests for partner country-based reward conditions (US, CA, null, multi-value).
    • Added end-to-end lead reward verification covering partner country flows.
    • Updated test fixtures: sale reward renamed and a new lead reward fixture added.

@vercel
Copy link
Contributor

vercel bot commented Oct 21, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Updated (UTC)
dub Ready Ready Preview Oct 21, 2025 4:58am

💡 Enable Vercel Agent with $100 free credit for automated AI reviews

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 21, 2025

Walkthrough

Loads partner on program enrollment calls and threads partner.country into reward evaluation and schemas, plus adds tests and test fixtures to validate partner country-based reward conditions and end-to-end lead/sale commission assertions.

Changes

Cohort / File(s) Change Summary
API endpoints & migration scripts
apps/web/app/(ee)/api/commissions/[commissionId]/route.ts, apps/web/scripts/migrations/backfill-click-commissions.ts, apps/web/scripts/tella/update-commissions.ts
Add partner: true to the include passed to getProgramEnrollmentOrThrow, causing the partner relation (including country) to be loaded with program enrollment.
Backfill utility
apps/web/lib/api/partners/backfill-link-commissions.ts
Adds partner: true to getProgramEnrollmentOrThrow include when backfilling link commissions.
Partner reward logic
apps/web/lib/partners/determine-partner-reward.ts
Extend ProgramEnrollmentWithReward to include partner.country and inject programEnrollment.partner?.country into the reward context for modifier condition evaluation.
Zod reward schemas
apps/web/lib/zod/schemas/rewards.ts
Add country to CONDITION_PARTNER_ATTRIBUTES, include country in ENTITY_ATTRIBUTE_TYPES for customer/partner, and extend rewardContextSchema.partner with country: z.string().nullish().
Tests — reward conditions
apps/web/tests/rewards/reward-conditions.test.ts
Add "partner country conditions" tests covering equals_to, in, and null scenarios for partner.country (US, CA, null).
Tests — tracking E2E
apps/web/tests/tracks/track-lead.test.ts, apps/web/tests/tracks/track-sale.test.ts, apps/web/tests/utils/resource.ts
track-lead.test.ts: add commission verification for lead flows using partner.country (US) and helper to assert commission earnings. track-sale.test.ts: switch E2E_REWARDE2E_SALE_REWARD. tests/utils/resource.ts: rename E2E_REWARDE2E_SALE_REWARD and add E2E_LEAD_REWARD with country-US modifier.

Sequence Diagram(s)

sequenceDiagram
    participant API as Track / Commission API
    participant Service as ProgramEnrollment Service
    participant Reward as Reward Evaluator
    participant DB as Commissions DB

    rect rgb(230,247,255)
    API->>Service: getProgramEnrollmentOrThrow(id, include: { links, saleReward, partner: true, ... })
    Service-->>API: programEnrollment (includes partner with country)
    end

    rect rgb(245,255,230)
    API->>Reward: evaluatePartnerReward(programEnrollment, event)
    Note right of Reward: rewardContext.partner.country populated
    Reward-->>API: rewardDecision (modifiers matched by country)
    end

    rect rgb(255,250,230)
    API->>DB: createCommission(rewardDecision, enrollment, event)
    DB-->>API: commission record
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • steven-tey

Poem

🐰 I hop where countries join the chain,
Partner.country tucked in the gain,
Modifiers match and rewards take flight,
Tests give thumbs-up in morning light,
Hooray — commissions bloom tonight! 🥕

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The pull request title "Add partner.country to reward conditions" directly and accurately summarizes the main objective of the changeset. The changes across all modified files focus on enabling partner country as a condition attribute for reward evaluation, including adding partner data fetching, extending zod schemas to include country attributes, and adding test coverage for country-based reward conditions. The title is specific and clear—a developer scanning history would immediately understand that this PR introduces country-based partner reward conditions without ambiguity or vagueness.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch partner-country-reward

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between dd2cb56 and 1640090.

📒 Files selected for processing (3)
  • apps/web/tests/tracks/track-lead.test.ts (4 hunks)
  • apps/web/tests/tracks/track-sale.test.ts (3 hunks)
  • apps/web/tests/utils/resource.ts (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
apps/web/tests/tracks/track-sale.test.ts (1)
apps/web/tests/utils/resource.ts (1)
  • E2E_SALE_REWARD (41-71)
apps/web/tests/tracks/track-lead.test.ts (2)
apps/web/lib/types.ts (3)
  • Customer (417-417)
  • CommissionResponse (434-434)
  • TrackLeadResponse (413-413)
apps/web/tests/utils/resource.ts (2)
  • E2E_LEAD_REWARD (72-92)
  • E2E_TRACK_CLICK_HEADERS (10-14)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: build
🔇 Additional comments (6)
apps/web/tests/utils/resource.ts (2)

41-41: LGTM: Improved naming clarity.

Renaming E2E_REWARD to E2E_SALE_REWARD makes the purpose explicit and aligns with the addition of E2E_LEAD_REWARD.


72-92: LGTM: Well-structured test fixture for partner country conditions.

The new E2E_LEAD_REWARD constant properly supports testing the partner.country reward condition feature with appropriate base and modifier amounts.

apps/web/tests/tracks/track-sale.test.ts (1)

11-11: LGTM: Consistent refactoring to use renamed constant.

All references properly updated from E2E_REWARD to E2E_SALE_REWARD following the rename in resource.ts.

Also applies to: 137-137, 143-143, 318-318, 325-325

apps/web/tests/tracks/track-lead.test.ts (3)

1-9: LGTM: Necessary imports for commission verification.

All new imports are properly utilized in the verifyCommission helper and the new test case.


102-109: LGTM: Proper commission verification with base reward amount.

The addition verifies that the base reward amount (1000) is correctly applied when no modifiers match. The 2-second delay accommodates async processing.


264-302: The test assumes "marvin" link is associated with a US-based partner, but this dependency is undocumented and unverified.

The test expects commission earnings of 200 (from E2E_LEAD_REWARD.modifiers[0].amount), which triggers only when partner.country === "US". However, the test makes no explicit verification that the "marvin" link is owned by a US partner. The link appears to be pre-seeded in the test database outside this codebase, making the test brittle and dependent on external data setup.

Recommendation: Either add an explicit assertion verifying the partner's country before checking earnings, or document that "marvin" is intentionally seeded as a US partner link.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

♻️ Duplicate comments (1)
apps/web/tests/tracks/track-lead.test.ts (1)

43-50: Add validation for empty customer array before accessing customers[0].

The code accesses customers[0] without verifying the array is non-empty, which will cause a runtime error if no customer matches the externalId.

Apply this diff to add the missing assertion:

 const { data: customers } = await http.get<Customer[]>({
   path: "/customers",
   query: {
     externalId: customerExternalId,
   },
 });
 
+expect(customers).toHaveLength(1);
 const customer = customers[0];

Based on learnings

🧹 Nitpick comments (1)
apps/web/tests/tracks/track-lead.test.ts (1)

289-289: Hardcoded sleep duration may cause flaky tests.

The 2-second wait is arbitrary and could fail if commission processing takes longer in CI environments or under load.

Consider implementing a polling mechanism with timeout instead:

// Helper to poll for commission with retry
const waitForCommission = async (
  http: HttpClient,
  customerExternalId: string,
  maxAttempts = 10,
  delayMs = 500
) => {
  for (let i = 0; i < maxAttempts; i++) {
    const { data: customers } = await http.get<Customer[]>({
      path: "/customers",
      query: { externalId: customerExternalId },
    });
    
    if (customers.length > 0) {
      const { data: commissions } = await http.get<CommissionResponse[]>({
        path: "/commissions",
        query: { customerId: customers[0].id },
      });
      
      if (commissions.length > 0) {
        return commissions[0];
      }
    }
    
    await new Promise((resolve) => setTimeout(resolve, delayMs));
  }
  throw new Error("Commission not found after polling");
};
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1640090 and 87b0047.

📒 Files selected for processing (1)
  • apps/web/tests/tracks/track-lead.test.ts (3 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
apps/web/tests/tracks/track-lead.test.ts (2)
apps/web/lib/types.ts (3)
  • Customer (417-417)
  • CommissionResponse (434-434)
  • TrackLeadResponse (413-413)
apps/web/tests/utils/resource.ts (2)
  • E2E_TRACK_CLICK_HEADERS (10-14)
  • E2E_LEAD_REWARD (72-92)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: build
🔇 Additional comments (1)
apps/web/tests/tracks/track-lead.test.ts (1)

258-296: Verify test assumptions and expected earnings calculation.

This test has several assumptions that should be verified:

  1. Partner country mapping: The test uses the "marvin" link and expects US partner behavior. Confirm that "marvin" is explicitly configured with country: "US" in the test fixtures or setup.

  2. Earnings calculation: The test expects E2E_LEAD_REWARD.modifiers[0].amount (200), but E2E_LEAD_REWARD has a base amount: 1000 plus a modifier with amount: 200. Verify whether the expected earnings should be:

    • Just the modifier: 200 (current implementation)
    • Base + modifier: 1200
    • Base amount only when conditions don't match

Run the following script to verify the "marvin" link configuration:

@steven-tey steven-tey merged commit 1c15cba into main Oct 21, 2025
7 checks passed
@steven-tey steven-tey deleted the partner-country-reward branch October 21, 2025 05:02
This was referenced Oct 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants