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('queue', $apps, true)) { continue; } $meta = (array) data_get($grant, 'metadata.queue', []); $ownerRef = (string) ($grant['account'] ?? ''); $organizationId = (int) ($meta['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) ($meta['role'] ?? 'member'), 'branch_id' => $meta['branch_id'] ?? null, ], ); } } }