Die Schalter aus Phase 3 schrieben nur in eine Tabelle, die niemand las. Ein gesperrter Portal-Abgleich lief trotzdem weiter. Das ist behoben. `App\Tenancy\TenantFeatures` ist die eine Stelle, die beantwortet, ob ein Mandant einen Dienst hat. Sie wird an allen Wegen abgefragt, die zum Dienst führen: - `RolePolicy::allows()` – damit greift die Mandanten-Ebene in *jeder* Richtlinie des Admins. Die Rolle sagt, was ein Benutzer darf; der Dienst sagt, was der Mandant überhaupt hat. Keine Rolle wiegt einen gesperrten Dienst auf. - `Channel::importsOccupations()` – Import und Scheduler. - Der öffentliche iCal-Feed antwortet mit 404. - `ProcessPortalMail` und `channels:fetch-mail` für den Postfach-Schnellweg. `TenantServiceKey::isWired()` trennt Gebautes von Geplantem. Nur verdrahtete Dienste erscheinen als Schalter; die drei KI-Dienste stehen als Hinweis da, bis die Anwendung sie abfragt. Eigene API-Schlüssel sind aus dem Betreiber- Formular verschwunden – sie gehören in den Admin des Mandanten, wenn es soweit ist (A16). Neu im Betreiber-Bereich: Seite „System" mit den Grenzen, die für alle Mandanten gelten (aus der .env, bewusst nicht änderbar), der Mandantenliste und dem Zugriffsprotokoll. Bisher war dort nur die Mandantenliste. Die Migration schaltet jedem vorhandenen Mandanten die Portal-Dienste frei, damit der Umbau laufenden Betrieb nicht stillschweigend stoppt. 240 Feature-Tests grün, davon fünf neue in `TenantServiceGateTest`. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
136 lines
3.8 KiB
PHP
136 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Channels\ChannelRegistry;
|
|
use App\Channels\Contracts\ChannelConnector;
|
|
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'])]
|
|
#[Hidden(['export_token'])]
|
|
class Channel extends Model
|
|
{
|
|
/** @use HasFactory<ChannelFactory> */
|
|
use BelongsToTenant, 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->servesItsLandlord() && $this->connector()->canImport($this);
|
|
}
|
|
|
|
/**
|
|
* 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'))));
|
|
}
|
|
}
|