Skip to content

Conversation

@waleedlatif1
Copy link
Collaborator

Summary

  • added referrers, code redemption, campaign tracking, etc

Type of Change

  • New feature

Testing

Tested manually

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel
Copy link

vercel bot commented Feb 11, 2026

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

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped Feb 12, 2026 4:22am

Request Review

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 11, 2026

Greptile Overview

Greptile Summary

This PR implements a comprehensive referral and campaign tracking system with UTM attribution, manual code redemption, and bonus credit application. The implementation includes database schema changes, API endpoints, admin management tools, and frontend components. Additionally, it adds execution stream reconnection support and extends Confluence tool capabilities.

Key Changes:

  • Database schema adds referral_campaigns and referral_attribution tables with proper constraints and indexes
  • UTM parameters are captured via proxy middleware in sim_utm cookie with timestamp verification
  • Automatic attribution via /api/attribution matches campaigns by UTM specificity and applies bonus credits transactionally
  • Manual code redemption via /api/referral-code/redeem enforces one-per-user and one-per-org uniqueness
  • Admin APIs provide full CRUD operations for campaign management with proper pagination
  • Execution stream system now supports reconnection after page refresh using Redis-backed event buffering
  • Four new Confluence tools added: delete label, delete page property, get pages by label, and list space labels

Previous Review Comments Addressed:

  • userStats record creation moved inside transactions in both attribution endpoints (lines 162-165 in attribution/route.ts, lines 104-115 in redeem/route.ts)
  • Admin campaign listing now uses COUNT aggregation instead of fetching all rows (line 63 in referral-campaigns/route.ts)

Outstanding Issue:
The developer's previous response about getHighestPrioritySubscription not needing transaction context assumes no writes occur during the transaction. While this is currently true, the pattern violates transaction isolation best practices since applyBonusCredits promises to participate in the caller's transaction but reads subscription state outside it. This creates a potential race condition if subscription/member tables are modified concurrently.

Confidence Score: 4/5

  • This PR is generally safe to merge with one architectural concern around transaction isolation
  • The implementation is well-structured with proper error handling, validation, and transactional guarantees. Database migrations are correct, and previous review comments have been addressed. The score is 4 instead of 5 due to the getHighestPrioritySubscription call in applyBonusCredits not using the transaction context, which could lead to subtle race conditions under concurrent subscription modifications, though this is unlikely in practice
  • apps/sim/lib/billing/credits/bonus.ts requires attention for the transaction isolation pattern

Important Files Changed

Filename Overview
packages/db/schema.ts Added two new tables: referral_campaigns and referral_attribution with proper indexes and foreign key constraints for tracking referral codes and campaign attribution
apps/sim/app/api/attribution/route.ts UTM-based attribution endpoint with campaign matching logic, timestamp verification, and transactional bonus credit application - userStats is now created inside transaction
apps/sim/app/api/referral-code/redeem/route.ts Manual code redemption endpoint with user/org-level uniqueness checks and transactional credit application - userStats is now created inside transaction
apps/sim/lib/billing/credits/bonus.ts Bonus credit application logic with plan-based routing to either user or organization credit balances
apps/sim/proxy.ts Added UTM cookie capture logic in proxy middleware to track referral parameters with timestamp for attribution verification
apps/sim/lib/execution/event-buffer.ts New Redis-backed execution event buffering system for storing and retrieving execution stream events with TTL and batching
apps/sim/hooks/use-execution-stream.ts Improved execution stream handling with shared abort controllers across hook instances and reconnection support for page refreshes

Sequence Diagram

sequenceDiagram
    participant User
    participant Proxy
    participant Client as Client Hook
    participant AttrAPI as /api/attribution
    participant RedeemAPI as /api/referral-code/redeem
    participant DB as Database
    participant Bonus as applyBonusCredits

    Note over User,Proxy: UTM Attribution Flow
    User->>Proxy: Visit site with UTM params
    Proxy->>Proxy: Capture UTM + timestamp
    Proxy->>User: Set sim_utm cookie (1hr)
    User->>Client: Login/signup
    Client->>Client: Detect sim_utm cookie
    Client->>AttrAPI: POST /api/attribution
    AttrAPI->>DB: Verify user created after cookie
    AttrAPI->>DB: Find matching campaign
    AttrAPI->>DB: Start transaction
    AttrAPI->>DB: Insert referral_attribution (conflict: userId)
    AttrAPI->>Bonus: Apply bonus credits
    Bonus->>DB: Update creditBalance + usageLimit
    AttrAPI->>DB: Commit transaction
    AttrAPI->>Client: Return success + bonus amount
    Client->>Client: Delete sim_utm cookie

    Note over User,RedeemAPI: Manual Code Redemption Flow
    User->>RedeemAPI: POST /api/referral-code/redeem
    RedeemAPI->>DB: Find campaign by code
    RedeemAPI->>DB: Check existing user attribution
    RedeemAPI->>DB: Check org attribution (if team)
    RedeemAPI->>DB: Start transaction
    RedeemAPI->>DB: Insert referral_attribution (conflict: userId or orgId)
    RedeemAPI->>Bonus: Apply bonus credits
    Bonus->>DB: Update user/org creditBalance + usageLimit
    RedeemAPI->>DB: Commit transaction
    RedeemAPI->>User: Return success + bonus amount
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

17 files reviewed, 3 comments

Edit Code Review Agent Settings | Greptile

waleedlatif1 and others added 8 commits February 11, 2026 19:37
…3202)

* fix(confl): use recommended query param pattern for confluence route

* use unused var
)

* fix(terminal): reconnect to running executions after page refresh

* fix(terminal): use ExecutionEvent type instead of any in reconnection stream

* fix(execution): type event buffer with ExecutionEvent instead of Record<string, unknown>

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix(execution): validate fromEventId query param in reconnection endpoint

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* Fix some bugs

* fix(variables): fix tag dropdown and cursor alignment in variables block (#3199)

* feat(confluence): added list space labels, delete label, delete page prop (#3201)

* updated route

* ack comments

* fix(execution): reset execution state in reconnection cleanup to unblock re-entry

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix(execution): restore running entries when reconnection is interrupted by navigation

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* done

* remove cast in ioredis types

* ack PR comments

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: Siddharth Ganesan <siddharthganesan@gmail.com>
@waleedlatif1
Copy link
Collaborator Author

@greptile
@cursor review

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

62 files reviewed, 2 comments

Edit Code Review Agent Settings | Greptile

@waleedlatif1
Copy link
Collaborator Author

@greptile
@cursor review

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

62 files reviewed, 2 comments

Edit Code Review Agent Settings | Greptile

@waleedlatif1
Copy link
Collaborator Author

@greptile
@cursor review

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

62 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@waleedlatif1
Copy link
Collaborator Author

@cursor review
@greptile

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

62 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

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.

2 participants