From 19fbf04f609d54cc23e1599a07039e1901f04624 Mon Sep 17 00:00:00 2001 From: Smiley48 <43863676+Smiley48@users.noreply.github.com> Date: Wed, 20 Jul 2022 12:46:32 +0300 Subject: [PATCH] Fix existing customer in loyalty (#262) --- .docker/Dockerfile | 2 +- .github/workflows/ci.yml | 4 +- CHANGELOG.md | 4 + Makefile | 11 +-- docker-compose.yml | 1 + intaro.retailcrm/description.ru | 3 +- intaro.retailcrm/include.php | 1 - intaro.retailcrm/install/version.php | 4 +- .../lib/service/loyaltyaccountservice.php | 67 +++++++++++++++- tests/bootstrap.php | 12 ++- .../general/events/RetailCrmEventTest.php | 12 ++- .../general/order/RetailCrmOrder_v5Test.php | 3 +- .../general/services/RetailCrmServiceTest.php | 46 +++++++++++ .../lib/service/LoyaltyAccountServiceTest.php | 78 +++++++++++++++++++ 14 files changed, 222 insertions(+), 26 deletions(-) create mode 100644 tests/lib/service/LoyaltyAccountServiceTest.php diff --git a/.docker/Dockerfile b/.docker/Dockerfile index b1149870..0a857b46 100755 --- a/.docker/Dockerfile +++ b/.docker/Dockerfile @@ -1,4 +1,4 @@ -FROM php:7.1-apache +FROM php:7.3-apache RUN a2enmod rewrite diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6cea27b9..36765d9c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,7 +20,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php-version: ['7.1', '7.2', '7.3'] + php-version: ['7.3'] bitrix-edition: ['small_business_encode', 'business_encode'] services: mysql: @@ -42,7 +42,7 @@ jobs: - name: Install Bitrix env: BITRIX_EDITION: ${{ matrix.bitrix-edition }} - run: make bitrix_install + run: make install_bitrix - name: Enable debug mode for tests run: php bin/enable_debugging "${{ env.BITRIX_PATH }}" - name: Validate composer.json and composer.lock diff --git a/CHANGELOG.md b/CHANGELOG.md index cccb834a..fd2ca7fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2022-07-18 v.6.1.3 +- Исправление работы модуля при попытки регистрации в программе лояльности уже существующего в ней клиента +- Исправлени тестов + ## 2022-07-01 v.6.1.2 - Исправление передачи корпоративных клиентов diff --git a/Makefile b/Makefile index 237f560d..102c84b5 100755 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ ifneq ($(NOT_USE_VENDOR),1) composer install endif -bitrix_install: download_bitrix +install_bitrix: download_bitrix @echo "===== Installing Bitrix..." @php bin/bitrix-install db_type @php bin/bitrix-install requirement @@ -43,11 +43,4 @@ endif cleanup: @rm -rf $(ROOT_DIR)/release/$(CURRENT_VERSION) - @rm $(ROOT_DIR)/release/$(CURRENT_VERSION).tar.gz - -# docker commands -install: - docker-compose exec bitrix make bitrix_install - -run_tests: - docker-compose exec bitrix make test + @rm $(ROOT_DIR)/release/$(CURRENT_VERSION).tar.gz \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 68cc5fe4..773314d3 100755 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -19,6 +19,7 @@ services: volumes: - ./:/bitrix-module - ./.docker/php.ini:/usr/local/etc/php/conf.d/z-bitrix.ini + - ./tmp:/tmp mysql: env_file: - .env diff --git a/intaro.retailcrm/description.ru b/intaro.retailcrm/description.ru index 23f37f91..f814c906 100644 --- a/intaro.retailcrm/description.ru +++ b/intaro.retailcrm/description.ru @@ -1 +1,2 @@ -- Исправление передачи корпоративных клиентов \ No newline at end of file +- Исправление работы модуля при попытки регистрации в программе лояльности уже существующего в ней клиента +- Исправлени тестов \ No newline at end of file diff --git a/intaro.retailcrm/include.php b/intaro.retailcrm/include.php index 4650f493..9e68ce2d 100644 --- a/intaro.retailcrm/include.php +++ b/intaro.retailcrm/include.php @@ -58,7 +58,6 @@ foreach ($arJsConfig as $ext => $arExt) { if (empty(ConfigProvider::getSitesAvailable())) { $client = ClientFactory::createClientAdapter(); - try { $credentials = $client->getCredentials(); diff --git a/intaro.retailcrm/install/version.php b/intaro.retailcrm/install/version.php index ac51fb0b..facab21d 100644 --- a/intaro.retailcrm/install/version.php +++ b/intaro.retailcrm/install/version.php @@ -1,6 +1,6 @@ '6.1.2', - 'VERSION_DATE' => '2022-07-01 15:00:00' + 'VERSION' => '6.1.3', + 'VERSION_DATE' => '2022-07-18 10:00:00' ]; diff --git a/intaro.retailcrm/lib/service/loyaltyaccountservice.php b/intaro.retailcrm/lib/service/loyaltyaccountservice.php index 6c07f273..71d3a649 100644 --- a/intaro.retailcrm/lib/service/loyaltyaccountservice.php +++ b/intaro.retailcrm/lib/service/loyaltyaccountservice.php @@ -20,6 +20,8 @@ use Intaro\RetailCrm\Component\Factory\ClientFactory; use Intaro\RetailCrm\Component\Json\Serializer; use Intaro\RetailCrm\Component\ServiceLocator; use Intaro\RetailCrm\Model\Api\LoyaltyAccount; +use Intaro\RetailCrm\Model\Api\LoyaltyAccountApiFilterType; +use Intaro\RetailCrm\Model\Api\Request\Loyalty\Account\LoyaltyAccountRequest; use Intaro\RetailCrm\Model\Api\Request\Loyalty\Account\LoyaltyAccountActivateRequest; use Intaro\RetailCrm\Model\Api\Request\Loyalty\Account\LoyaltyAccountCreateRequest; use Intaro\RetailCrm\Model\Api\Request\Loyalty\Account\LoyaltyAccountEditRequest; @@ -144,9 +146,34 @@ class LoyaltyAccountService 'UF_EXT_REG_PL_INTARO' => $createResponse->loyaltyAccount->active === true ? 'Y' : '', 'UF_LP_ID_INTARO' => $createResponse->loyaltyAccount->id, ]); - } - Utils::handleApiErrors($createResponse, GetMessage('REGISTER_ERROR')); + Utils::handleApiErrors($createResponse, GetMessage('REGISTER_ERROR')); + } elseif ($this->proveUserInLpExists($createResponse)) { + global $USER_FIELD_MANAGER; + + /** @var \Intaro\RetailCrm\Component\ApiClient\ClientAdapter $client */ + $client = ClientFactory::createClientAdapter(); + + $sitesAvailable = ConfigProvider::getSitesAvailable(); + + $getRequest = new LoyaltyAccountRequest(); + + $getRequest->filter = new LoyaltyAccountApiFilterType(); + $getRequest->filter->sites = $sitesAvailable; + $getRequest->filter->customerExternalId = (string)$userId; + + + $getResponse = $client->getLoyaltyAccounts($getRequest); + + if ($getResponse instanceof LoyaltyAccountCreateResponse) { + Utils::handleApiErrors($getResponse, GetMessage('REGISTER_ERROR')); + } + + $USER_FIELD_MANAGER->Update('USER', $userId, [ + 'UF_EXT_REG_PL_INTARO' => ($getResponse->loyaltyAccounts)[0]->active === true ? 'Y' : '', + 'UF_LP_ID_INTARO' => ($getResponse->loyaltyAccounts)[0]->id, + ]); + } } /** @@ -510,6 +537,20 @@ class LoyaltyAccountService $errorMsg = Utils::getErrorMsg($createResponse); + if ($this->proveUserInLpExists($createResponse)) { + if ( + $createResponse->loyaltyAccount->active === false + && $createResponse->loyaltyAccount->activatedAt === null + ) { + return [ + 'msg' => GetMessage('GO_TO_PERSONAL'), + 'error' => false + ]; + } else { + return ['msg' => GetMessage('REG_COMPLETE'), 'error' => false]; + } + } + if ($errorMsg !== null) { return ['msg' => $errorMsg, 'error' => true]; } @@ -617,4 +658,26 @@ class LoyaltyAccountService return $resultFieldsArray; } + + /** + * @param \Intaro\RetailCrm\Model\Api\Response\Loyalty\Account\LoyaltyAccountCreateResponse|null $createResponse + * + * @return bool + */ + public function proveUserInLpExists(?LoyaltyAccountCreateResponse $createResponse): bool + { + if ( + $createResponse !== null + && $createResponse->success !== true + && isset($createResponse->errors) + && isset(($createResponse->errors)['loyalty']) + && ($createResponse->errors)['loyalty'] === 'The customer is in this loyalty program already' + ) { + $response = true; + } else { + $response = false; + } + + return $response; + } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index ebc8d84d..d5510b44 100755 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -25,12 +25,16 @@ require($_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/main/include/prolog_before. global $USER; $USER->Authorize(1); -if (!IsModuleInstalled('intaro.retailcrm')) { - RegisterModule('intaro.retailcrm'); +$retailcrmModuleId = 'intaro.retailcrm'; + +if (!IsModuleInstalled($retailcrmModuleId)) { + RegisterModule($retailcrmModuleId); } -COption::SetOptionString('intaro.retailcrm', 'api_version', 'v5'); -CModule::IncludeModule('intaro.retailcrm'); +COption::SetOptionString($retailcrmModuleId, 'api_host', 'https://test.retailcrm.ru'); +COption::SetOptionString($retailcrmModuleId, 'api_key', 'nvwhebvwevboihbwvoijn'); +COption::SetOptionString($retailcrmModuleId, 'api_version', 'v5'); +CModule::IncludeModule($retailcrmModuleId); CModule::IncludeModule("main"); global $DB; diff --git a/tests/classes/general/events/RetailCrmEventTest.php b/tests/classes/general/events/RetailCrmEventTest.php index f123bedb..e48c869e 100755 --- a/tests/classes/general/events/RetailCrmEventTest.php +++ b/tests/classes/general/events/RetailCrmEventTest.php @@ -151,7 +151,7 @@ class RetailCrmEventTest extends PHPUnit\Framework\TestCase $result = $this->retailcrmEvent->OrderDelete($event); - $this->assertEquals(null, $result); + $this->assertEquals(true, $GLOBALS['RETAILCRM_ORDER_DELETE']); } /** @@ -160,11 +160,17 @@ class RetailCrmEventTest extends PHPUnit\Framework\TestCase */ public function testOnUpdateOrder() { - $arFields = []; + $arFields = [ + 'CANCELED' => 'Y', + 'BASKET_ITEMS' => [], + 'ORDER_PROP' => [] + ]; $result = $this->retailcrmEvent->OnUpdateOrder(1, $arFields); - $this->assertEquals(null, $result); + $this->assertEquals(true, $GLOBALS['RETAILCRM_ORDER_OLD_EVENT']); + + $this->assertEquals(true, $GLOBALS['ORDER_DELETE_USER_ADMIN']); } /** diff --git a/tests/classes/general/order/RetailCrmOrder_v5Test.php b/tests/classes/general/order/RetailCrmOrder_v5Test.php index 796c9244..5ff94755 100644 --- a/tests/classes/general/order/RetailCrmOrder_v5Test.php +++ b/tests/classes/general/order/RetailCrmOrder_v5Test.php @@ -116,7 +116,8 @@ class RetailCrmOrder_v5Test extends BitrixTestCase { 'externalId' => RCrmActions::generatePaymentExternalId($arFields['PAYMENTS'][0]['ID']), 'status' => 'paid', 'paidAt' => $this->getDateTime()->format('Y-m-d H:i:s') - ]] + ]], + 'privilegeType' => 'none' ], ]]; } diff --git a/tests/classes/general/services/RetailCrmServiceTest.php b/tests/classes/general/services/RetailCrmServiceTest.php index 89ec02ad..3b61e7d0 100644 --- a/tests/classes/general/services/RetailCrmServiceTest.php +++ b/tests/classes/general/services/RetailCrmServiceTest.php @@ -33,4 +33,50 @@ class RetailCrmServiceTest extends PHPUnit\Framework\TestCase $this->assertEquals($newParams, $expectedArray); } + + /** + * @param array $data + * @param array $expected + * @dataProvider selectIntegrationDeliveriesProvider + */ + public function testSelectIntegrationDeliveries(array $data, array $expected) + { + $this->assertEquals($expected, RetailCrmService::selectIntegrationDeliveries($data)); + } + + /** + * @param array $data + * @param array $expected + * @dataProvider selectIntegrationPaymentsProvider + */ + public function testSelectIntegrationPayments(array $data, array $expected) + { + $this->assertEquals($expected, RetailCrmService::selectIntegrationPayments($data)); + } + + public function selectIntegrationDeliveriesProvider() + { + return [[ + 'data' => [ + [ + 'code' => 'test1', + 'integrationCode' => 'test2' + ] + ], + 'expected' => ['test1' => 'test2'] + ]]; + } + + public function selectIntegrationPaymentsProvider() + { + return [[ + 'data' => [ + [ + 'code' => 'test1', + 'integrationModule' => 'test2' + ] + ], + 'expected' => ['test1'] + ]]; + } } diff --git a/tests/lib/service/LoyaltyAccountServiceTest.php b/tests/lib/service/LoyaltyAccountServiceTest.php new file mode 100644 index 00000000..503264ce --- /dev/null +++ b/tests/lib/service/LoyaltyAccountServiceTest.php @@ -0,0 +1,78 @@ +success = false; + $createResponse->errors = [ + 'loyalty' => 'The customer is in this loyalty program already' + ]; + + return [[ + 'createResponse' => $createResponse, + 'expected' => true + ]]; + } + + /** + * @return array[] + */ + public function proveNotUserInLpExistsProvider() + { + $createResponse = new LoyaltyAccountCreateResponse(); + $createResponse->success = false; + $createResponse->errors = [ + 'loyalty' => 'Some other failure' + ]; + + return [[ + 'createResponse' => $createResponse, + 'expected' => false + ]]; + } +} \ No newline at end of file