Skip to content

Commit

Permalink
Added a method for get task comments
Browse files Browse the repository at this point in the history
  • Loading branch information
uryvskiy-dima committed Feb 5, 2024
1 parent 7263656 commit 8ddc49c
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 10 deletions.
37 changes: 34 additions & 3 deletions src/Model/Entity/Tasks/TaskComment.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@
/**
* PHP version 7.3
*
* @category TaskComment
* @category TaskCommentHistory
* @package RetailCrm\Api\Model\Entity\Tasks
*/

namespace RetailCrm\Api\Model\Entity\Tasks;

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

/**
* Class TaskCommentHistory
*
* @category TaskHistory
* @package RetailCrm\Api\Model\Entity\Tasks
*/
class TaskComment
{
/**
Expand All @@ -25,7 +32,31 @@ class TaskComment
* @var string
*
* @JMS\Type("string")
* @JMS\SerializedName("comment")
* @JMS\SerializedName("text")
*/
public $text;

/**
* @var int
*
* @JMS\Type("int")
* @JMS\SerializedName("creator")
*/
public $creator;

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

/**
* @var DateTime
*
* @JMS\Type("DateTime<'Y-m-d H:i:s'>")
* @JMS\SerializedName("updatedAt")
*/
public $comment;
public $updatedAt;
}
31 changes: 31 additions & 0 deletions src/Model/Entity/Tasks/TaskCommentHistory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

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

namespace RetailCrm\Api\Model\Entity\Tasks;

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

class TaskCommentHistory
{
/**
* @var int
*
* @JMS\Type("int")
* @JMS\SerializedName("id")
*/
public $id;

/**
* @var string
*
* @JMS\Type("string")
* @JMS\SerializedName("text")
*/
public $text;
}
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\TaskCommentHistory
*
* @JMS\Type("RetailCrm\Api\Model\Entity\Tasks\TaskComment")
* @JMS\Type("RetailCrm\Api\Model\Entity\Tasks\TaskCommentHistory")
* @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;
}
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;
}
}
57 changes: 52 additions & 5 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 @@ -215,16 +216,62 @@ public function testHistory(): void
->reply()
->withBody($json);

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

$request = new TaskHistoryRequest();
$request->limit = 100;
$request->page = 1;
$request->filter = new TaskHistoryFilter();
$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, 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 8ddc49c

Please sign in to comment.