Files
ladill-meet/app/Services/Meet/SalesCentreWorkspaceProvisioner.php
T
isaaccladandCursor 2a29c89f74
Deploy Ladill Meet / deploy (push) Successful in 1m34s
Auto-provision Meet workspaces for Care video visit scheduling.
Care appointments can create Meet rooms for accounts that have never opened Meet, matching the Sales Centre provisioning path.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-12 14:14:19 +00:00

80 lines
2.3 KiB
PHP

<?php
namespace App\Services\Meet;
use App\Models\Branch;
use App\Models\Organization;
use App\Models\User;
use Illuminate\Support\Str;
class SalesCentreWorkspaceProvisioner
{
public function __construct(
protected OrganizationResolver $organizations,
) {}
/**
* @param array<string, mixed> $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';
}
}