|
| 1 | +# Referral System |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +Referral rewards are paid to the referrer based on real successful payments from referred users. |
| 6 | + |
| 7 | +- Reward rate: `20%` of payment (`2000` basis points) |
| 8 | +- Referred user reward: none |
| 9 | +- Behavior flag: `RECURRING_REFERRAL_REWARD` |
| 10 | + - `true`: reward every successful eligible payment |
| 11 | + - `false`: reward only once per referral |
| 12 | + |
| 13 | +## Main Rules |
| 14 | + |
| 15 | +- Each user has one unique referral code. |
| 16 | +- A user can apply a code only before their first confirmed payment. |
| 17 | +- Self-referral is blocked (user id + identity fields). |
| 18 | +- Rewarding is idempotent: |
| 19 | + - same payment cannot reward twice |
| 20 | + - non-recurring mode allows only one reward for that referral |
| 21 | + |
| 22 | +## Data Model |
| 23 | + |
| 24 | +### `ReferralCode` (`referralcodes`) |
| 25 | + |
| 26 | +- `userId` (unique) |
| 27 | +- `referralCode` (unique) |
| 28 | + |
| 29 | +### `Referral` (`referrals`) |
| 30 | + |
| 31 | +- `referrerId` |
| 32 | +- `referredUserId` (unique) |
| 33 | +- `referralCode` |
| 34 | +- `status`: `pending | converted` |
| 35 | +- `sourceIp`, `deviceFingerprint`, `convertedAt` |
| 36 | + |
| 37 | +### `ReferralTransaction` (`referraltransactions`) |
| 38 | + |
| 39 | +- `referralId`, `referrerId`, `referredUserId` |
| 40 | +- `sourcePaymentId`, `sourcePaymentGateway`, `sourcePaymentObjectId` |
| 41 | +- `paymentAmountUsd`, `rewardAmountUsd`, `rewardRate` (Decimal128) |
| 42 | +- `creditTransactionId` |
| 43 | +- `idempotencyKey` (unique) |
| 44 | + |
| 45 | +## Migration |
| 46 | + |
| 47 | +Migration file: `src/migrations/1744200000000-referral-system.ts` |
| 48 | + |
| 49 | +What it does: |
| 50 | + |
| 51 | +- creates indexes for referral collections |
| 52 | +- backfills missing referral codes for existing users |
| 53 | +- backfills `Referral` records from legacy `user.referral.invitedBy` |
| 54 | +- supports `users` and `tgusers` collections |
| 55 | + |
| 56 | +### Run migration (non-interactive) |
| 57 | + |
| 58 | +```bash |
| 59 | +npx ts-migrate-mongoose up -f src/migrate.ts -a true |
| 60 | +``` |
| 61 | + |
| 62 | +### Check migration status |
| 63 | + |
| 64 | +```bash |
| 65 | +npm run migrate:list |
| 66 | +``` |
| 67 | + |
| 68 | +### Roll back referral migration (if needed) |
| 69 | + |
| 70 | +```bash |
| 71 | +npm run migrate:down |
| 72 | +``` |
| 73 | + |
| 74 | +## Core Services |
| 75 | + |
| 76 | +- `src/services/referral/referralCodeService.ts` |
| 77 | + - ensures and fetches user referral codes |
| 78 | +- `src/services/referral/referralService.ts` |
| 79 | + - apply code, enforce eligibility, return referral stats |
| 80 | +- `src/services/referral/referralRewardService.ts` |
| 81 | + - compute reward in cents, award credits, upsert audit transaction, mark referral converted |
| 82 | + |
| 83 | +## API |
| 84 | + |
| 85 | +- `GET /referral/stats` |
| 86 | + - returns code, referral link, totals, and referral rows |
| 87 | +- `POST /referral/apply` |
| 88 | + - request: `{ "code": "ABCD1234", "deviceFingerprint": "optional" }` |
| 89 | + - supports `x-device-fingerprint` header |
| 90 | + |
| 91 | +## Payment Integration |
| 92 | + |
| 93 | +Reward processing is triggered on successful payment flows in: |
| 94 | + |
| 95 | +- `src/controllers/payment/coinbase/webhook.ts` |
| 96 | +- `src/controllers/payment/stripe/handleWebhook.ts` |
| 97 | + |
| 98 | +## Config |
| 99 | + |
| 100 | +`RECURRING_REFERRAL_REWARD` is read via `nconf`. Truthy values: `true`, `1`, `yes`. |
| 101 | + |
| 102 | +## Tests |
| 103 | + |
| 104 | +Key test: `src/services/referral/__tests__/referralRewardService.test.ts` |
| 105 | + |
| 106 | +- non-recurring behavior |
| 107 | +- recurring behavior |
| 108 | +- payment idempotency |
0 commit comments