Files
fewo-stommer/app/Models/Channel.php
LarsandCursor 8ebc1a3878 Assign the existing site to one landlord so stommer.info can run tenancy.
Seebad 0.3 stays in this release. With a single landlord every host still reaches the default website.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-23 12:00:11 +02:00

125 lines
3.4 KiB
PHP

<?php
namespace App\Models;
use App\Channels\ChannelRegistry;
use App\Channels\Contracts\ChannelConnector;
use App\Models\Concerns\BelongsToTenant;
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->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'))));
}
}