146 lines
3.6 KiB
PHP
146 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use DateTimeInterface;
|
|
use EloquentFilter\Filterable;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Laravel\Sanctum\HasApiTokens;
|
|
|
|
class User extends Authenticatable
|
|
{
|
|
use HasApiTokens, HasFactory, Notifiable, Filterable;
|
|
|
|
const GENDERS = [
|
|
'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.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
|
|
protected $guarded = [];
|
|
|
|
/**
|
|
* The attributes that should be hidden for serialization.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $hidden = [
|
|
'password',
|
|
'remember_token',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'email_verified_at' => 'datetime',
|
|
];
|
|
|
|
protected $appends = [
|
|
'full_name'
|
|
];
|
|
|
|
public function getFullNameAttribute()
|
|
{
|
|
return $this->first_name . ' ' . $this->last_name;
|
|
}
|
|
|
|
public function getAvatarAttribute()
|
|
{
|
|
return isset($this->attributes['avatar']) ? url($this->attributes['avatar']) : null;
|
|
}
|
|
|
|
public function setPasswordAttribute($password)
|
|
{
|
|
$this->attributes['password'] = Hash::make($password);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
public function packageNames()
|
|
{
|
|
return $this->belongsToMany(PackageName::class, 'user_package_name')->withPivot(['id', 'tries','fcm_token'])->withTimestamps();
|
|
}
|
|
|
|
public function products()
|
|
{
|
|
return $this->belongsToMany(Product::class, 'user_product')->withPivot(['expire_at', 'purchase_token', 'gateway'])->withTimestamps();
|
|
}
|
|
|
|
protected function serializeDate(DateTimeInterface $date)
|
|
{
|
|
return $date->format('Y-m-d H:i:s');
|
|
}
|
|
public function plans()
|
|
{
|
|
return $this->hasMany(Plan::class);
|
|
}
|
|
// public function response()
|
|
// {
|
|
// return $this->hasOne(UserResponse::class);
|
|
// }
|
|
|
|
public function breathingExercises()
|
|
{
|
|
return $this->hasMany(BreathingExercise::class);
|
|
}
|
|
public function breathingSessions()
|
|
{
|
|
return $this->hasMany(UserBreathingSession::class, 'user_id');
|
|
}
|
|
|
|
public function savedMedia()
|
|
{
|
|
return $this->belongsToMany(Media::class, 'saved_media')->withTimestamps();
|
|
}
|
|
|
|
} |