Skip to content

Lead and sales rewards metadata condition#4032

Open
pepeladeira wants to merge 1 commit into
mainfrom
lead-sales-rewards-metadata
Open

Lead and sales rewards metadata condition#4032
pepeladeira wants to merge 1 commit into
mainfrom
lead-sales-rewards-metadata

Conversation

@pepeladeira

@pepeladeira pepeladeira commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

Release Notes

  • New Features

    • Added support for metadata-based reward conditions, allowing rewards to be conditioned on lead and sale metadata fields.
    • Introduced metadata field operators for reward condition evaluation with proper type coercion.
    • Added QSTASH_URL environment variable configuration.
  • Tests

    • Added comprehensive test coverage for metadata conditions, including operator edge cases and field validation.
  • Chores

    • Updated environment configuration in workflows and example files.

@vercel

vercel Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

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

Project Deployment Actions Updated (UTC)
dub Ready Ready Preview Jun 11, 2026 8:43pm

Request Review

@coderabbitai

coderabbitai Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This PR adds complete support for metadata-based reward conditions in the partner rewards system. It introduces a new lead entity type and metadata attribute, propagates metadata through Stripe webhook handlers and conversion tracking, implements condition evaluation with proper type coercion, and provides UI components and comprehensive test coverage for the new functionality.

Changes

Metadata-Based Reward Conditions Feature

Layer / File(s) Summary
Environment and Workflow Configuration
.github/workflows/e2e.yaml, .github/workflows/playwright.yaml, apps/web/.env.example
QSTASH_URL environment variable added to e2e workflows and example configuration for test infrastructure.
Reward Condition Schema and Type Definitions
apps/web/lib/zod/schemas/rewards.ts
Schema extended to support lead entity, metadata attribute type, and validation rules. New exports: condition attribute/ID collections, metadata operator groupings, and context schema with optional metadata for lead/sale.
Metadata Context Propagation to Commission Creation
apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts, apps/web/app/(ee)/api/stripe/integration/webhook/invoice-paid.ts, apps/web/lib/api/conversions/track-lead.ts, apps/web/lib/api/conversions/track-sale.ts
Enriches partner commission creation payloads with metadata extracted from Stripe webhooks (charge metadata, merged invoice metadata) and conversion tracking (lead/sale metadata).
Metadata Condition Evaluation
apps/web/lib/partners/evaluate-reward-conditions.ts
Implements metadata field resolution, value type coercion based on operator requirements (numeric vs text), and tightened edge-case handling for empty strings, arrays, and missing values.
UI Components and Form Validation
apps/web/ui/partners/rewards/rewards-logic.tsx, apps/web/ui/partners/rewards/add-edit-reward-sheet.tsx, apps/web/ui/partners/program-reward-modifiers-tooltip.tsx
Adds metadata field-name input, dedicated metadata operator menu, value input adaptation for numeric/text metadata, tooltip rendering of metadata field names, and base schema integration for partial form validation.
Test Coverage
apps/web/tests/rewards/reward-conditions.test.ts
Comprehensive tests for metadata condition evaluation with type coercion, schema validation of metadata attribute/entity rules, edge cases (empty values/fields), and operator behavior (contains/not_contains/starts_with/ends_with/not_in).

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • dubinc/dub#3824: Modifies Stripe webhook handlers to change context.sale payload structure for partner commission creation, affecting the commission context construction layer.

Suggested reviewers

  • steven-tey

Poem

🐰 A hop through metadata fields we go,
Lead and sale conditions now can flow,
With coerced types and operators keen,
The prettiest reward rules we've seen!
Tests and UI dance in sync so true,
Dub's partnerships just leveled up, wahoo!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Lead and sales rewards metadata condition' accurately describes the main change—adding metadata condition support for lead and sales reward entities—and reflects the primary objective of the changeset.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch lead-sales-rewards-metadata

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install timed out. The project may have too many dependencies for the sandbox.


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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
apps/web/ui/partners/program-reward-modifiers-tooltip.tsx (1)

179-220: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Render numeric 0 condition values in tooltip text.

At Line 182, condition.value && ... suppresses valid falsy operands like 0 (e.g., greater_than 0), so the tooltip can render an incomplete condition.

Suggested fix
-                  {condition.value &&
+                  {condition.value !== undefined &&
+                    condition.value !== null &&
+                    (typeof condition.value !== "string" ||
+                      condition.value.trim() !== "") &&
                     (condition.attribute === "country"
                       ? // Country names
                         Array.isArray(condition.value)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@apps/web/ui/partners/program-reward-modifiers-tooltip.tsx` around lines 179 -
220, The tooltip logic is skipping falsy but valid values like 0 because it uses
"condition.value && ..."; update the check to explicitly allow 0 by replacing
that truthy check with an explicit null/undefined check (e.g., condition.value
!== undefined && condition.value !== null, or condition.value != null) before
the big rendering expression so all the downstream branches (country lookup
using COUNTRIES, subscription formatting via formatSubscriptionDuration, product
label handling, Array.isArray branches using attribute?.options,
currencyFormatter, formatDateTime, and options lookup on attribute) still run
for numeric 0 or other valid falsy values; keep the rest of the rendering logic
and symbols (CONDITION_OPERATOR_LABELS, COUNTRIES, formatSubscriptionDuration,
currencyFormatter, formatDateTime, attribute) unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@apps/web/.env.example`:
- Line 32: The QSTASH_URL variable is quoted and triggers dotenv-linter's
QuoteCharacter rule; update the QSTASH_URL entry (the QSTASH_URL key in the .env
example) to use an unquoted URL value (remove the surrounding double quotes) so
the line reads a bare URL and passes the linter.

---

Outside diff comments:
In `@apps/web/ui/partners/program-reward-modifiers-tooltip.tsx`:
- Around line 179-220: The tooltip logic is skipping falsy but valid values like
0 because it uses "condition.value && ..."; update the check to explicitly allow
0 by replacing that truthy check with an explicit null/undefined check (e.g.,
condition.value !== undefined && condition.value !== null, or condition.value !=
null) before the big rendering expression so all the downstream branches
(country lookup using COUNTRIES, subscription formatting via
formatSubscriptionDuration, product label handling, Array.isArray branches using
attribute?.options, currencyFormatter, formatDateTime, and options lookup on
attribute) still run for numeric 0 or other valid falsy values; keep the rest of
the rendering logic and symbols (CONDITION_OPERATOR_LABELS, COUNTRIES,
formatSubscriptionDuration, currencyFormatter, formatDateTime, attribute)
unchanged.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 7834f79f-5668-4266-bea9-f64a458aa69d

📥 Commits

Reviewing files that changed from the base of the PR and between c15eae7 and 8bc444a.

📒 Files selected for processing (13)
  • .github/workflows/e2e.yaml
  • .github/workflows/playwright.yaml
  • apps/web/.env.example
  • apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts
  • apps/web/app/(ee)/api/stripe/integration/webhook/invoice-paid.ts
  • apps/web/lib/api/conversions/track-lead.ts
  • apps/web/lib/api/conversions/track-sale.ts
  • apps/web/lib/partners/evaluate-reward-conditions.ts
  • apps/web/lib/zod/schemas/rewards.ts
  • apps/web/tests/rewards/reward-conditions.test.ts
  • apps/web/ui/partners/program-reward-modifiers-tooltip.tsx
  • apps/web/ui/partners/rewards/add-edit-reward-sheet.tsx
  • apps/web/ui/partners/rewards/rewards-logic.tsx

Comment thread apps/web/.env.example

# Upstash QStash – required for queues and background jobs
# Get your QStash Token here: https://upstash.com/docs/qstash/overall/getstarted
QSTASH_URL="https://qstash-us-east-1.upstash.io"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Remove unnecessary quotes from QSTASH_URL to satisfy dotenv-linter.

Line 32 is flagged by dotenv-linter (QuoteCharacter). Use an unquoted URL value to avoid lint noise.

Suggested fix
-QSTASH_URL="https://qstash-us-east-1.upstash.io"
+QSTASH_URL=https://qstash-us-east-1.upstash.io
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
QSTASH_URL="https://qstash-us-east-1.upstash.io"
QSTASH_URL=https://qstash-us-east-1.upstash.io
🧰 Tools
🪛 dotenv-linter (4.0.0)

[warning] 32-32: [QuoteCharacter] The value has quote characters (', ")

(QuoteCharacter)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@apps/web/.env.example` at line 32, The QSTASH_URL variable is quoted and
triggers dotenv-linter's QuoteCharacter rule; update the QSTASH_URL entry (the
QSTASH_URL key in the .env example) to use an unquoted URL value (remove the
surrounding double quotes) so the line reads a bare URL and passes the linter.

Source: Linters/SAST tools

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.

1 participant