$validated */ public function ensureHost(array $validated): User { $publicId = (string) $validated['host_user_ref']; return User::updateOrCreate( ['public_id' => $publicId], [ 'name' => trim((string) ($validated['host_name'] ?? '')) ?: ( ($validated['source']['app'] ?? '') === 'care' ? 'Care practitioner' : 'Sales representative' ), 'email' => $this->resolveEmail($publicId, $validated['host_email'] ?? null), ], ); } public function ensureOrganization(User $host): Organization { $ref = $host->ownerRef(); $existing = Organization::owned($ref)->first(); if ($existing) { $this->organizations->ensureOwnerMember($host, $existing); return $existing; } $label = trim($host->name) !== '' ? $host->name.' workspace' : 'Ladill Meet workspace'; $organization = Organization::create([ 'owner_ref' => $ref, 'name' => $label, 'slug' => Str::slug($label).'-'.Str::lower(Str::random(4)), 'timezone' => config('app.timezone', 'Africa/Accra'), 'settings' => [ 'onboarded' => true, 'org_type' => 'suite', 'provisioned_by' => 'suite_service_api', ], ]); $this->organizations->ensureOwnerMember($host, $organization); Branch::create([ 'owner_ref' => $ref, 'organization_id' => $organization->id, 'name' => 'Main', 'is_active' => true, ]); AuditLogger::record($ref, 'organization.created', $organization->id, $ref, Organization::class, $organization->id); return $organization; } private function resolveEmail(string $publicId, mixed $email): string { $email = trim((string) $email); return $email !== '' ? $email : $publicId.'@users.ladill.com'; } }