Skip to content

Commit

Permalink
Add new method for get task comments (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
uryvskiy-dima authored Feb 8, 2024
1 parent 7263656 commit 3927344
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 34 deletions.
37 changes: 37 additions & 0 deletions src/Model/Entity/Tasks/BaseComment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* PHP version 7.3
*
* @category BaseComment
* @package RetailCrm\Api\Model\Entity\Tasks
*/

namespace RetailCrm\Api\Model\Entity\Tasks;

use RetailCrm\Api\Component\Serializer\Annotation as JMS;

/**
* Class BaseComment
*
* @category BaseComment
* @package RetailCrm\Api\Model\Entity\Tasks
*/
class BaseComment
{
/**
* @var int
*
* @JMS\Type("int")
* @JMS\SerializedName("id")
*/
public $id;

/**
* @var string
*
* @JMS\Type("string")
* @JMS\SerializedName("text")
*/
public $text;
}
29 changes: 22 additions & 7 deletions src/Model/Entity/Tasks/TaskComment.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,38 @@

namespace RetailCrm\Api\Model\Entity\Tasks;

use DateTime;
use RetailCrm\Api\Component\Serializer\Annotation as JMS;

class TaskComment
/**
* Class TaskComment
*
* @category TaskComment
* @package RetailCrm\Api\Model\Entity\Tasks
*/
class TaskComment extends BaseComment
{
/**
* @var int
*
* @JMS\Type("int")
* @JMS\SerializedName("id")
* @JMS\SerializedName("creator")
*/
public $creator;

/**
* @var DateTime
*
* @JMS\Type("DateTime<'Y-m-d H:i:s'>")
* @JMS\SerializedName("createdAt")
*/
public $id;
public $createdAt;

/**
* @var string
* @var DateTime
*
* @JMS\Type("string")
* @JMS\SerializedName("comment")
* @JMS\Type("DateTime<'Y-m-d H:i:s'>")
* @JMS\SerializedName("updatedAt")
*/
public $comment;
public $updatedAt;
}
4 changes: 2 additions & 2 deletions src/Model/Entity/Tasks/TaskHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
24 changes: 24 additions & 0 deletions src/Model/Request/Tasks/TaskGetCommentsRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* PHP version 7.3
*
* @category TaskGetCommentsRequest
* @package RetailCrm\Api\Model\Request\Tasks
*/

namespace RetailCrm\Api\Model\Request\Tasks;

use RetailCrm\Api\Interfaces\RequestInterface;
use RetailCrm\Api\Model\Request\Traits\PageLimitTrait;

/**
* Class TaskGetCommentsRequest
*
* @category TaskGetCommentsRequest
* @package RetailCrm\Api\Model\Request\Tasks
*/
class TaskGetCommentsRequest implements RequestInterface
{
use PageLimitTrait;
}
31 changes: 31 additions & 0 deletions src/Model/Response/Tasks/TaskGetCommentsResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/**
* PHP version 7.3
*
* @category TaskGetCommentsResponse
* @package RetailCrm\Api\Model\Response\Tasks
*/

namespace RetailCrm\Api\Model\Response\Tasks;

use RetailCrm\Api\Model\Entity\Tasks\TaskComment;
use RetailCrm\Api\Model\Response\AbstractPaginatedResponse;
use RetailCrm\Api\Component\Serializer\Annotation as JMS;

/**
* Class TaskGetCommentsResponse
*
* @category TaskGetCommentsRequest
* @package RetailCrm\Api\Model\Response\Tasks
*/
class TaskGetCommentsResponse extends AbstractPaginatedResponse
{
/**
* @var TaskComment[]
*
* @JMS\Type("array<RetailCrm\Api\Model\Entity\Tasks\TaskComment>")
* @JMS\SerializedName("comments")
*/
public $comments;
}
6 changes: 6 additions & 0 deletions src/Model/Response/Tasks/TasksHistoryResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
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
{
/**
Expand Down
62 changes: 62 additions & 0 deletions src/ResourceGroup/Tasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -329,4 +335,60 @@ public function history(?TaskHistoryRequest $request = null): TasksHistoryRespon

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;
}
}
97 changes: 72 additions & 25 deletions tests/src/ResourceGroup/TasksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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;
Expand Down Expand Up @@ -182,31 +183,31 @@ 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": 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;

Expand All @@ -215,7 +216,7 @@ public function testHistory(): void
->reply()
->withBody($json);

$client = TestClientFactory::createClient($mock->getClient());
$client = TestClientFactory::createClient($mock->getClient());

$request = new TaskHistoryRequest();
$request->limit = 100;
Expand All @@ -227,4 +228,50 @@ public function testHistory(): void

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

0 comments on commit 3927344

Please sign in to comment.