Files
Scheduler/app/Models/Bot.php
Oskar-Mikael 378355ad5b
All checks were successful
Deploy App / deploy (push) Successful in 11s
Add bot logs
2025-08-31 21:54:18 +02:00

70 lines
1.4 KiB
PHP

<?php
namespace App\Models;
use Cron\CronExpression;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Lorisleiva\CronTranslator\CronTranslator;
class Bot extends Model
{
protected $fillable = [
'name',
'class',
'config',
'schedule',
'enabled',
'user_id',
];
protected $casts = [
'config' => 'array',
];
public function nextDue(): Attribute
{
return Attribute::make(
get: function () {
$cron = new CronExpression($this->schedule);
return $cron->getNextRunDate()->format('Y-m-d H:i:s');
}
);
}
public function cronToHuman(): Attribute
{
return Attribute::make(
get: fn() => CronTranslator::translate($this->schedule)
);
}
public function className(): Attribute
{
return Attribute::make(
get: fn() => class_basename($this->class)
);
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function logs()
{
return $this->hasMany(BotLog::class);
}
public function latestLog()
{
return $this->hasOne(BotLog::class)->latestOfMany();
}
public function failedLogs()
{
return $this->hasMany(BotLog::class)->where('status', 'failed');
}
}