Files
ladill-meet/app/Services/Meet/SalesCentreWorkspaceProvisioner.php
T
isaaccladandCursor da8d64045b
Deploy Ladill Meet / deploy (push) Successful in 1m38s
Auto-provision Meet workspace for Sales Centre demo links.
Create rep user, organization, and branch on first demo room request so sales reps do not need to onboard in Meet first.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-11 21:12:52 +00:00

78 lines
2.2 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'] ?? '')) ?: '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' : 'Sales 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' => 'sales',
'provisioned_by' => 'sales_centre',
],
]);
$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';
}
}