File: /home/xedaptot/ai.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);
}
}