Files
ladill-care/app/Services/Care/TeamMemberProvisioner.php
isaaccladandCursor d643687335
Deploy Ladill Care / deploy (push) Successful in 1m3s
Lock doctors to a single branch — never a branch picker.
Doctors were treated as multi-branch when Identity cleared branch_id; now they stay on their assigned/desk site, specialty desks are home-branch only, and the queue shows site name as text only.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-18 08:45:57 +00:00

67 lines
2.1 KiB
PHP

<?php
namespace App\Services\Care;
use App\Models\Member;
use App\Models\Organization;
use App\Models\User;
use App\Services\Identity\IdentityTeamClient;
class TeamMemberProvisioner
{
public function __construct(
protected IdentityTeamClient $identity,
) {}
public function sync(User $user): void
{
Member::query()
->where('user_ref', strtolower((string) $user->email))
->update(['user_ref' => $user->ownerRef()]);
try {
$access = $this->identity->teamAccess($user->ownerRef());
} catch (\Throwable) {
return;
}
foreach ($access as $grant) {
if (($grant['self'] ?? false) || ($grant['full_access'] ?? false)) {
continue;
}
$apps = (array) ($grant['apps'] ?? []);
if (! in_array('care', $apps, true)) {
continue;
}
$appMeta = (array) data_get($grant, 'metadata.care', []);
$ownerRef = (string) ($grant['account'] ?? '');
$organizationId = (int) ($appMeta['organization_id'] ?? 0);
if ($organizationId <= 0 && $ownerRef !== '') {
$organizationId = (int) Organization::query()->where('owner_ref', $ownerRef)->value('id');
}
if ($organizationId <= 0) {
continue;
}
Member::updateOrCreate(
[
'organization_id' => $organizationId,
'user_ref' => $user->ownerRef(),
],
[
'owner_ref' => $ownerRef !== '' ? $ownerRef : $user->ownerRef(),
'role' => (string) ($appMeta['role'] ?? 'member'),
'branch_id' => array_key_exists('branch_id', $appMeta)
? $appMeta['branch_id']
: Member::query()
->where('organization_id', $organizationId)
->where('user_ref', $user->ownerRef())
->value('branch_id'),
],
);
}
}
}