Files
fewo-stommer/app/Models/Channel.php
T
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

171 lines
5.7 KiB
PHP

<?php
namespace App\Models;
use App\Channels\ChannelRegistry;
use App\Channels\Contracts\ChannelConnector;
use App\Enums\ChannelBookingModel;
use App\Enums\ChannelEchoBehavior;
use App\Enums\ChannelExportMode;
use App\Enums\ChannelStatus;
use App\Enums\TenantServiceKey;
use App\Models\Concerns\BelongsToTenant;
use App\Tenancy\TenantFeatures;
use Database\Factories\ChannelFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Hidden;
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;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Str;
/**
* A sales channel (portal) of a property. Our system leads: occupations are imported from the
* channel and every channel gets its own export feed without its own occupations.
*/
#[Fillable(['property_id', 'name', 'connector', 'settings', 'is_active', 'requires_manual_blocking', 'manage_url', 'status', 'booking_model', 'import_enabled', 'export_enabled', 'export_mode', 'external_poll_minutes', 'echo_behavior', 'echo_notes', 'operating_notes', 'portal_verified_at'])]
#[Hidden(['export_token', 'previous_export_token', 'export_snapshot', 'settings'])]
class Channel extends Model
{
/** @use HasFactory<ChannelFactory> */
use BelongsToTenant, HasFactory;
protected $attributes = [
'status' => 'setup', 'booking_model' => 'mixed', 'import_enabled' => false,
'export_enabled' => false, 'export_mode' => 'test',
'echo_behavior' => 'unverified',
];
protected static function booted(): void
{
static::creating(function (Channel $channel): void {
$channel->export_token ??= Str::random(48);
});
static::saving(function (Channel $channel): void {
if ($channel->isDirty('status')) {
$channel->is_active = $channel->status === ChannelStatus::Active;
} elseif ($channel->isDirty('is_active')) {
$channel->status = $channel->is_active ? ChannelStatus::Active : ChannelStatus::Paused;
}
});
}
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'settings' => 'array',
'status' => ChannelStatus::class,
'booking_model' => ChannelBookingModel::class,
'echo_behavior' => ChannelEchoBehavior::class,
'portal_verified_at' => 'immutable_datetime',
'export_mode' => ChannelExportMode::class,
'import_enabled' => 'boolean',
'export_enabled' => 'boolean',
'sync_warnings' => 'array',
'last_error_at' => 'immutable_datetime',
'export_snapshot_at' => 'immutable_datetime',
'last_export_requested_at' => 'immutable_datetime',
'previous_export_token_expires_at' => 'immutable_datetime',
'is_active' => 'boolean',
'requires_manual_blocking' => 'boolean',
'last_synced_at' => 'immutable_datetime',
'last_success_at' => 'immutable_datetime',
'alerted_at' => 'immutable_datetime',
];
}
/**
* @return BelongsTo<Property, $this>
*/
public function property(): BelongsTo
{
return $this->belongsTo(Property::class);
}
/**
* @return HasMany<BlockedPeriod, $this>
*/
public function blockedPeriods(): HasMany
{
return $this->hasMany(BlockedPeriod::class);
}
/**
* @return HasMany<ChannelSyncLog, $this>
*/
public function syncLogs(): HasMany
{
return $this->hasMany(ChannelSyncLog::class)->latest('id');
}
/**
* @return HasMany<ChannelTask, $this>
*/
public function tasks(): HasMany
{
return $this->hasMany(ChannelTask::class);
}
/**
* @param Builder<Channel> $query
* @return Builder<Channel>
*/
#[Scope]
protected function active(Builder $query): Builder
{
return $query->where('is_active', true)->where('status', ChannelStatus::Active);
}
public function connector(): ChannelConnector
{
return app(ChannelRegistry::class)->resolve($this->connector);
}
public function importsOccupations(): bool
{
return $this->is_active && $this->status === ChannelStatus::Active && $this->import_enabled
&& $this->servesItsLandlord() && $this->connector()->canImport($this);
}
public function exportsOccupations(): bool
{
return $this->is_active && $this->status === ChannelStatus::Active && $this->export_enabled && $this->servesItsLandlord();
}
/**
* Whether the landlord this channel belongs to has the portal synchronisation unlocked at all.
* Asked here, so that neither an import nor the export feed can run around the switch.
*/
public function servesItsLandlord(): bool
{
return app(TenantFeatures::class)->enabled(TenantServiceKey::PortalSync, $this->tenant);
}
public function exportUrl(): string
{
return route('channels.feed', ['token' => $this->export_token]);
}
public function setting(string $key): mixed
{
return $this->settings[$key] ?? null;
}
/**
* The import is considered stale when no successful sync happened within the threshold.
*/
public function isStale(): bool
{
return $this->importsOccupations()
&& ($this->last_success_at === null || $this->last_success_at->lessThan(now()->subMinutes(config('fewo.channels.stale_after_minutes'))));
}
}