feat: add referral

This commit is contained in:
2026-06-06 13:10:53 +03:30
parent c930c26589
commit 9080cf866e
6 changed files with 144 additions and 7 deletions
+32
View File
@@ -18,6 +18,12 @@ class User extends Authenticatable
'male' => 1,
'female' => 2,
];
// Referral program (کد معرف)
const REFERRAL_POINTS_PER_INVITE = 100; // points granted to the referrer per successful invite
const REFERRAL_SHARE = 0.10; // referrer keeps 10% of each friend's earned xp, forever
const REFERRAL_SUBSCRIPTION_TARGET = 10; // successful invites needed for the free-month milestone
/**
* The attributes that are mass assignable.
*
@@ -69,6 +75,32 @@ public function otpTokens()
return $this->hasMany(OtpTokens::class);
}
public function referrer()
{
return $this->belongsTo(User::class, 'referred_by');
}
public function referrals()
{
return $this->hasMany(User::class, 'referred_by');
}
/**
* Award xp to this user and, if they were referred, credit the
* referrer their permanent 10% share of the earned points.
*/
public function awardXp(int $amount): void
{
$this->increment('xp', $amount);
if ($this->referred_by) {
$share = (int) floor($amount * self::REFERRAL_SHARE);
if ($share > 0) {
self::where('id', $this->referred_by)->increment('referral_points', $share);
}
}
}
public function transactions()
{
return $this->hasMany(Transaction::class);