From 95fd66305e0c1f2c82c8fd119db864f8d74674c0 Mon Sep 17 00:00:00 2001 From: Ivan Chaplygin Date: Wed, 24 Apr 2024 15:19:05 +0300 Subject: [PATCH] ref #95034 Adding a validator --- .../class-wc-retailcrm-loyalty-constraint.php | 15 +++- .../class-wc-retailcrm-loyalty-validator.php | 90 +++++++++++++++++++ 2 files changed, 103 insertions(+), 2 deletions(-) diff --git a/src/include/validators/loyalty-validator/class-wc-retailcrm-loyalty-constraint.php b/src/include/validators/loyalty-validator/class-wc-retailcrm-loyalty-constraint.php index 669b3f9..2521be3 100644 --- a/src/include/validators/loyalty-validator/class-wc-retailcrm-loyalty-constraint.php +++ b/src/include/validators/loyalty-validator/class-wc-retailcrm-loyalty-constraint.php @@ -14,7 +14,18 @@ if (!class_exists('WC_Retailcrm_Loyalty_Constraint')) : */ class WC_Retailcrm_Loyalty_Constraint { - /** @var string */ - public $err = 'error'; + public $notFoundCrmUser = 'User not found in the system'; + + public $errorFoundLoyalty = 'Error when searching for participation in loyalty programs'; + + public $notFoundActiveParticipation = 'No active participation in the loyalty program was detected'; + + public $notExistBonuses = 'No bonuses for debiting'; + + public $notFoundLoyalty = 'Loyalty program not found'; + + public $loyaltyInactive = 'Loyalty program is not active'; + + public $loyaltyBlocked = 'Loyalty program blocked'; } endif; diff --git a/src/include/validators/loyalty-validator/class-wc-retailcrm-loyalty-validator.php b/src/include/validators/loyalty-validator/class-wc-retailcrm-loyalty-validator.php index 2182aaa..c7ba0a8 100644 --- a/src/include/validators/loyalty-validator/class-wc-retailcrm-loyalty-validator.php +++ b/src/include/validators/loyalty-validator/class-wc-retailcrm-loyalty-validator.php @@ -14,9 +14,99 @@ if (!class_exists('WC_Retailcrm_Loyalty_Validator')) : */ class WC_Retailcrm_Loyalty_Validator extends WC_Retailcrm_Loyalty_Constraint { + /** @var WC_Retailcrm_Client_V5 */ + protected $apiClient; + + protected $settings; + + public function __construct($apiClient, $settings) + { + $this->apiClient = $apiClient; + $this->settings = $settings; + } + public function checkAccount(int $userId) { + $result = false; + try { + $crmUser = $this->getUser($userId); + //add check customer corporate + $actualAccount = $this->getLoyaltyAccount($crmUser['id']); + $this->checkActiveLoyalty($actualAccount['loyalty']['id']); + + } catch (Throwable $exception) { + if ($exception instanceof ValidatorException) { + WC_Admin_Settings::add_error((esc_html__($exception->getMessage(), 'retailcrm')) . "userId: $userId"); + } else { + WC_Admin_Settings::add_error($exception->getMessage()); + } + } + } + + /** + * @throws ValidatorException + */ + private function getUser($userId) + { + $responseUser = $this->apiClient->customersGet($userId); + + if (!isset($responseUser['customer']['id'])) { + throw new ValidatorException($this->notFoundCrmUser, 400); + } + + return $responseUser['customer']; + } + + /** + * @throws ValidatorException + */ + private function getLoyaltyAccount($crmUserId) + { + $filter['customerId'] = $crmUserId; + $responseLoyalty = $this->apiClient->getLoyaltyAccountList($filter); + + if (!$responseLoyalty->isSuccessful() || !$responseLoyalty->offsetExists('loyaltyAccounts')) { + throw new ValidatorException($this->errorFoundLoyalty, 400); + } + + $actualAccount = null; + + foreach ($responseLoyalty['loyaltyAccounts'] as $loyaltyAccount) { + if ($loyaltyAccount['active'] === true) { + $actualAccount = $loyaltyAccount; + } + } + + if (!isset($actualAccount)) { + throw new ValidatorException($this->notFoundActiveParticipation, 400); + } + + if ($actualAccount['amount'] === 0 && $actualAccount['level']['type'] !== 'discount') { + throw new ValidatorException($this->notExistBonuses, 400); + } + + return $actualAccount; + } + + /** + * @throws ValidatorException + */ + private function checkActiveLoyalty($idLoyalty) + { + $responseLoyalty = $this->apiClient->getLoyalty($idLoyalty); + + if (!$responseLoyalty->isSuccessful() || !$responseLoyalty->offsetExists('loyalty')) { + throw new ValidatorException($this->notFoundLoyalty, 400); + } + + if ($responseLoyalty['loyalty']['active'] !== true) { + throw new ValidatorException($this->loyaltyInactive, 400); + } + + if ($responseLoyalty['loyalty']['blocked'] === true) { + throw new ValidatorException($this->loyaltyBlocked, 400); + } } } endif;