Spaces:
Running
Running
File size: 4,427 Bytes
907b200 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | <?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use App\Models\Researcher;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Laravel\Cashier\Billable;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable, Billable;
protected $fillable = [
'name',
'email',
'password',
'role',
'email_verification_otp',
'email_verification_otp_expires_at',
'is_verified',
'password_reset_otp',
'password_reset_otp_expires_at',
'last_otp_sent_at',
'current_plan_id',
];
protected $hidden = [
'password',
'remember_token',
'email_verification_otp',
'password_reset_otp',
];
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'email_verification_otp_expires_at' => 'datetime',
'password_reset_otp_expires_at' => 'datetime',
'is_verified' => 'boolean',
'last_otp_sent_at' => 'datetime',
];
}
public function researcher()
{
return $this->hasOne(Researcher::class);
}
public function isEmailOtpValid(string $otp): bool
{
return $this->email_verification_otp === $otp
&& $this->email_verification_otp_expires_at
&& $this->email_verification_otp_expires_at->isFuture();
}
public function isPasswordResetOtpValid(string $otp): bool
{
return $this->password_reset_otp === $otp
&& $this->password_reset_otp_expires_at
&& $this->password_reset_otp_expires_at->isFuture();
}
public function clearEmailOtp(): void
{
$this->update([
'email_verification_otp' => null,
'email_verification_otp_expires_at' => null,
]);
}
public function clearPasswordResetOtp(): void
{
$this->update([
'password_reset_otp' => null,
'password_reset_otp_expires_at' => null,
]);
}
public function aiJobs()
{
return $this->hasMany(AiJob::class);
}
public function chemistryThreads(): HasMany
{
return $this->hasMany(ChemistryThread::class);
}
public function chemistryAnalyses(): HasMany
{
return $this->hasMany(ChemistryAnalysis::class);
}
public function chemistryCsvJobs(): HasMany
{
return $this->hasMany(ChemistryCsvJob::class);
}
public function currentPlan(): BelongsTo
{
return $this->belongsTo(Plan::class, 'current_plan_id');
}
public function isFree(): bool
{
return $this->currentPlan?->type === 'free';
}
public function isPro(): bool
{
return $this->currentPlan?->type === 'pro';
}
public function isMax(): bool
{
return $this->currentPlan?->type === 'max';
}
public function hasActiveSubscription(): bool
{
return $this->subscribed('default');
}
protected static function booted(): void
{
static::created(function (User $user) {
$freePlan = Plan::where(
'type',
'free'
)->first();
if ($freePlan) {
$user->update([
'current_plan_id' => $freePlan->id,
]);
}
});
}
public function ensureHasPlan(): void
{
if ($this->current_plan_id !== null) {
return;
}
$freePlan = Plan::where('type', 'free')->first();
if ($freePlan) {
$this->update([
'current_plan_id' => $freePlan->id,
]);
}
}
public function syncCurrentPlan(): void
{
$subscription = $this->subscription('default');
if (! $subscription) {
return;
}
$plan = Plan::where(
'stripe_price_id',
$subscription->stripe_price
)->first();
if ($plan && $this->current_plan_id !== $plan->id) {
$this->update([
'current_plan_id' => $plan->id,
]);
}
}
}
|