- Connector-System (app/Channels, config/channels.php) mit iCal-Connector, robustem Parser und Export-Feed je Portal (ohne eigene Belegungen, ohne Gästedaten) - Abgleich alle 5 Min. mit Protokoll, Echo-Erkennung, Konflikt-Mail bei möglicher Doppelbuchung - Überwachung: Störungs-Mail, Status-Endpunkt /health/sync für n8n - Portal-Aufgaben für Sofortbuchungsportale (FeWo-direkt): automatisch aus Buchungen/Sperren/anderen Portalen, Mail + Erinnerung, Abhaken im Admin - Optionaler signierter Automatisierungs-Webhook mit Rückmeldung - Herkunftsmessung: Abfrage im Formular + UTM-Parameter - Portal-Analyse: AGB-Prüfung Automatisierung, Umsetzungsbeschreibung Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
124 lines
3.3 KiB
PHP
124 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Channels\ChannelRegistry;
|
|
use App\Channels\Contracts\ChannelConnector;
|
|
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'])]
|
|
#[Hidden(['export_token'])]
|
|
class Channel extends Model
|
|
{
|
|
/** @use HasFactory<ChannelFactory> */
|
|
use HasFactory;
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (Channel $channel): void {
|
|
$channel->export_token ??= Str::random(48);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Get the attributes that should be cast.
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'settings' => 'array',
|
|
'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);
|
|
}
|
|
|
|
public function connector(): ChannelConnector
|
|
{
|
|
return app(ChannelRegistry::class)->resolve($this->connector);
|
|
}
|
|
|
|
public function importsOccupations(): bool
|
|
{
|
|
return $this->is_active && $this->connector()->canImport($this);
|
|
}
|
|
|
|
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'))));
|
|
}
|
|
}
|