diff --git a/CHANGELOG.md b/CHANGELOG.md index 0fed4365..ba71246d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 2023-03-30 v.6.2.4 +- Добавлен функционал слияния дублей пользователей по истории + ## 2023-03-29 v.6.2.3 - Исправление ошибок при установке модуля и выгрузке истории заказов для работы с несколькими сайтами diff --git a/intaro.retailcrm/classes/general/history/RetailCrmHistory_v5.php b/intaro.retailcrm/classes/general/history/RetailCrmHistory_v5.php index b6ea0cf1..396d4438 100644 --- a/intaro.retailcrm/classes/general/history/RetailCrmHistory_v5.php +++ b/intaro.retailcrm/classes/general/history/RetailCrmHistory_v5.php @@ -126,7 +126,8 @@ class RetailCrmHistory $registerNewUser = true; if (!empty($customer['email'])) { - $dbUser = CUser::GetList(($by = 'ID'), ($sort = 'ASC'), array('=EMAIL' => $customer['email'])); + $dbUser = CUser::GetList(($by = 'ID'), ($sort = 'DESC'), ['=EMAIL' => $customer['email']]); + switch ($dbUser->SelectedRowsCount()) { case 0: $login = $customer['email']; @@ -138,8 +139,9 @@ class RetailCrmHistory $registerNewUser = false; break; default: - $login = uniqid('user_' . time()) . '@example.com'; - $customerBuilder->setLogin($login); + $lastBitrixUser = $dbUser->Fetch(); + $registeredUserID = $lastBitrixUser['ID']; + $registerNewUser = false; break; } } @@ -159,15 +161,18 @@ class RetailCrmHistory continue; } - } - if(RCrmActions::apiMethod( - $api, - 'customersFixExternalIds', - __METHOD__, - array(array('id' => $customer['id'], 'externalId' => $registeredUserID))) == false - ) { - continue; + if ( + RCrmActions::apiMethod + ( + $api, + 'customersFixExternalIds', + __METHOD__, + [['id' => $customer['id'], 'externalId' => $registeredUserID]] + ) == false + ) { + continue; + } } $customer['externalId'] = $registeredUserID; @@ -414,8 +419,8 @@ class RetailCrmHistory $dbUser = CUser::GetList( ($by = 'ID'), - ($sort = 'ASC'), - array('=EMAIL' => $order['customer']['email']) + ($sort = 'DESC'), + ['=EMAIL' => $order['customer']['email']] ); switch ($dbUser->SelectedRowsCount()) { @@ -429,8 +434,9 @@ class RetailCrmHistory $registerNewUser = false; break; default: - $login = uniqid('user_' . time()) . '@example.com'; - $corporateCustomerBuilder->setLogin($login); + $lastBitrixUser = $dbUser->Fetch(); + $registeredUserID = $lastBitrixUser['ID']; + $registerNewUser = false; break; } @@ -883,8 +889,8 @@ class RetailCrmHistory $registerNewUser = true; $dbUser = CUser::GetList( ($by = 'ID'), - ($sort = 'ASC'), - array('=EMAIL' => $response['customer']['email']) + ($sort = 'DESC'), + ['=EMAIL' => $response['customer']['email']] ); switch ($dbUser->SelectedRowsCount()) { @@ -899,9 +905,9 @@ class RetailCrmHistory $registerNewUser = false; break; default: - $login = uniqid('user_' . time()) . '@example.com'; - $customerBuilder->setLogin($login); - $customerBuilder->buildPassword(); + $lastBitrixUser = $dbUser->Fetch(); + $registeredUserID = $lastBitrixUser['ID']; + $registerNewUser = false; break; } diff --git a/intaro.retailcrm/description.ru b/intaro.retailcrm/description.ru index 421b395e..905c4eec 100644 --- a/intaro.retailcrm/description.ru +++ b/intaro.retailcrm/description.ru @@ -1 +1 @@ -- Исправление ошибок при установке модуля и выгрузке истории заказов для работы с несколькими сайтами \ No newline at end of file +- Добавлен функционал слияния дублей пользователей по истории diff --git a/intaro.retailcrm/install/version.php b/intaro.retailcrm/install/version.php index 08163ae1..acc6155a 100644 --- a/intaro.retailcrm/install/version.php +++ b/intaro.retailcrm/install/version.php @@ -1,6 +1,6 @@ '6.2.3', - 'VERSION_DATE' => '2023-03-29 18:00:00' + 'VERSION' => '6.2.4', + 'VERSION_DATE' => '2023-03-30 18:00:00' ]; diff --git a/tests/bootstrap.php b/tests/bootstrap.php index d5510b44..7bd87020 100755 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -44,3 +44,4 @@ $DB->Query($strSql); require_once __DIR__ . '/BitrixTestCase.php'; require_once __DIR__ . '/helpers/Helpers.php'; +require_once __DIR__ . '/datasets/DataHistory.php'; diff --git a/tests/classes/general/history/RetailCrmHistory_v5Test.php b/tests/classes/general/history/RetailCrmHistory_v5Test.php index b2277517..89716a38 100644 --- a/tests/classes/general/history/RetailCrmHistory_v5Test.php +++ b/tests/classes/general/history/RetailCrmHistory_v5Test.php @@ -2,6 +2,8 @@ use Bitrix\Sale\Order; use Bitrix\Currency\CurrencyManager; +use Tests\Intaro\RetailCrm\DataHistory; + /** * Class RetailCrmHistory_v5Test */ @@ -18,6 +20,82 @@ class RetailCrmHistory_v5Test extends \BitrixTestCase CModule::IncludeModule('intaro.retailcrm'); } + /** + * @runInSeparateProcess + * @preserveGlobalState disabled + */ + public function testRegisterUser(): void + { + $actionsMock = Mockery::mock('alias:' . RCrmActions::class); + + $actionsMock->shouldReceive('apiMethod')->withAnyArgs()->andReturn(DataHistory::get_history_data_new_customer()); + + $this->deleteTestingUser(); + RetailCrmHistory::customerHistory(); + + $dbUser = CUser::GetList(($by = 'ID'), ($sort = 'DESC'), array('=EMAIL' => 'testbitrixreg@gmail.com')); + + $this->assertEquals(1, $dbUser->SelectedRowsCount()); + } + + /** + * @runInSeparateProcess + * @preserveGlobalState disabled + */ + public function testUnregisterDoubleUser(): void + { + $this->deleteTestingUser(); + + $user = new CUser; + $arFields = [ + 'NAME' => 'test', + 'LAST_NAME' => 'test', + 'LOGIN' => 'test', + 'EMAIL' => 'testbitrixreg@gmail.com', + 'LID' => 'ru', + 'ACTIVE' => 'Y', + 'GROUP_ID' => [10, 11], + 'PASSWORD' => '123456', + 'CONFIRM_PASSWORD' => '123456', + ]; + + $ID = $user->Add($arFields); + + $this->assertTrue((int) $ID > 0); + + $actionsMock = Mockery::mock('alias:' . RCrmActions::class); + $actionsMock->shouldReceive('apiMethod')->withAnyArgs()->andReturn(DataHistory::get_history_data_new_customer()); + + RetailCrmHistory::customerHistory(); + + $dbUser = CUser::GetList(($by = 'ID'), ($sort = 'DESC'), array('=EMAIL' => 'testbitrixreg@gmail.com')); + + $this->assertEquals(1, $dbUser->SelectedRowsCount()); + + $user = new CUser; + $arFields = [ + 'NAME' => 'test2', + 'LAST_NAME' => 'test2', + 'LOGIN' => 'test2', + 'EMAIL' => 'testbitrixreg@gmail.com', + 'LID' => 'ru', + 'ACTIVE' => 'Y', + 'GROUP_ID' => [10, 11], + 'PASSWORD' => '123456', + 'CONFIRM_PASSWORD' => '123456', + ]; + + $ID = $user->Add($arFields); + + $this->assertTrue((int) $ID > 0); + + RetailCrmHistory::customerHistory(); + + $dbUser = CUser::GetList(($by = 'ID'), ($sort = 'DESC'), array('=EMAIL' => 'testbitrixreg@gmail.com')); + + $this->assertEquals(2, $dbUser->SelectedRowsCount()); + } + public function testSetPasswordUser(): void { $customers = $this->getCustomers(); @@ -101,6 +179,17 @@ class RetailCrmHistory_v5Test extends \BitrixTestCase $this->assertEquals(1515, $cmsOrder->getField('RESPONSIBLE_ID')); } + private function deleteTestingUser(): void + { + $dbUser = CUser::GetList(($by = 'ID'), ($sort = 'DESC'), array('=EMAIL' => 'testbitrixreg@gmail.com')); + + if ($dbUser->SelectedRowsCount() > 0) { + while ($user = $dbUser->Fetch()) { + CUser::Delete((int) $user['ID']); + } + } + } + private function getCustomers(): array { return [ @@ -126,4 +215,4 @@ class RetailCrmHistory_v5Test extends \BitrixTestCase ], ]; } -} \ No newline at end of file +} diff --git a/tests/datasets/DataHistory.php b/tests/datasets/DataHistory.php new file mode 100644 index 00000000..b1702881 --- /dev/null +++ b/tests/datasets/DataHistory.php @@ -0,0 +1,64 @@ + true, + 'history' => [ + [ + 'id' => 18009, + 'createdAt' => '2021-12-03 13:22:45', + 'created' => true, + 'source' => 'user', + 'user' => ['id' => 11], + 'field' => 'id', + 'oldValue' => null, + 'newValue' => 3758, + 'customer' => [ + 'type' => 'customer', + 'id' => 3758, + 'isContact' => false, + 'createdAt' => '2021-12-03 13:22:45', + 'vip' => false, + 'bad' => false, + 'site' => 'bitrix', + 'marginSumm' => 0, + 'totalSumm' => 0, + 'averageSumm' => 0, + 'ordersCount' => 0, + 'personalDiscount' => 0, + 'cumulativeDiscount' => 0, + 'address' => [ + 'id' => 3503, + 'index' => 123456, + 'countryIso' => 'ES', + 'region' => 'Region', + 'city' => 'City', + 'text' => 'Street', + ], + 'customFields' => ['crm_customer' => 'test_customer'], + 'segments' => [], + 'firstName' => 'Test_Name', + 'lastName' => 'Test_LastName', + 'email' => 'testbitrixreg@gmail.com', + 'phones' => ['0' => ['number' => '+79184563200']], + 'birthday' => '2021-10-01' + ] + ] + ], + 'pagination' => [ + 'limit' => 100, + 'totalCount' => 1, + 'currentPage' => 1, + 'totalPageCount' => 1 + ] + ]; + } +}