HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: /home/xedaptot/be.naniguide.com/app/Model/AdActivityLog.php
<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Library\Traits\HasUid;

class AdActivityLog extends Model
{
    use HasFactory;
    use HasUid;

    const STATUS_SUCCESS = 'success';
    const STATUS_WARNING = 'warning';
    const STATUS_ERROR = 'error';
    const STATUS_INFO = 'info';

    public $timestamps = false;

    protected $table = 'ad_activity_logs';

    protected $fillable = [
        'uid',
        'customer_id',
        'admin_id',
        'user_id',
        'loggable_type',
        'loggable_id',
        'action',
        'status',
        'title',
        'description',
        'error_code',
        'error_message',
        'resolution_hint',
        'metadata',
        'platform',
        'is_read_customer',
        'is_read_admin',
        'created_at',
    ];

    protected $casts = [
        'metadata' => 'array',
        'is_read_customer' => 'boolean',
        'is_read_admin' => 'boolean',
        'created_at' => 'datetime',
    ];

    protected static function newFactory()
    {
        return \Database\Factories\AdActivityLogFactory::new();
    }

    protected static function booted(): void
    {
        static::creating(function ($log) {
            if (!$log->created_at) {
                $log->created_at = now();
            }
        });
    }

    // Relations

    public function loggable()
    {
        return $this->morphTo();
    }

    public function customer()
    {
        return $this->belongsTo(Customer::class);
    }

    // Scopes

    public function scopeOfCustomer($query, $customer)
    {
        return $query->where('customer_id', $customer->id);
    }

    public function scopeErrors($query)
    {
        return $query->where('status', self::STATUS_ERROR);
    }

    public function scopeWarnings($query)
    {
        return $query->where('status', self::STATUS_WARNING);
    }

    public function scopeUnreadByCustomer($query)
    {
        return $query->where('is_read_customer', false);
    }

    public function scopeUnreadByAdmin($query)
    {
        return $query->where('is_read_admin', false);
    }

    public function scopeOfPlatform($query, string $platform)
    {
        return $query->where('platform', $platform);
    }

    public function scopeOfEntity($query, string $type)
    {
        return $query->where('loggable_type', $type);
    }

    public function scopeSearch($query, ?string $keyword)
    {
        if ($keyword) {
            $query->where(function ($q) use ($keyword) {
                $q->where('title', 'like', "%{$keyword}%")
                  ->orWhere('description', 'like', "%{$keyword}%");
            });
        }

        return $query;
    }

    // Helpers

    public function isError(): bool
    {
        return $this->status === self::STATUS_ERROR;
    }

    public function isWarning(): bool
    {
        return $this->status === self::STATUS_WARNING;
    }

    public function hasResolutionHint(): bool
    {
        return !empty($this->resolution_hint);
    }
}