From f926b59e36c00b1c9f3a50bb995fb57029ed498b Mon Sep 17 00:00:00 2001 From: Uryvskiy Dima Date: Tue, 16 Jan 2024 14:00:43 +0300 Subject: [PATCH 1/4] Add history method for task --- src/Model/Entity/Tasks/TaskComment.php | 29 +++++ src/Model/Entity/Tasks/TaskHistory.php | 107 ++++++++++++++++++ src/Model/Filter/Tasks/TaskHistoryFilter.php | 53 +++++++++ .../Request/Tasks/TaskHistoryRequest.php | 28 +++++ .../Response/Tasks/TasksHistoryResponse.php | 32 ++++++ src/ResourceGroup/Tasks.php | 64 +++++++++++ 6 files changed, 313 insertions(+) create mode 100644 src/Model/Entity/Tasks/TaskComment.php create mode 100644 src/Model/Entity/Tasks/TaskHistory.php create mode 100644 src/Model/Filter/Tasks/TaskHistoryFilter.php create mode 100644 src/Model/Request/Tasks/TaskHistoryRequest.php create mode 100644 src/Model/Response/Tasks/TasksHistoryResponse.php diff --git a/src/Model/Entity/Tasks/TaskComment.php b/src/Model/Entity/Tasks/TaskComment.php new file mode 100644 index 0000000..a394c10 --- /dev/null +++ b/src/Model/Entity/Tasks/TaskComment.php @@ -0,0 +1,29 @@ +") + * @JMS\SerializedName("createdAt") + */ + public $createdAt; + + /** + * @var bool + * + * @JMS\Type("bool") + * @JMS\SerializedName("created") + */ + public $created; + + /** + * @var string + * + * @JMS\Type("string") + * @JMS\SerializedName("source") + */ + public $source; + + /** + * @var \RetailCrm\Api\Model\Entity\HistoryUser + * + * @JMS\Type("RetailCrm\Api\Model\Entity\HistoryUser") + * @JMS\SerializedName("user") + */ + public $user; + + /** + * @var string + * + * @JMS\Type("string") + * @JMS\SerializedName("field") + */ + public $field; + + /** + * @var mixed + * + * @JMS\Type("mixed") + * @JMS\SerializedName("oldValue") + */ + public $oldValue; + + /** + * @var mixed + * + * @JMS\Type("mixed") + * @JMS\SerializedName("newValue") + */ + public $newValue; + + /** + * @var \RetailCrm\Api\Model\Entity\HistoryApiKey + * + * @JMS\Type("RetailCrm\Api\Model\Entity\HistoryApiKey") + * @JMS\SerializedName("apiKey") + */ + public $apiKey; + + /** + * @var \RetailCrm\Api\Model\Entity\Tasks\Task + * + * @JMS\Type("RetailCrm\Api\Model\Entity\Tasks\Task") + * @JMS\SerializedName("task") + */ + public $task; + + /** + * @var \RetailCrm\Api\Model\Entity\Tasks\TaskComment + * + * @JMS\Type("RetailCrm\Api\Model\Entity\Tasks\TaskComment") + * @JMS\SerializedName("comment") + */ + public $comment; +} diff --git a/src/Model/Filter/Tasks/TaskHistoryFilter.php b/src/Model/Filter/Tasks/TaskHistoryFilter.php new file mode 100644 index 0000000..4186fb4 --- /dev/null +++ b/src/Model/Filter/Tasks/TaskHistoryFilter.php @@ -0,0 +1,53 @@ +") + * @Form\SerializedName("startDate") + */ + public $startDate; + + /** + * @var DateTime|null + * + * @Form\Type("DateTime<'Y-m-d H:i:s'>") + * @Form\SerializedName("endDate") + */ + public $endDate; +} diff --git a/src/Model/Request/Tasks/TaskHistoryRequest.php b/src/Model/Request/Tasks/TaskHistoryRequest.php new file mode 100644 index 0000000..a9de66b --- /dev/null +++ b/src/Model/Request/Tasks/TaskHistoryRequest.php @@ -0,0 +1,28 @@ +") + * @JMS\SerializedName("generatedAt") + */ + public $generatedAt; + + /** + * @var \RetailCrm\Api\Model\Entity\Tasks\TaskHistory[] + * + * @JMS\Type("array") + * @JMS\SerializedName("history") + */ + public $history; +} diff --git a/src/ResourceGroup/Tasks.php b/src/ResourceGroup/Tasks.php index 0f0d21e..5d34bec 100644 --- a/src/ResourceGroup/Tasks.php +++ b/src/ResourceGroup/Tasks.php @@ -10,11 +10,13 @@ namespace RetailCrm\Api\ResourceGroup; use RetailCrm\Api\Enum\RequestMethod; +use RetailCrm\Api\Model\Request\Tasks\TaskHistoryRequest; use RetailCrm\Api\Model\Request\Tasks\TasksCreateRequest; use RetailCrm\Api\Model\Request\Tasks\TasksRequest; use RetailCrm\Api\Model\Response\IdResponse; use RetailCrm\Api\Model\Response\SuccessResponse; use RetailCrm\Api\Model\Response\Tasks\TasksGetResponse; +use RetailCrm\Api\Model\Response\Tasks\TasksHistoryResponse; use RetailCrm\Api\Model\Response\Tasks\TasksResponse; /** @@ -264,4 +266,66 @@ class Tasks extends AbstractApiResourceGroup ); return $response; } + + /** + * Makes GET "/api/v5/tasks/history" request. + * + * Example: + * ```php + * use RetailCrm\Api\Factory\SimpleClientFactory; + * use RetailCrm\Api\Interfaces\ApiExceptionInterface; + * use RetailCrm\Api\Model\Filter\Tasks\TaskHistoryFilter; + * use RetailCrm\Api\Model\Request\Tasks\TaskHistoryRequest; + * + * $client = SimpleClientFactory::createClient('https://test.retailcrm.pro', 'apiKey'); + * + * $request = new TaskHistoryRequest(); + * $request->limit = 100; + * $request->page = 1; + * $request->filter = new TaskHistoryFilter(); + * $request->filter->sinceId = 1111; + * + * try { + * $response = $client->tasks->history($request); + * } catch (ApiExceptionInterface $exception) { + * echo sprintf( + * 'Error from RetailCRM API (status code: %d): %s', + * $exception->getStatusCode(), + * $exception->getMessage() + * ); + * + * if (count($exception->getErrorResponse()->errors) > 0) { + * echo PHP_EOL . 'Errors: ' . implode(', ', $exception->getErrorResponse()->errors); + * } + * + * return; + * } + * + * echo 'Tasks history: ' . print_r($response->history, true); + * ``` + * + * @param \RetailCrm\Api\Model\Request\Tasks\TaskHistoryRequest|null $request + * + * @return \RetailCrm\Api\Model\Response\Tasks\TasksHistoryResponse + * @throws \RetailCrm\Api\Interfaces\ApiExceptionInterface + * @throws \RetailCrm\Api\Interfaces\ClientExceptionInterface + * @throws \RetailCrm\Api\Exception\Api\AccountDoesNotExistException + * @throws \RetailCrm\Api\Exception\Api\ApiErrorException + * @throws \RetailCrm\Api\Exception\Api\MissingCredentialsException + * @throws \RetailCrm\Api\Exception\Api\MissingParameterException + * @throws \RetailCrm\Api\Exception\Api\ValidationException + * @throws \RetailCrm\Api\Exception\Client\HandlerException + * @throws \RetailCrm\Api\Exception\Client\HttpClientException + */ + public function history(?TaskHistoryRequest $request = null): TasksHistoryResponse + { + /** @var TasksHistoryResponse $response */ + $response = $this->sendRequest( + RequestMethod::GET, + 'tasks/history', + $request, + TasksHistoryResponse::class + ); + return $response; + } } From f37434af1781c6db5db687fc5a8bc72235ec769c Mon Sep 17 00:00:00 2001 From: Uryvskiy Dima Date: Tue, 16 Jan 2024 18:09:05 +0300 Subject: [PATCH 2/4] Add test --- src/Model/Entity/Tasks/TaskComment.php | 2 + src/Model/Entity/Tasks/TaskHistory.php | 3 ++ src/Model/Filter/Tasks/TaskHistoryFilter.php | 1 + .../Request/Tasks/TaskHistoryRequest.php | 2 +- .../Response/Tasks/TasksHistoryResponse.php | 1 + tests/src/ResourceGroup/TasksTest.php | 52 +++++++++++++++++++ 6 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/Model/Entity/Tasks/TaskComment.php b/src/Model/Entity/Tasks/TaskComment.php index a394c10..ff163ec 100644 --- a/src/Model/Entity/Tasks/TaskComment.php +++ b/src/Model/Entity/Tasks/TaskComment.php @@ -9,6 +9,8 @@ namespace RetailCrm\Api\Model\Entity\Tasks; +use RetailCrm\Api\Component\Serializer\Annotation as JMS; + class TaskComment { /** diff --git a/src/Model/Entity/Tasks/TaskHistory.php b/src/Model/Entity/Tasks/TaskHistory.php index 2791408..52457c7 100644 --- a/src/Model/Entity/Tasks/TaskHistory.php +++ b/src/Model/Entity/Tasks/TaskHistory.php @@ -9,6 +9,9 @@ namespace RetailCrm\Api\Model\Entity\Tasks; +use DateTime; +use RetailCrm\Api\Component\Serializer\Annotation as JMS; + /** * Class TaskHistory * diff --git a/src/Model/Filter/Tasks/TaskHistoryFilter.php b/src/Model/Filter/Tasks/TaskHistoryFilter.php index 4186fb4..15d09d4 100644 --- a/src/Model/Filter/Tasks/TaskHistoryFilter.php +++ b/src/Model/Filter/Tasks/TaskHistoryFilter.php @@ -10,6 +10,7 @@ namespace RetailCrm\Api\Model\Filter\Tasks; use DateTime; +use RetailCrm\Api\Component\FormData\Mapping as Form; /** * Class TaskHistoryFilter diff --git a/src/Model/Request/Tasks/TaskHistoryRequest.php b/src/Model/Request/Tasks/TaskHistoryRequest.php index a9de66b..45fc7a6 100644 --- a/src/Model/Request/Tasks/TaskHistoryRequest.php +++ b/src/Model/Request/Tasks/TaskHistoryRequest.php @@ -12,10 +12,10 @@ namespace RetailCrm\Api\Model\Request\Tasks; use RetailCrm\Api\Interfaces\RequestInterface; use RetailCrm\Api\Model\Filter\Tasks\TaskHistoryFilter; use RetailCrm\Api\Model\Request\Traits\HistoryPageLimitTrait; +use RetailCrm\Api\Component\FormData\Mapping as Form; class TaskHistoryRequest implements RequestInterface { - use HistoryPageLimitTrait; /** diff --git a/src/Model/Response/Tasks/TasksHistoryResponse.php b/src/Model/Response/Tasks/TasksHistoryResponse.php index 3d8ef02..4f8d4a6 100644 --- a/src/Model/Response/Tasks/TasksHistoryResponse.php +++ b/src/Model/Response/Tasks/TasksHistoryResponse.php @@ -12,6 +12,7 @@ namespace RetailCrm\Api\Model\Response\Tasks; use DateTime; use RetailCrm\Api\Component\Serializer\Annotation as JMS; use RetailCrm\Api\Model\Response\AbstractPaginatedResponse; + class TasksHistoryResponse extends AbstractPaginatedResponse { /** diff --git a/tests/src/ResourceGroup/TasksTest.php b/tests/src/ResourceGroup/TasksTest.php index 0856bd3..1863298 100644 --- a/tests/src/ResourceGroup/TasksTest.php +++ b/tests/src/ResourceGroup/TasksTest.php @@ -14,6 +14,8 @@ use RetailCrm\Api\Enum\Tasks\TaskStatus; use RetailCrm\Api\Model\Entity\Tasks\AbstractCustomer; use RetailCrm\Api\Model\Entity\Tasks\Task; use RetailCrm\Api\Model\Filter\Tasks\TaskFilter; +use RetailCrm\Api\Model\Filter\Tasks\TaskHistoryFilter; +use RetailCrm\Api\Model\Request\Tasks\TaskHistoryRequest; use RetailCrm\Api\Model\Request\Tasks\TasksCreateRequest; use RetailCrm\Api\Model\Request\Tasks\TasksRequest; use RetailCrm\TestUtils\Factory\TestClientFactory; @@ -175,4 +177,54 @@ EOF; self::assertModelEqualsToResponse($json, $response); } + + public function testHistory(): void + { + $json = <<<'EOF' +{ + "success":true, + "history":[ + { + "id":1, + "createdAt":"2023-03-22 19:00:29", + "created":true, + "source":"rule", + "field":"id", + "oldValue":null, + "newValue":1, + "task":{ + "id":95978, + "text":"", + "commentary":"", + "createdAt":"2023-03-22 19:00:29", + "complete":false, + "performer":116, + "performerType":"user", + "customer":{ + "type":"customer", + "id":1 + } + } + } + ] +} +EOF; + + $mock = static::createApiMockBuilder('tasks/1'); + $mock->matchMethod(RequestMethod::GET) + ->reply(200) + ->withBody($json); + + $client = TestClientFactory::createClient($mock->getClient()); + + $request = new TaskHistoryRequest(); + $request->limit = 100; + $request->page = 1; + $request->filter = new TaskHistoryFilter(); + $request->filter->sinceId = 1; + + $response = $client->tasks->history($request); + + self::assertModelEqualsToResponse($json, $response); + } } From 0fce4a49db6ffec02d786cbc00080f1bde7c5843 Mon Sep 17 00:00:00 2001 From: Uryvskiy Dima Date: Tue, 16 Jan 2024 23:47:15 +0300 Subject: [PATCH 3/4] Fix tests --- src/Model/Entity/Tasks/TaskComment.php | 4 +-- src/ResourceGroup/Tasks.php | 3 +- tests/src/ResourceGroup/TasksTest.php | 40 +++++++++++++------------- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/Model/Entity/Tasks/TaskComment.php b/src/Model/Entity/Tasks/TaskComment.php index ff163ec..6f9295b 100644 --- a/src/Model/Entity/Tasks/TaskComment.php +++ b/src/Model/Entity/Tasks/TaskComment.php @@ -3,8 +3,8 @@ /** * PHP version 7.3 * - * @category TaskHistory - * @package RetailCrm\Api\Model\Entity\TaskComment + * @category TaskComment + * @package RetailCrm\Api\Model\Entity\T */ namespace RetailCrm\Api\Model\Entity\Tasks; diff --git a/src/ResourceGroup/Tasks.php b/src/ResourceGroup/Tasks.php index 5d34bec..d9aaf42 100644 --- a/src/ResourceGroup/Tasks.php +++ b/src/ResourceGroup/Tasks.php @@ -283,7 +283,7 @@ class Tasks extends AbstractApiResourceGroup * $request->limit = 100; * $request->page = 1; * $request->filter = new TaskHistoryFilter(); - * $request->filter->sinceId = 1111; + * $request->filter->sinceId = 1; * * try { * $response = $client->tasks->history($request); @@ -326,6 +326,7 @@ class Tasks extends AbstractApiResourceGroup $request, TasksHistoryResponse::class ); + return $response; } } diff --git a/tests/src/ResourceGroup/TasksTest.php b/tests/src/ResourceGroup/TasksTest.php index 1863298..a21f10d 100644 --- a/tests/src/ResourceGroup/TasksTest.php +++ b/tests/src/ResourceGroup/TasksTest.php @@ -185,24 +185,24 @@ EOF; "success":true, "history":[ { - "id":1, - "createdAt":"2023-03-22 19:00:29", - "created":true, - "source":"rule", - "field":"id", - "oldValue":null, - "newValue":1, - "task":{ - "id":95978, - "text":"", - "commentary":"", - "createdAt":"2023-03-22 19:00:29", - "complete":false, - "performer":116, - "performerType":"user", + "id": 1, + "createdAt": "2023-03-22 19:00:29", + "created": true, + "source": "rule", + "field": "id", + "oldValue": null, + "newValue": 1, + "task": { + "id": 1, + "text": "", + "commentary": "", + "createdAt": "2023-03-22 19:00:29", + "complete": false, + "performer": 2, + "performerType": "user", "customer":{ - "type":"customer", - "id":1 + "type": "customer", + "id": 1 } } } @@ -210,9 +210,9 @@ EOF; } EOF; - $mock = static::createApiMockBuilder('tasks/1'); + $mock = static::createApiMockBuilder('tasks/history'); $mock->matchMethod(RequestMethod::GET) - ->reply(200) + ->reply() ->withBody($json); $client = TestClientFactory::createClient($mock->getClient()); @@ -225,6 +225,6 @@ EOF; $response = $client->tasks->history($request); - self::assertModelEqualsToResponse($json, $response); + self::assertModelEqualsToResponse($json, $response, true); } } From a2d70356558715a0021558a76b0fdcf252939340 Mon Sep 17 00:00:00 2001 From: Uryvskiy Dima Date: Wed, 17 Jan 2024 00:44:40 +0300 Subject: [PATCH 4/4] Add description --- src/Model/Entity/Tasks/TaskComment.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model/Entity/Tasks/TaskComment.php b/src/Model/Entity/Tasks/TaskComment.php index 6f9295b..0ec4c1b 100644 --- a/src/Model/Entity/Tasks/TaskComment.php +++ b/src/Model/Entity/Tasks/TaskComment.php @@ -4,7 +4,7 @@ * PHP version 7.3 * * @category TaskComment - * @package RetailCrm\Api\Model\Entity\T + * @package RetailCrm\Api\Model\Entity\Tasks */ namespace RetailCrm\Api\Model\Entity\Tasks;