feat: add /api/referrals endpoints with per-endpoint audit logging - #731
Merged
greatest0fallt1me merged 3 commits intoJul 29, 2026
Merged
Conversation
- Add referrals table to DB schema (id, user_id, referral_code, campaign_id,
referred_user, status, created_at) with indexes on user_id and referral_code
- Add referralService with createReferral and listUserReferrals functions
- Add POST /api/referrals (create referral code) with audit logging:
- Writes audit row via createAuditLog with action=referral.create,
beforeState=null, afterState={referralCode, campaignId}
- Captures actor (walletAddress), IP, correlationId
- Add GET /api/referrals (list user referrals)
- Add migration 0004_add_referrals.sql
- Mount referralsRouter at /api/referrals in src/index.ts
- Add 14 focused tests for auth guard, validation, happy path with audit
assertions, rate limiting, and error propagation
Closes Predictify-org#627
|
@PHADAR6 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
Contributor
|
LGTM ✅ green CI, clean work — merging! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
This PR adds a full
/api/referralsendpoint suite with structured audit logging for all mutations. The implementation follows the established pattern from/api/indexer/cursor— injectable dependencies for testability, rate limiting, auth guards, and audit rows persisted toaudit_logswith before/after state snapshots.Related Issue
Closes #627
Changes
🗄️ Database
referralstable insrc/db/schema.ts— columns:id,user_id,referral_code,campaign_id,referred_user,status,created_atwith indexes onuser_idandreferral_codedrizzle/0004_add_referrals.sql— SQL migration creating thereferralstable🔧 Backend Services
src/services/referralService.ts—createReferral()generates uniqueREF-XXXX-XXXXcodes and persists them;listUserReferrals()returns paginated user referralssrc/services/auditService.tsintegration — every POST mutation writes an audit row viacreateAuditLog🛣️ Routes
src/routes/referrals.ts—createReferralsRouterwith:POST /api/referrals(requires auth, rate-limited at 60 req/min)GET /api/referrals(requires auth, rate-limited at 60 req/min)action: "referral.create",beforeState: null,afterState: { referralCode, campaignId }, actor, IP, and correlationId🔗 Integration
src/index.ts— registeredreferralsRouterat/api/referralsVerification Results
action: "referral.create"with before/after statebeforeState: null,afterState: { referralCode, campaignId }Timeline