ref #89483 The 'page' parameter has been removed from the history API methods (#211)

This commit is contained in:
Uryvskiy Dima 2023-04-25 10:58:07 +03:00 committed by GitHub
parent de9f0720f9
commit c5828e33a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 113 additions and 60 deletions

View File

@ -1,3 +1,6 @@
## v3.5.3
* Оптимизирован алгоритм получения истории заказов и клиентов
## v3.5.2
* Добавлен признак корзины при создании заказа

View File

@ -1 +1 @@
3.5.2
3.5.3

View File

@ -0,0 +1,4 @@
# Events
**RetailcrmUpdateSinceIdEvent** - обновляет значение sinceId на актуальное.
При запуске события, модуль получает историю за сутки и проставляет последнее значение sinceId для истории заказов и клиентов.

View File

@ -1,6 +1,2 @@
# Job Manager
## Force option
Режим принудительного запуска позволяет запустить задачу независимо от того, запущена ли сейчас другая задача через Job
Manager. Запущенная таким образом задача не блокирует работу Job Manager

View File

@ -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,44 +474,34 @@ 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,
]
);
}
if ($historyResponse instanceof RetailcrmApiResponse
&& $historyResponse->offsetExists('history')
&& !empty($historyResponse['history'])
) {
for ($startPage; $startPage <= $lastPage; ++$startPage) {
if ($historyResponse instanceof RetailcrmApiResponse && !empty($historyResponse['history'])) {
$history = $historyResponse['history'];
$lastSinceId = end($history)['id'];
if ($currentSinceID !== (string) $lastSinceId) {
$historyResponse = call_user_func([self::$api, $method], ['sinceId' => $lastSinceId], 100);
}
}
if (0 !== $lastSinceId && $currentSinceID !== (string) $lastSinceId) {
RetailcrmLogger::writeDebug(__METHOD__, "Updating to: $lastSinceId");
Configuration::updateValue($key, $lastSinceId);
}
}
}
return true;
}

View File

@ -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;
}

View File

@ -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

View File

@ -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';

View File

@ -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,
],
];
}
}

View File

@ -45,7 +45,7 @@ class RetailcrmApiPaginatedRequestTest extends RetailcrmApiRequestTestAbstract
return $request
->setApi($this->apiMock)
->setMethod('ordersHistory')
->setParams([[], '{{page}}'])
->setParams([[]])
->setDataKey('history')
->setLimit($limit)
->setPageLimit($pageLimit)