Referral System Technical Documentation
Referral System Technical Documentation
Overview
The referral system enables users to earn rewards by referring new users to the platform. When
a referred user completes transactions, the referrer receives a bonus equal to 5% of the service
charge for each transaction. This document outlines the technical implementation details of the
referral system.
Architecture
The referral system is integrated with the existing transaction flow, specifically with the escrow
transaction process. The system consists of the following components:
Database Schema
User Table Modifications
prisma
model user {
// Existing fields
referral_code String? @unique @db.VarChar(10)
referred_by BigInt? @db.UnsignedBigInt
referrer user? @relation("UserReferrals", fields:
[referred_by], references: [id])
referrals user[] @relation("UserReferrals")
}
The user_referral_bonus table has been modified to handle all transaction types:
prisma
model user_referral_bonus {
id BigInt @id @default(autoincrement())
@db.UnsignedBigInt
referrer BigInt @db.UnsignedBigInt
referee BigInt @db.UnsignedBigInt
transaction_id String
transaction_type String // ESCROW, WALLET_TRANSFER, etc.
service_charge Decimal @db.Decimal(10, 2)
bonus_amount Decimal @db.Decimal(10, 2)
status String @default("completed")
created_at DateTime @default(now())
completed_at DateTime?
currency String @db.VarChar(3)
// Relations
referrer_user user
@relation("user_referral_bonus_referrerTouser", fields: [referrer],
references: [id])
referee_user user
@relation("user_referral_bonus_refereeTouser", fields: [referee],
references: [id])
}
Core Services
ReferralService
1. Generating Referral Codes: Creates unique referral codes for users
2. Tracking Referrals: Connects new users with their referrers during sign-up
3. Reporting: Provides views of referred users and bonus history
WalletService
1. Processing Referral Bonuses: Calculates and awards bonuses based on transaction
service charges
2. Crediting Wallets: Updates wallet balances with earned referral bonuses
Implementation Details
Referral Flow
Integration Points
The referral system integrates with the existing codebase at the following points:
typescript
// In handleCompletedStatus of EscrowService
// Process referral bonus for both buyer and seller
await Promise.all([
// Process buyer's referral bonus
this.walletService.processReferralBonus(
BigInt(user.id),
escrow.amount,
serviceCharge,
escrow.unique_identifier
),
// Process seller's referral bonus
this.walletService.processReferralBonus(
BigInt(payload.sellerId),
escrow.amount,
serviceCharge,
escrow.unique_identifier
)
]);
API Endpoints
Referral Code
POST /referral/generate-code
Generates a new referral code for the user if one doesn't exist.
Request Body:
json
{
"userId": "123456"
}
Response:
json
{
"referralCode": "XYZ12345"
}
Referred Users
GET /referral/referred-users/:userId
Parameters:
Response:
json
{
"users": [
{
"id": "987654",
"fullName": "John Doe",
"username": "johndoe",
"signupDate": "2024-02-15T14:30:00Z",
"totalTransactions": 5
}
],
"pagination": {
"total": 25,
"pages": 3,
"currentPage": 1,
"limit": 10
}
}
Bonus History
GET /referral/bonus-history/:userId
Parameters:
Response:
{
"bonuses": [
{
"id": "123",
"amount": 25.50,
"currency": "NGN",
"transactionType": "ESCROW",
"transactionId": "TXN123456",
"referredUser": {
"username": "johndoe",
"fullName": "John Doe"
},
"date": "2024-02-20T10:15:00Z"
}
],
"pagination": {
"total": 15,
"pages": 2,
"currentPage": 1,
"limit": 10
}
}
Total Bonus
GET /referral/total-bonus/:userId
Returns the total referral bonus earned by the user, grouped by currency.
Parameters:
Response:
json
{
"totalReferrals": 25,
"bonuses": [
{
"currency": "NGN",
"total": 15000.00
},
{
"currency": "USD",
"total": 100.00
}
]
}
Bonus Calculation
The referral bonus is calculated as follows:
Example:
Security Considerations
1. Referral Code Uniqueness: Each referral code is guaranteed to be unique via database
constraints
2. Validation: Referral codes are validated during sign-up
3. Error Handling: Referral processing is designed to be non-blocking for the main
transaction flow
4. Idempotency: The system prevents duplicate bonus awards for the same transaction
Future Enhancements
1. Tiered Referral Program: Different bonus rates based on user activity or tier
2. Time-Limited Campaigns: Special referral rates during promotional periods
3. Multi-level Referrals: Extending the program to second-level referrals
4. Notifications: Alerting users when they earn referral bonuses
5. Analytics Dashboard: Providing users with visual analytics of their referral performance
Migration Plan
The migration to implement the referral system requires: