244 lines
6.0 KiB
PHP
244 lines
6.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\DripEmailType;
|
|
use App\Notifications\VerifyEmailNotification;
|
|
use Database\Factories\UserFactory;
|
|
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
|
use Illuminate\Contracts\Translation\HasLocalePreference;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Concerns\HasUuids;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Illuminate\Notifications\Notification;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Laravel\Cashier\Billable;
|
|
use Laravel\Fortify\TwoFactorAuthenticatable;
|
|
use Laravel\Pennant\Concerns\HasFeatures;
|
|
|
|
class User extends Authenticatable implements HasLocalePreference, MustVerifyEmail
|
|
{
|
|
/** @use HasFactory<UserFactory> */
|
|
use Billable, HasFactory, HasFeatures, HasUuids, Notifiable, SoftDeletes, TwoFactorAuthenticatable;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
protected $fillable = [
|
|
'name',
|
|
'email',
|
|
'password',
|
|
'encryption_salt',
|
|
'onboarded_at',
|
|
'paywall_seen_at',
|
|
'currency_code',
|
|
'locale',
|
|
'timezone',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be hidden for serialization.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
protected $hidden = [
|
|
'password',
|
|
'two_factor_secret',
|
|
'two_factor_recovery_codes',
|
|
'remember_token',
|
|
];
|
|
|
|
/**
|
|
* Get the attributes that should be cast.
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'email_verified_at' => 'datetime',
|
|
'password' => 'hashed',
|
|
'two_factor_confirmed_at' => 'datetime',
|
|
'onboarded_at' => 'datetime',
|
|
'paywall_seen_at' => 'datetime',
|
|
];
|
|
}
|
|
|
|
public function isOnboarded(): bool
|
|
{
|
|
return $this->onboarded_at !== null;
|
|
}
|
|
|
|
public function hasSeenPaywall(): bool
|
|
{
|
|
return $this->paywall_seen_at !== null;
|
|
}
|
|
|
|
/** @return HasOne<UserSetting, $this> */
|
|
public function setting(): HasOne
|
|
{
|
|
return $this->hasOne(UserSetting::class);
|
|
}
|
|
|
|
/** @return HasOne<EncryptedMessage, $this> */
|
|
public function encryptedMessage(): HasOne
|
|
{
|
|
return $this->hasOne(EncryptedMessage::class);
|
|
}
|
|
|
|
/** @return HasMany<Transaction, $this> */
|
|
public function transactions(): HasMany
|
|
{
|
|
return $this->hasMany(Transaction::class);
|
|
}
|
|
|
|
/** @return HasMany<Account, $this> */
|
|
public function accounts(): HasMany
|
|
{
|
|
return $this->hasMany(Account::class);
|
|
}
|
|
|
|
/** @return HasMany<Category, $this> */
|
|
public function categories(): HasMany
|
|
{
|
|
return $this->hasMany(Category::class);
|
|
}
|
|
|
|
/** @return HasMany<Bank, $this> */
|
|
public function banks(): HasMany
|
|
{
|
|
return $this->hasMany(Bank::class)
|
|
->where(function (Builder $query) {
|
|
$query->whereNull('user_id')
|
|
->orWhere('banks.user_id', $this->id);
|
|
});
|
|
}
|
|
|
|
/** @return HasMany<AutomationRule, $this> */
|
|
public function automationRules(): HasMany
|
|
{
|
|
return $this->hasMany(AutomationRule::class);
|
|
}
|
|
|
|
/** @return HasMany<Label, $this> */
|
|
public function labels(): HasMany
|
|
{
|
|
return $this->hasMany(Label::class);
|
|
}
|
|
|
|
/** @return HasMany<UserMailLog, $this> */
|
|
public function mailLogs(): HasMany
|
|
{
|
|
return $this->hasMany(UserMailLog::class);
|
|
}
|
|
|
|
/** @return HasMany<Budget, $this> */
|
|
public function budgets(): HasMany
|
|
{
|
|
return $this->hasMany(Budget::class);
|
|
}
|
|
|
|
/** @return HasMany<BankingConnection, $this> */
|
|
public function bankingConnections(): HasMany
|
|
{
|
|
return $this->hasMany(BankingConnection::class);
|
|
}
|
|
|
|
public function hasReceivedEmail(DripEmailType $type): bool
|
|
{
|
|
return $this->mailLogs()->where('email_type', $type)->exists();
|
|
}
|
|
|
|
public function hasProPlan(): bool
|
|
{
|
|
if (! config('subscriptions.enabled')) {
|
|
return true;
|
|
}
|
|
|
|
return $this->subscribed('default');
|
|
}
|
|
|
|
/**
|
|
* The tax rates that should apply to the customer's subscriptions.
|
|
*
|
|
* @return array<int, string>
|
|
*/
|
|
public function taxRates(): array
|
|
{
|
|
return config('subscriptions.tax_rates', []);
|
|
}
|
|
|
|
public function isDemoAccount(): bool
|
|
{
|
|
return $this->email === config('app.demo.email');
|
|
}
|
|
|
|
public function preferredLocale(): string
|
|
{
|
|
return $this->locale ?? 'en';
|
|
}
|
|
|
|
public function isDeleted(): bool
|
|
{
|
|
return $this->trashed();
|
|
}
|
|
|
|
public function markAsDeleted(): void
|
|
{
|
|
if ($this->trashed()) {
|
|
return;
|
|
}
|
|
|
|
DB::transaction(function () {
|
|
$this->forceFill([
|
|
'email' => $this->deletedEmail(),
|
|
])->saveQuietly();
|
|
|
|
$this->delete();
|
|
});
|
|
}
|
|
|
|
public function canReceiveEmails(): bool
|
|
{
|
|
return ! $this->isDeleted();
|
|
}
|
|
|
|
public function routeNotificationForMail(?Notification $notification = null): ?string
|
|
{
|
|
if (! $this->canReceiveEmails()) {
|
|
return null;
|
|
}
|
|
|
|
return $this->email;
|
|
}
|
|
|
|
public function sendEmailVerificationNotification(): void
|
|
{
|
|
if (! $this->canReceiveEmails()) {
|
|
return;
|
|
}
|
|
|
|
$this->notify(new VerifyEmailNotification);
|
|
}
|
|
|
|
private function deletedEmail(): string
|
|
{
|
|
$timestamp = $this->freshTimestamp()->format('YmdHis');
|
|
$originalEmail = $this->getOriginal('email');
|
|
$candidate = "{$timestamp}_{$originalEmail}";
|
|
|
|
if (! static::withTrashed()->where('email', $candidate)->exists()) {
|
|
return $candidate;
|
|
}
|
|
|
|
return "{$timestamp}_{$this->getKey()}_{$originalEmail}";
|
|
}
|
|
}
|