*/ 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 */ protected function casts(): array { return [ 'settings' => 'array', 'ical_analysis' => 'encrypted:array', 'ical_analyzed_at' => 'immutable_datetime', 'ical_rules' => 'array', 'ical_active_rules' => 'array', 'ical_export_mapping' => 'array', 'ical_export_names' => 'boolean', '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 */ public function property(): BelongsTo { return $this->belongsTo(Property::class); } /** * @return HasMany */ public function blockedPeriods(): HasMany { return $this->hasMany(BlockedPeriod::class); } /** * @return HasMany */ public function syncLogs(): HasMany { return $this->hasMany(ChannelSyncLog::class)->latest('id'); } /** @return HasMany */ public function exportRequests(): HasMany { return $this->hasMany(ChannelExportRequest::class)->latest('requested_at')->latest('id'); } /** * @return HasMany */ public function tasks(): HasMany { return $this->hasMany(ChannelTask::class); } /** * @param Builder $query * @return Builder */ #[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')))); } }