diff --git a/examples/users_actions.php b/examples/users_actions.php index 8fc9fb5..f440bc4 100644 --- a/examples/users_actions.php +++ b/examples/users_actions.php @@ -117,6 +117,14 @@ function (AccessTokenInterface $accessToken, string $baseDomain) { die; } +// Деактивация добавленных пользователей +try { + $usersService->deactivate($usersCollection); +} catch (AmoCRMApiException $e) { + printError($e); + die(); +} + //Получим всех пользователей аккаунта try { $usersCollection = $usersService->get(); diff --git a/src/AmoCRM/EntitiesServices/Users.php b/src/AmoCRM/EntitiesServices/Users.php index 286e2b4..7642d71 100644 --- a/src/AmoCRM/EntitiesServices/Users.php +++ b/src/AmoCRM/EntitiesServices/Users.php @@ -3,6 +3,12 @@ namespace AmoCRM\EntitiesServices; use AmoCRM\Collections\UsersCollection; +use AmoCRM\Exceptions\AmoCRMApiConnectExceptionException; +use AmoCRM\Exceptions\AmoCRMApiException; +use AmoCRM\Exceptions\AmoCRMApiHttpClientException; +use AmoCRM\Exceptions\AmoCRMApiNoContentException; +use AmoCRM\Exceptions\AmoCRMApiTooManyRedirectsException; +use AmoCRM\Exceptions\AmoCRMoAuthApiException; use AmoCRM\Exceptions\NotAvailableForActionException; use AmoCRM\Filters\BaseEntityFilter; use AmoCRM\Helpers\EntityTypesInterface; @@ -15,6 +21,7 @@ use AmoCRM\Models\Rights\RightModel; use AmoCRM\Models\RoleModel; use AmoCRM\Models\UserModel; +use InvalidArgumentException; /** * Class Users @@ -100,6 +107,67 @@ protected function processAction(BaseApiCollection $collection, array $response) return $collection; } + /** + * @throws AmoCRMApiException + * @throws AmoCRMApiNoContentException + * @throws AmoCRMApiHttpClientException + * @throws AmoCRMoAuthApiException + * @throws AmoCRMApiConnectExceptionException + * @throws AmoCRMApiTooManyRedirectsException + */ + public function activate(UsersCollection $users): bool + { + try { + $this->request->post(sprintf('%s/activate', $this->getMethod()), $this->prepareUserIdsPayload($users)); + } catch (AmoCRMApiNoContentException $exception) { + return true; + } + + return false; + } + + /** + * @throws AmoCRMApiException + * @throws AmoCRMApiNoContentException + * @throws AmoCRMApiHttpClientException + * @throws AmoCRMoAuthApiException + * @throws AmoCRMApiConnectExceptionException + * @throws AmoCRMApiTooManyRedirectsException + */ + public function deactivate(UsersCollection $users): bool + { + try { + $this->request->post(sprintf('%s/deactivate', $this->getMethod()), $this->prepareUserIdsPayload($users)); + } catch (AmoCRMApiNoContentException $exception) { + return true; + } + + return false; + } + + protected function prepareUserIdsPayload(UsersCollection $users): array + { + if ($users->isEmpty()) { + throw new InvalidArgumentException('Collection is empty'); + } + + if ($users->count() > 10) { + throw new InvalidArgumentException('Maximum 10 users per request'); + } + + $payload = []; + foreach ($users as $user) { + /** @var UserModel $user */ + if (!$id = $user->getId()) { + throw new InvalidArgumentException('User without ID in collection'); + } + + $payload[] = ['id' => $id]; + } + + return $payload; + } + /** * @param BaseApiCollection $collection *