From 3927344dde4948af18bf7d908131b17dfc2217bd Mon Sep 17 00:00:00 2001 From: Uryvskiy Dima Date: Thu, 8 Feb 2024 12:52:58 +0300 Subject: [PATCH] Add new method for get task comments (#188) --- src/Model/Entity/Tasks/BaseComment.php | 37 +++++++ src/Model/Entity/Tasks/TaskComment.php | 29 ++++-- src/Model/Entity/Tasks/TaskHistory.php | 4 +- .../Request/Tasks/TaskGetCommentsRequest.php | 24 +++++ .../Tasks/TaskGetCommentsResponse.php | 31 ++++++ .../Response/Tasks/TasksHistoryResponse.php | 6 ++ src/ResourceGroup/Tasks.php | 62 ++++++++++++ tests/src/ResourceGroup/TasksTest.php | 97 ++++++++++++++----- 8 files changed, 256 insertions(+), 34 deletions(-) create mode 100644 src/Model/Entity/Tasks/BaseComment.php create mode 100644 src/Model/Request/Tasks/TaskGetCommentsRequest.php create mode 100644 src/Model/Response/Tasks/TaskGetCommentsResponse.php diff --git a/src/Model/Entity/Tasks/BaseComment.php b/src/Model/Entity/Tasks/BaseComment.php new file mode 100644 index 0000000..3ff0189 --- /dev/null +++ b/src/Model/Entity/Tasks/BaseComment.php @@ -0,0 +1,37 @@ +") + * @JMS\SerializedName("createdAt") */ - public $comment; + public $createdAt; + + /** + * @var DateTime + * + * @JMS\Type("DateTime<'Y-m-d H:i:s'>") + * @JMS\SerializedName("updatedAt") + */ + public $updatedAt; } diff --git a/src/Model/Entity/Tasks/TaskHistory.php b/src/Model/Entity/Tasks/TaskHistory.php index 52457c7..62b7b6e 100644 --- a/src/Model/Entity/Tasks/TaskHistory.php +++ b/src/Model/Entity/Tasks/TaskHistory.php @@ -101,9 +101,9 @@ class TaskHistory public $task; /** - * @var \RetailCrm\Api\Model\Entity\Tasks\TaskComment + * @var \RetailCrm\Api\Model\Entity\Tasks\BaseComment * - * @JMS\Type("RetailCrm\Api\Model\Entity\Tasks\TaskComment") + * @JMS\Type("RetailCrm\Api\Model\Entity\Tasks\BaseComment") * @JMS\SerializedName("comment") */ public $comment; diff --git a/src/Model/Request/Tasks/TaskGetCommentsRequest.php b/src/Model/Request/Tasks/TaskGetCommentsRequest.php new file mode 100644 index 0000000..1932cee --- /dev/null +++ b/src/Model/Request/Tasks/TaskGetCommentsRequest.php @@ -0,0 +1,24 @@ +") + * @JMS\SerializedName("comments") + */ + public $comments; +} diff --git a/src/Model/Response/Tasks/TasksHistoryResponse.php b/src/Model/Response/Tasks/TasksHistoryResponse.php index 4f8d4a6..abd07dc 100644 --- a/src/Model/Response/Tasks/TasksHistoryResponse.php +++ b/src/Model/Response/Tasks/TasksHistoryResponse.php @@ -13,6 +13,12 @@ use DateTime; use RetailCrm\Api\Component\Serializer\Annotation as JMS; use RetailCrm\Api\Model\Response\AbstractPaginatedResponse; +/** + * Class TasksHistoryResponse + * + * @category TasksHistoryResponse + * @package RetailCrm\Api\Model\Response\Tasks + */ class TasksHistoryResponse extends AbstractPaginatedResponse { /** diff --git a/src/ResourceGroup/Tasks.php b/src/ResourceGroup/Tasks.php index d9aaf42..0ad74b9 100644 --- a/src/ResourceGroup/Tasks.php +++ b/src/ResourceGroup/Tasks.php @@ -10,11 +10,17 @@ namespace RetailCrm\Api\ResourceGroup; use RetailCrm\Api\Enum\RequestMethod; +use RetailCrm\Api\Exception\ApiException; +use RetailCrm\Api\Exception\Client\HandlerException; +use RetailCrm\Api\Exception\ClientException; +use RetailCrm\Api\Interfaces\ApiExceptionInterface; +use RetailCrm\Api\Model\Request\Tasks\TaskGetCommentsRequest; 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\TaskGetCommentsResponse; use RetailCrm\Api\Model\Response\Tasks\TasksGetResponse; use RetailCrm\Api\Model\Response\Tasks\TasksHistoryResponse; use RetailCrm\Api\Model\Response\Tasks\TasksResponse; @@ -329,4 +335,60 @@ class Tasks extends AbstractApiResourceGroup return $response; } + + /** + * Makes GET "/api/v5/tasks/{id}/comments" request. + * + * Example: + * ```php + * use RetailCrm\Api\Factory\SimpleClientFactory; + * use RetailCrm\Api\Interfaces\ApiExceptionInterface; + * use RetailCrm\Api\Model\Request\Tasks\TaskGetCommentsRequest; + * + * $client = SimpleClientFactory::createClient('https://test.retailcrm.pro', 'apiKey'); + * + * $request = new TaskGetCommentsRequest(); + * $request->limit = 100; + * $request->page = 1; + * + * try { + * $response = $client->tasks->getComments(1, $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 'Task: ' . print_r($response->task, true); + * ``` + * + * @param int $id + * @param TaskGetCommentsRequest $request + * + * @return TaskGetCommentsResponse + * @throws ApiException + * @throws ClientException + * @throws HandlerException + * @throws ApiExceptionInterface + */ + public function getComments(int $id, TaskGetCommentsRequest $request): TaskGetCommentsResponse + { + /** @var TaskGetCommentsResponse $response */ + $response = $this->sendRequest( + RequestMethod::GET, + 'tasks/' . $id . '/comments', + $request, + TaskGetCommentsResponse::class + ); + + return $response; + } } diff --git a/tests/src/ResourceGroup/TasksTest.php b/tests/src/ResourceGroup/TasksTest.php index a21f10d..bfcf707 100644 --- a/tests/src/ResourceGroup/TasksTest.php +++ b/tests/src/ResourceGroup/TasksTest.php @@ -15,6 +15,7 @@ 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\TaskGetCommentsRequest; use RetailCrm\Api\Model\Request\Tasks\TaskHistoryRequest; use RetailCrm\Api\Model\Request\Tasks\TasksCreateRequest; use RetailCrm\Api\Model\Request\Tasks\TasksRequest; @@ -182,31 +183,31 @@ EOF; { $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": 1, - "text": "", - "commentary": "", - "createdAt": "2023-03-22 19:00:29", - "complete": false, - "performer": 2, - "performerType": "user", - "customer":{ - "type": "customer", - "id": 1 - } - } + "success": true, + "history": [ + { + "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 } - ] + } + } + ] } EOF; @@ -215,7 +216,7 @@ EOF; ->reply() ->withBody($json); - $client = TestClientFactory::createClient($mock->getClient()); + $client = TestClientFactory::createClient($mock->getClient()); $request = new TaskHistoryRequest(); $request->limit = 100; @@ -227,4 +228,50 @@ EOF; self::assertModelEqualsToResponse($json, $response, true); } + + public function testGetComments(): void + { + $json = <<<'EOF' +{ + "success": true, + "pagination": { + "limit": 20, + "totalCount": 4, + "currentPage": 1, + "totalPageCount": 1 + }, + "comments": [ + { + "id": 1150, + "creator": 1, + "text": "Тест 2", + "createdAt": "2024-02-05 16:58:23", + "updatedAt": "2024-02-05 16:58:23" + }, + { + "id": 1149, + "creator": 1, + "text": "Тест 1", + "createdAt": "2024-02-05 16:58:19", + "updatedAt": "2024-02-05 16:58:19" + } + ] +} +EOF; + + $mock = static::createApiMockBuilder('tasks/1/comments'); + $mock->matchMethod(RequestMethod::GET) + ->reply() + ->withBody($json); + + $client = TestClientFactory::createClient($mock->getClient()); + + $request = new TaskGetCommentsRequest(); + $request->limit = 100; + $request->page = 1; + + $response = $client->tasks->getComments(1, $request); + + self::assertModelEqualsToResponse($json, $response); + } }