TenantAtlas/app/Services/Alerts/WorkspaceTimezoneResolver.php
ahmido 3ed275cef3 feat(alerts): Monitoring cluster + v1 resources (spec 099) (#121)
Implements spec `099-alerts-v1-teams-email`.

- Monitoring navigation: Alerts as a cluster under Monitoring; default landing is Alert deliveries.
- Tenant panel: Alerts points to `/admin/alerts` and the cluster navigation is hidden in tenant panel.
- Guard compliance: removes direct `Gate::` usage from Alert resources so `NoAdHocFilamentAuthPatternsTest` passes.

Verification:
- Full suite: `1348 passed, 7 skipped` (EXIT=0).

Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #121
2026-02-18 15:20:43 +00:00

69 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services\Alerts;
use App\Models\Workspace;
use App\Models\WorkspaceSetting;
class WorkspaceTimezoneResolver
{
public function resolve(Workspace $workspace): string
{
$candidates = [
$this->normalizeTimezone($workspace->getAttribute('timezone')),
$this->settingTimezone($workspace, 'alerts', 'timezone'),
$this->settingTimezone($workspace, 'workspace', 'timezone'),
$this->settingTimezone($workspace, 'general', 'timezone'),
$this->normalizeTimezone(config('app.timezone')),
];
foreach ($candidates as $candidate) {
if ($candidate !== null) {
return $candidate;
}
}
return 'UTC';
}
private function settingTimezone(Workspace $workspace, string $domain, string $key): ?string
{
$setting = WorkspaceSetting::query()
->where('workspace_id', (int) $workspace->getKey())
->where('domain', $domain)
->where('key', $key)
->first(['value']);
if (! $setting instanceof WorkspaceSetting) {
return null;
}
return $this->normalizeTimezone($setting->getAttribute('value'));
}
private function normalizeTimezone(mixed $value): ?string
{
if (is_array($value)) {
$value = $value['timezone'] ?? null;
}
if (! is_string($value)) {
return null;
}
$value = trim($value);
if ($value === '') {
return null;
}
if (! in_array($value, \DateTimeZone::listIdentifiers(), true)) {
return null;
}
return $value;
}
}