Skip to content

Commit

Permalink
#59 Update TwentyI/Api::searchForStackUser() to use new explicitStack…
Browse files Browse the repository at this point in the history
…UserCheck endpoint
  • Loading branch information
uphlewis committed Jul 16, 2024
1 parent b38836c commit a0eb479
Showing 1 changed file with 4 additions and 18 deletions.
22 changes: 4 additions & 18 deletions src/TwentyI/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,29 +54,15 @@ public function __construct(string $generalApiKey, ?LoggerInterface $logger = nu
public function searchForStackUser(string $email, bool $orFail = false): ?string
{
try {
// Attempt to create a new stack user first, and upon exception, search for existing user.
// We do this because for large resellers, the list of users can be VERY large and requests can time-out.
// For new customers at least, this should speed things up dramatically and avoid unnecessary time-outs.
try {
return $this->createStackUser($email);
} catch (\Throwable $e) {
// ignore
}

// "list all + filter results" appears to be the only way to find a user
// $allUsers = $this->services->getWithFields('/reseller/*/susers')->users;
$allUsers = $this->services->getWithFields('/reseller/*/explicitStackUser', [
// 'name' => $email,
]);
$user = Arr::first($allUsers, function ($user) use ($email) {
return ($user->email ?? $user->name) === $email;
});
$user = $this->services->postWithFields('/reseller/*/explicitStackUserCheck', [
'email' => $email,
])->result ?? null;

if (!$user && $orFail) {
throw (new ProvisionFunctionError('Stack user not found'))->withData(['email' => $email]);
}

return $user ? sprintf('%s:%s', $user->type ?? 'stack-user', $user->id) : null;
return $user;
} catch (\Throwable $e) {
$this->handleException($e, 'Could not list stack users', ['email' => $email]);
}
Expand Down

0 comments on commit a0eb479

Please sign in to comment.