Files
fewo-stommer/app/Models/ChannelTask.php
Lars 783068501c
Deploy FeWo mit PipelineManager / deploy (push) Successful in 2m33s
Generische iCal-Kanäle mit Bestandsklärung und sicheren Exporten bereitstellen
2026-10-27 17:14:45 +01:00

88 lines
2.8 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Models;
use App\Availability\AvailabilityChecker;
use App\Enums\ChannelTaskAction;
use App\Enums\ChannelTaskStatus;
use App\Models\Concerns\BelongsToTenant;
use Database\Factories\ChannelTaskFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* A change that has to be made by hand (or by an external automation) on a portal that
* cannot be kept in sync fast enough by calendar feeds, e.g. because of instant booking.
*/
#[Fillable(['channel_id', 'action', 'starts_on', 'ends_on', 'reason', 'status', 'completed_by', 'completed_at', 'reminded_at', 'automation_sent_at', 'automation_error'])]
class ChannelTask extends Model
{
/** @use HasFactory<ChannelTaskFactory> */
use BelongsToTenant, HasFactory;
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'action' => ChannelTaskAction::class,
'status' => ChannelTaskStatus::class,
'starts_on' => 'immutable_date',
'ends_on' => 'immutable_date',
'completed_at' => 'immutable_datetime',
'reminded_at' => 'immutable_datetime',
'automation_sent_at' => 'immutable_datetime',
];
}
/**
* @return BelongsTo<Channel, $this>
*/
public function channel(): BelongsTo
{
return $this->belongsTo(Channel::class);
}
/**
* @param Builder<ChannelTask> $query
* @return Builder<ChannelTask>
*/
#[Scope]
protected function open(Builder $query): Builder
{
return $query->where('status', ChannelTaskStatus::Open);
}
public function complete(string $completedBy): void
{
if ($this->status !== ChannelTaskStatus::Open) {
return;
}
if ($this->action === ChannelTaskAction::Unblock && ! app(AvailabilityChecker::class)->isAvailable($this->channel->property, $this->starts_on, $this->ends_on)) {
$this->update(['status' => ChannelTaskStatus::Dismissed, 'automation_error' => 'Aktuell belegt – Entsperraufgabe nicht mehr gültig.']);
return;
}
$this->update([
'status' => ChannelTaskStatus::Done,
'completed_by' => $completedBy,
'completed_at' => now(),
]);
}
/**
* Human readable period, e.g. "14.06.2027 – 21.06.2027".
*/
public function period(): string
{
return $this->starts_on->format('d.m.Y').' – '.$this->ends_on->format('d.m.Y');
}
}