ownerRef($request); $organization = Organization::owned($owner)->first(); if (! $organization) { return response()->json([ 'data' => [ 'provisioned' => false, 'onboarded' => false, 'integrations' => ['care' => false, 'frontdesk' => false], ], ]); } return response()->json([ 'data' => [ 'provisioned' => true, 'onboarded' => (bool) data_get($organization->settings, 'onboarded', false), 'organization_name' => $organization->name, 'integrations' => [ 'care' => (bool) data_get($organization->settings, 'integrations.care_enabled', false), 'frontdesk' => (bool) data_get($organization->settings, 'integrations.frontdesk_enabled', false), ], ], ]); } public function provision(Request $request): JsonResponse { $owner = $this->ownerRef($request); $caller = (string) $request->attributes->get('service_caller', ''); $validated = $request->validate([ 'organization_name' => ['nullable', 'string', 'max:255'], 'branch_name' => ['nullable', 'string', 'max:255'], 'timezone' => ['nullable', 'timezone'], 'industry' => ['nullable', 'string'], ]); $organization = Organization::owned($owner)->first(); $created = false; if (! $organization) { $created = true; $name = $validated['organization_name'] ?? 'Service queues'; $organization = Organization::create([ 'owner_ref' => $owner, 'name' => $name, 'slug' => Str::slug($name).'-'.substr($owner, 0, 6), 'timezone' => $validated['timezone'] ?? config('app.timezone', 'UTC'), 'settings' => [ 'onboarded' => true, 'industry' => $validated['industry'] ?? ($caller === 'care' ? 'healthcare' : 'visitor'), 'appointment_mode' => 'hybrid', 'integrations' => [ 'care_enabled' => false, 'frontdesk_enabled' => false, ], ], ]); $branch = Branch::create([ 'owner_ref' => $owner, 'organization_id' => $organization->id, 'name' => $validated['branch_name'] ?? 'Main branch', 'is_active' => true, ]); Department::create([ 'owner_ref' => $owner, 'branch_id' => $branch->id, 'name' => 'Reception', 'type' => 'reception', 'is_active' => true, ]); AuditLogger::record($owner, 'organization.created', $organization->id, $owner, Organization::class, $organization->id); } if (in_array($caller, ['care', 'frontdesk'], true)) { $this->syncIntegrationFlag($organization->fresh(), $caller, true); } return response()->json([ 'data' => [ 'provisioned' => true, 'organization_name' => $organization->fresh()->name, 'integrations' => [ 'care' => (bool) data_get($organization->fresh()->settings, 'integrations.care_enabled', false), 'frontdesk' => (bool) data_get($organization->fresh()->settings, 'integrations.frontdesk_enabled', false), ], ], ], $created ? 201 : 200); } public function update(Request $request): JsonResponse { $validated = $request->validate([ 'care_enabled' => ['nullable', 'boolean'], 'frontdesk_enabled' => ['nullable', 'boolean'], ]); $organization = $this->organization($request); $caller = (string) $request->attributes->get('service_caller', ''); $settings = $organization->settings ?? []; $integrations = $settings['integrations'] ?? []; if (array_key_exists('care_enabled', $validated) && in_array($caller, ['care', 'crm'], true)) { $integrations['care_enabled'] = (bool) $validated['care_enabled']; } if (array_key_exists('frontdesk_enabled', $validated) && in_array($caller, ['frontdesk', 'crm'], true)) { $integrations['frontdesk_enabled'] = (bool) $validated['frontdesk_enabled']; } $settings['integrations'] = $integrations; $organization->update(['settings' => $settings]); return response()->json([ 'data' => [ 'integrations' => [ 'care' => (bool) ($integrations['care_enabled'] ?? false), 'frontdesk' => (bool) ($integrations['frontdesk_enabled'] ?? false), ], ], ]); } protected function syncIntegrationFlag(Organization $organization, string $caller, bool $enabled): void { if (! in_array($caller, ['care', 'frontdesk'], true)) { return; } $settings = $organization->settings ?? []; $integrations = $settings['integrations'] ?? []; $key = $caller.'_enabled'; $integrations[$key] = $enabled; $settings['integrations'] = $integrations; $organization->update(['settings' => $settings]); } }