From c5828e33a232d320384023ea7638b8920904491b Mon Sep 17 00:00:00 2001 From: Uryvskiy Dima Date: Tue, 25 Apr 2023 10:58:07 +0300 Subject: [PATCH] ref #89483 The 'page' parameter has been removed from the history API methods (#211) --- CHANGELOG.md | 3 + VERSION | 2 +- doc/2. Workflow/CLI & Job Manager/Events.md | 4 ++ .../CLI & Job Manager/Job Manager.md | 4 -- retailcrm/lib/RetailcrmHistory.php | 47 ++++++------- retailcrm/lib/api/RetailcrmApiClientV5.php | 28 +++----- .../lib/api/RetailcrmApiPaginatedRequest.php | 12 ++-- retailcrm/retailcrm.php | 2 +- tests/lib/RetailcrmHistoryTest.php | 69 +++++++++++++++++++ .../api/RetailcrmApiPaginatedRequestTest.php | 2 +- 10 files changed, 113 insertions(+), 60 deletions(-) create mode 100644 doc/2. Workflow/CLI & Job Manager/Events.md diff --git a/CHANGELOG.md b/CHANGELOG.md index d5c3eb6..4d4b40a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## v3.5.3 +* Оптимизирован алгоритм получения истории заказов и клиентов + ## v3.5.2 * Добавлен признак корзины при создании заказа diff --git a/VERSION b/VERSION index 87ce492..444877d 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.5.2 +3.5.3 diff --git a/doc/2. Workflow/CLI & Job Manager/Events.md b/doc/2. Workflow/CLI & Job Manager/Events.md new file mode 100644 index 0000000..08de38e --- /dev/null +++ b/doc/2. Workflow/CLI & Job Manager/Events.md @@ -0,0 +1,4 @@ +# Events + +**RetailcrmUpdateSinceIdEvent** - обновляет значение sinceId на актуальное. +При запуске события, модуль получает историю за сутки и проставляет последнее значение sinceId для истории заказов и клиентов. \ No newline at end of file diff --git a/doc/2. Workflow/CLI & Job Manager/Job Manager.md b/doc/2. Workflow/CLI & Job Manager/Job Manager.md index f5dc409..79ea228 100644 --- a/doc/2. Workflow/CLI & Job Manager/Job Manager.md +++ b/doc/2. Workflow/CLI & Job Manager/Job Manager.md @@ -1,6 +1,2 @@ # Job Manager -## Force option - -Режим принудительного запуска позволяет запустить задачу независимо от того, запущена ли сейчас другая задача через Job -Manager. Запущенная таким образом задача не блокирует работу Job Manager diff --git a/retailcrm/lib/RetailcrmHistory.php b/retailcrm/lib/RetailcrmHistory.php index 73378dd..4027162 100755 --- a/retailcrm/lib/RetailcrmHistory.php +++ b/retailcrm/lib/RetailcrmHistory.php @@ -92,6 +92,7 @@ class RetailcrmHistory ; $historyChanges = []; + if (0 < count($history)) { $historyChanges = static::filterHistory($history, 'customer'); $end = end($history); @@ -245,9 +246,11 @@ class RetailcrmHistory ; $historyChanges = []; + if (0 < count($history)) { $historyChanges = static::filterHistory($history, 'order'); $end = end($history); + Configuration::updateValue('RETAILCRM_LAST_ORDERS_SYNC', $end['id']); } @@ -471,42 +474,32 @@ class RetailcrmHistory return false; } + $lastSinceId = 0; $currentSinceID = Configuration::get($key); RetailcrmLogger::writeDebug(__METHOD__, "Current $entity sinceId: $currentSinceID"); - $historyResponse = call_user_func_array( + $historyResponse = call_user_func( [self::$api, $method], - [ - ['sinceId' => $currentSinceID], - null, - 20, - ] + ['startDate' => date('Y-m-d H:i:s', strtotime('-1 days', strtotime(date('Y-m-d H:i:s'))))], + 100 ); - if ($historyResponse instanceof RetailcrmApiResponse && $historyResponse->offsetExists('pagination')) { + if ($historyResponse instanceof RetailcrmApiResponse && !empty($historyResponse['pagination'])) { + $startPage = $historyResponse['pagination']['currentPage']; $lastPage = $historyResponse['pagination']['totalPageCount']; - if (1 < $lastPage) { - $historyResponse = call_user_func_array( - [self::$api, $method], - [ - ['sinceId' => $currentSinceID], - $lastPage, - 20, - ] - ); + + for ($startPage; $startPage <= $lastPage; ++$startPage) { + if ($historyResponse instanceof RetailcrmApiResponse && !empty($historyResponse['history'])) { + $history = $historyResponse['history']; + $lastSinceId = end($history)['id']; + + $historyResponse = call_user_func([self::$api, $method], ['sinceId' => $lastSinceId], 100); + } } - if ($historyResponse instanceof RetailcrmApiResponse - && $historyResponse->offsetExists('history') - && !empty($historyResponse['history']) - ) { - $history = $historyResponse['history']; - $lastSinceId = end($history)['id']; - - if ($currentSinceID !== (string) $lastSinceId) { - RetailcrmLogger::writeDebug(__METHOD__, "Updating to: $lastSinceId"); - Configuration::updateValue($key, $lastSinceId); - } + if (0 !== $lastSinceId && $currentSinceID !== (string) $lastSinceId) { + RetailcrmLogger::writeDebug(__METHOD__, "Updating to: $lastSinceId"); + Configuration::updateValue($key, $lastSinceId); } } diff --git a/retailcrm/lib/api/RetailcrmApiClientV5.php b/retailcrm/lib/api/RetailcrmApiClientV5.php index 39f74f3..443dd47 100644 --- a/retailcrm/lib/api/RetailcrmApiClientV5.php +++ b/retailcrm/lib/api/RetailcrmApiClientV5.php @@ -745,21 +745,18 @@ class RetailcrmApiClientV5 * Get orders history * * @param array $filter - * @param null $page * @param null $limit * * @return RetailcrmApiResponse */ - public function ordersHistory(array $filter = [], $page = null, $limit = null) + public function ordersHistory(array $filter = [], $limit = null) { $parameters = []; if (count($filter)) { $parameters['filter'] = $filter; } - if (null !== $page) { - $parameters['page'] = (int) $page; - } + if (null !== $limit) { $parameters['limit'] = (int) $limit; } @@ -1070,21 +1067,18 @@ class RetailcrmApiClientV5 * Get customers history * * @param array $filter - * @param null $page * @param null $limit * * @return RetailcrmApiResponse */ - public function customersHistory(array $filter = [], $page = null, $limit = null) + public function customersHistory(array $filter = [], $limit = null) { $parameters = []; if (count($filter)) { $parameters['filter'] = $filter; } - if (null !== $page) { - $parameters['page'] = (int) $page; - } + if (null !== $limit) { $parameters['limit'] = (int) $limit; } @@ -1387,21 +1381,18 @@ class RetailcrmApiClientV5 * Get corporate customers history * * @param array $filter - * @param null $page * @param null $limit * * @return RetailcrmApiResponse */ - public function customersCorporateHistory(array $filter = [], $page = null, $limit = null) + public function customersCorporateHistory(array $filter = [], $limit = null) { $parameters = []; if (count($filter)) { $parameters['filter'] = $filter; } - if (null !== $page) { - $parameters['page'] = (int) $page; - } + if (null !== $limit) { $parameters['limit'] = (int) $limit; } @@ -1971,7 +1962,6 @@ class RetailcrmApiClientV5 * Get orders assembly history * * @param array $filter (default: array()) - * @param int $page (default: null) * @param int $limit (default: null) * * @return RetailcrmApiResponse @@ -1980,16 +1970,14 @@ class RetailcrmApiClientV5 * @throws \RetailCrm\Exception\CurlException * @throws \RetailCrm\Exception\InvalidJsonException */ - public function ordersPacksHistory(array $filter = [], $page = null, $limit = null) + public function ordersPacksHistory(array $filter = [], $limit = null) { $parameters = []; if (count($filter)) { $parameters['filter'] = $filter; } - if (null !== $page) { - $parameters['page'] = (int) $page; - } + if (null !== $limit) { $parameters['limit'] = (int) $limit; } diff --git a/retailcrm/lib/api/RetailcrmApiPaginatedRequest.php b/retailcrm/lib/api/RetailcrmApiPaginatedRequest.php index 94bdae7..2b5ab36 100644 --- a/retailcrm/lib/api/RetailcrmApiPaginatedRequest.php +++ b/retailcrm/lib/api/RetailcrmApiPaginatedRequest.php @@ -134,13 +134,12 @@ class RetailcrmApiPaginatedRequest extends RetailcrmApiRequest $page = 1; do { - $response = call_user_func_array( - [$this->api, $this->method], - $this->buildParams($this->params, $page) - ); + $response = call_user_func_array([$this->api, $this->method], $this->buildParams($this->params, $page)); if ($response instanceof RetailcrmApiResponse && $response->offsetExists($this->dataKey)) { - $this->data = array_merge($this->data, $response[$this->dataKey]); + foreach ($response[$this->dataKey] as $data) { + $this->data[] = $data; + } $page = $this->getNextPageNumber($page, $response); } @@ -182,6 +181,7 @@ class RetailcrmApiPaginatedRequest extends RetailcrmApiRequest protected function buildParams($placeholderParams, $currentPage) { foreach ($placeholderParams as $key => $param) { + // Set page and limit for customersCorporateAddresses method if ('{{page}}' == $param) { $placeholderParams[$key] = $currentPage; } @@ -195,7 +195,7 @@ class RetailcrmApiPaginatedRequest extends RetailcrmApiRequest } /** - * Get the next page number from the response + * Get the next page number from the response. Use for customersCorporateAddresses method * * @param int $page * @param RetailcrmApiResponse $response diff --git a/retailcrm/retailcrm.php b/retailcrm/retailcrm.php index 1f6e73c..d0f635b 100644 --- a/retailcrm/retailcrm.php +++ b/retailcrm/retailcrm.php @@ -48,7 +48,7 @@ require_once dirname(__FILE__) . '/bootstrap.php'; class RetailCRM extends Module { - const VERSION = '3.5.2'; + const VERSION = '3.5.3'; const API_URL = 'RETAILCRM_ADDRESS'; const API_KEY = 'RETAILCRM_API_TOKEN'; diff --git a/tests/lib/RetailcrmHistoryTest.php b/tests/lib/RetailcrmHistoryTest.php index 8a59d9d..2578b03 100644 --- a/tests/lib/RetailcrmHistoryTest.php +++ b/tests/lib/RetailcrmHistoryTest.php @@ -196,6 +196,33 @@ class RetailcrmHistoryTest extends RetailcrmTestCase $this->assertEquals($updReference, $secondUpdOrder->reference); } + public function testLastSinceId() + { + RetailcrmHistory::$default_lang = (int) Configuration::get('PS_LANG_DEFAULT'); + RetailcrmHistory::$api = $this->apiMock; + + $this->apiClientMock->expects($this->any()) + ->method('ordersHistory') + ->willReturn(new RetailcrmApiResponse('200', json_encode($this->getLastSinceId()))) + ; + + $this->apiClientMock->expects($this->any()) + ->method('customersHistory') + ->willReturn(new RetailcrmApiResponse('200', json_encode($this->getLastSinceId()))) + ; + + $lastSinceId = 0; + + Configuration::updateValue('RETAILCRM_LAST_ORDERS_SYNC', $lastSinceId); + Configuration::updateValue('RETAILCRM_LAST_CUSTOMERS_SYNC', $lastSinceId); + + $this->assertTrue(RetailcrmHistory::updateSinceId('orders')); + $this->assertTrue(RetailcrmHistory::updateSinceId('customers')); + $this->assertNotEquals($lastSinceId, Configuration::get('RETAILCRM_LAST_ORDERS_SYNC')); + $this->assertNotEquals($lastSinceId, Configuration::get('RETAILCRM_LAST_CUSTOMERS_SYNC')); + $this->assertFalse(RetailcrmHistory::updateSinceId('test_test')); + } + public function orderCreateDataProvider() { return [ @@ -1201,4 +1228,46 @@ class RetailcrmHistoryTest extends RetailcrmTestCase return $order; } + + private function getLastSinceId() + { + return [ + 'success' => true, + 'history' => [ + [ + 'id' => 1, + 'createdAt' => '2018-01-01 00:00:00', + 'created' => true, + 'source' => 'api', + 'field' => 'id', + 'oldValue' => null, + 'newValue' => 4949, + ], + [ + 'id' => 2, + 'createdAt' => '2018-02-01 00:00:00', + 'created' => true, + 'source' => 'api', + 'field' => 'id', + 'oldValue' => null, + 'newValue' => 5050, + ], + [ + 'id' => 3, + 'createdAt' => '2018-03-01 00:00:00', + 'created' => true, + 'source' => 'api', + 'field' => 'id', + 'oldValue' => null, + 'newValue' => 5151, + ], + ], + 'pagination' => [ + 'limit' => 100, + 'totalCount' => 1, + 'currentPage' => 1, + 'totalPageCount' => 1, + ], + ]; + } } diff --git a/tests/lib/api/RetailcrmApiPaginatedRequestTest.php b/tests/lib/api/RetailcrmApiPaginatedRequestTest.php index 6bcdddd..0f57af3 100644 --- a/tests/lib/api/RetailcrmApiPaginatedRequestTest.php +++ b/tests/lib/api/RetailcrmApiPaginatedRequestTest.php @@ -45,7 +45,7 @@ class RetailcrmApiPaginatedRequestTest extends RetailcrmApiRequestTestAbstract return $request ->setApi($this->apiMock) ->setMethod('ordersHistory') - ->setParams([[], '{{page}}']) + ->setParams([[]]) ->setDataKey('history') ->setLimit($limit) ->setPageLimit($pageLimit)