authorizeAbility($request, 'settings.view'); $organization = $this->organization($request); $canManage = app(\App\Services\Frontdesk\FrontdeskPermissions::class) ->isAdmin($this->member($request)); $webhook = WebhookEndpoint::owned($this->ownerRef($request)) ->where('organization_id', $organization->id) ->first(); return view('frontdesk.integrations.edit', [ 'organization' => $organization, 'canManage' => $canManage, 'webhook' => $webhook, 'webhookEvents' => config('frontdesk.webhook_events', []), 'integrations' => config('frontdesk.integrations', []), 'icalUrl' => $this->signedIcalUrl($organization->id, $this->ownerRef($request)), ]); } public function update(Request $request): RedirectResponse { $this->authorizeAbility($request, 'settings.manage'); $organization = $this->organization($request); $validated = $request->validate([ 'webhook_url' => ['nullable', 'url', 'max:500'], 'webhook_secret' => ['nullable', 'string', 'max:128'], 'webhook_events' => ['nullable', 'array'], 'webhook_events.*' => ['string'], 'webhook_active' => ['boolean'], ]); if (empty($validated['webhook_url'])) { WebhookEndpoint::owned($this->ownerRef($request)) ->where('organization_id', $organization->id) ->delete(); return back()->with('success', 'Integration settings saved.'); } WebhookEndpoint::updateOrCreate( [ 'owner_ref' => $this->ownerRef($request), 'organization_id' => $organization->id, ], [ 'url' => $validated['webhook_url'], 'secret' => $validated['webhook_secret'] ?? null, 'events' => array_values($validated['webhook_events'] ?? config('frontdesk.webhook_events')), 'is_active' => $request->boolean('webhook_active', true), ], ); return back()->with('success', 'Integration settings saved.'); } protected function signedIcalUrl(int $organizationId, string $ownerRef): string { $token = hash_hmac('sha256', "{$organizationId}:{$ownerRef}", (string) config('app.key')); return route('frontdesk.integrations.ical', [ 'organization' => $organizationId, 'owner' => $ownerRef, 'token' => $token, ]); } }