Skip to content

Commit 8ddc49c

Browse files
committed
Added a method for get task comments
1 parent 7263656 commit 8ddc49c

File tree

7 files changed

+236
-10
lines changed

7 files changed

+236
-10
lines changed

src/Model/Entity/Tasks/TaskComment.php

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,21 @@
33
/**
44
* PHP version 7.3
55
*
6-
* @category TaskComment
6+
* @category TaskCommentHistory
77
* @package RetailCrm\Api\Model\Entity\Tasks
88
*/
99

1010
namespace RetailCrm\Api\Model\Entity\Tasks;
1111

12+
use DateTime;
1213
use RetailCrm\Api\Component\Serializer\Annotation as JMS;
1314

15+
/**
16+
* Class TaskCommentHistory
17+
*
18+
* @category TaskHistory
19+
* @package RetailCrm\Api\Model\Entity\Tasks
20+
*/
1421
class TaskComment
1522
{
1623
/**
@@ -25,7 +32,31 @@ class TaskComment
2532
* @var string
2633
*
2734
* @JMS\Type("string")
28-
* @JMS\SerializedName("comment")
35+
* @JMS\SerializedName("text")
36+
*/
37+
public $text;
38+
39+
/**
40+
* @var int
41+
*
42+
* @JMS\Type("int")
43+
* @JMS\SerializedName("creator")
44+
*/
45+
public $creator;
46+
47+
/**
48+
* @var DateTime
49+
*
50+
* @JMS\Type("DateTime<'Y-m-d H:i:s'>")
51+
* @JMS\SerializedName("createdAt")
52+
*/
53+
public $createdAt;
54+
55+
/**
56+
* @var DateTime
57+
*
58+
* @JMS\Type("DateTime<'Y-m-d H:i:s'>")
59+
* @JMS\SerializedName("updatedAt")
2960
*/
30-
public $comment;
61+
public $updatedAt;
3162
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/**
4+
* PHP version 7.3
5+
*
6+
* @category TaskCommentHistory
7+
* @package RetailCrm\Api\Model\Entity\Tasks
8+
*/
9+
10+
namespace RetailCrm\Api\Model\Entity\Tasks;
11+
12+
use RetailCrm\Api\Component\Serializer\Annotation as JMS;
13+
14+
class TaskCommentHistory
15+
{
16+
/**
17+
* @var int
18+
*
19+
* @JMS\Type("int")
20+
* @JMS\SerializedName("id")
21+
*/
22+
public $id;
23+
24+
/**
25+
* @var string
26+
*
27+
* @JMS\Type("string")
28+
* @JMS\SerializedName("text")
29+
*/
30+
public $text;
31+
}

src/Model/Entity/Tasks/TaskHistory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ class TaskHistory
101101
public $task;
102102

103103
/**
104-
* @var \RetailCrm\Api\Model\Entity\Tasks\TaskComment
104+
* @var \RetailCrm\Api\Model\Entity\Tasks\TaskCommentHistory
105105
*
106-
* @JMS\Type("RetailCrm\Api\Model\Entity\Tasks\TaskComment")
106+
* @JMS\Type("RetailCrm\Api\Model\Entity\Tasks\TaskCommentHistory")
107107
* @JMS\SerializedName("comment")
108108
*/
109109
public $comment;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/**
4+
* PHP version 7.3
5+
*
6+
* @category TaskGetCommentsRequest
7+
* @package RetailCrm\Api\Model\Request\Tasks
8+
*/
9+
10+
namespace RetailCrm\Api\Model\Request\Tasks;
11+
12+
use RetailCrm\Api\Interfaces\RequestInterface;
13+
use RetailCrm\Api\Model\Request\Traits\PageLimitTrait;
14+
15+
/**
16+
* Class TaskGetCommentsRequest
17+
*
18+
* @category TaskGetCommentsRequest
19+
* @package RetailCrm\Api\Model\Request\Tasks
20+
*/
21+
class TaskGetCommentsRequest implements RequestInterface
22+
{
23+
use PageLimitTrait;
24+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/**
4+
* PHP version 7.3
5+
*
6+
* @category TaskGetCommentsResponse
7+
* @package RetailCrm\Api\Model\Response\Tasks
8+
*/
9+
10+
namespace RetailCrm\Api\Model\Response\Tasks;
11+
12+
use RetailCrm\Api\Model\Entity\Tasks\TaskComment;
13+
use RetailCrm\Api\Model\Response\AbstractPaginatedResponse;
14+
use RetailCrm\Api\Component\Serializer\Annotation as JMS;
15+
16+
/**
17+
* Class TaskGetCommentsResponse
18+
*
19+
* @category TaskGetCommentsRequest
20+
* @package RetailCrm\Api\Model\Response\Tasks
21+
*/
22+
class TaskGetCommentsResponse extends AbstractPaginatedResponse
23+
{
24+
/**
25+
* @var TaskComment[]
26+
*
27+
* @JMS\Type("array<RetailCrm\Api\Model\Entity\Tasks\TaskComment>")
28+
* @JMS\SerializedName("comments")
29+
*/
30+
public $comments;
31+
}

src/ResourceGroup/Tasks.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,17 @@
1010
namespace RetailCrm\Api\ResourceGroup;
1111

1212
use RetailCrm\Api\Enum\RequestMethod;
13+
use RetailCrm\Api\Exception\ApiException;
14+
use RetailCrm\Api\Exception\Client\HandlerException;
15+
use RetailCrm\Api\Exception\ClientException;
16+
use RetailCrm\Api\Interfaces\ApiExceptionInterface;
17+
use RetailCrm\Api\Model\Request\Tasks\TaskGetCommentsRequest;
1318
use RetailCrm\Api\Model\Request\Tasks\TaskHistoryRequest;
1419
use RetailCrm\Api\Model\Request\Tasks\TasksCreateRequest;
1520
use RetailCrm\Api\Model\Request\Tasks\TasksRequest;
1621
use RetailCrm\Api\Model\Response\IdResponse;
1722
use RetailCrm\Api\Model\Response\SuccessResponse;
23+
use RetailCrm\Api\Model\Response\Tasks\TaskGetCommentsResponse;
1824
use RetailCrm\Api\Model\Response\Tasks\TasksGetResponse;
1925
use RetailCrm\Api\Model\Response\Tasks\TasksHistoryResponse;
2026
use RetailCrm\Api\Model\Response\Tasks\TasksResponse;
@@ -329,4 +335,60 @@ public function history(?TaskHistoryRequest $request = null): TasksHistoryRespon
329335

330336
return $response;
331337
}
338+
339+
/**
340+
* Makes GET "/api/v5/tasks/{id}/comments" request.
341+
*
342+
* Example:
343+
* ```php
344+
* use RetailCrm\Api\Factory\SimpleClientFactory;
345+
* use RetailCrm\Api\Interfaces\ApiExceptionInterface;
346+
* use RetailCrm\Api\Model\Request\Tasks\TaskGetCommentsRequest;
347+
*
348+
* $client = SimpleClientFactory::createClient('https://test.retailcrm.pro', 'apiKey');
349+
*
350+
* $request = new TaskGetCommentsRequest();
351+
* $request->limit = 100;
352+
* $request->page = 1;
353+
*
354+
* try {
355+
* $response = $client->tasks->getComments(1, $request);
356+
* } catch (ApiExceptionInterface $exception) {
357+
* echo sprintf(
358+
* 'Error from RetailCRM API (status code: %d): %s',
359+
* $exception->getStatusCode(),
360+
* $exception->getMessage()
361+
* );
362+
*
363+
* if (count($exception->getErrorResponse()->errors) > 0) {
364+
* echo PHP_EOL . 'Errors: ' . implode(', ', $exception->getErrorResponse()->errors);
365+
* }
366+
*
367+
* return;
368+
* }
369+
*
370+
* echo 'Task: ' . print_r($response->task, true);
371+
* ```
372+
*
373+
* @param int $id
374+
* @param TaskGetCommentsRequest $request
375+
*
376+
* @return TaskGetCommentsResponse
377+
* @throws ApiException
378+
* @throws ClientException
379+
* @throws HandlerException
380+
* @throws ApiExceptionInterface
381+
*/
382+
public function getComments(int $id, TaskGetCommentsRequest $request): TaskGetCommentsResponse
383+
{
384+
/** @var TaskGetCommentsResponse $response */
385+
$response = $this->sendRequest(
386+
RequestMethod::GET,
387+
'tasks/' . $id . '/comments',
388+
$request,
389+
TaskGetCommentsResponse::class
390+
);
391+
392+
return $response;
393+
}
332394
}

tests/src/ResourceGroup/TasksTest.php

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use RetailCrm\Api\Model\Entity\Tasks\Task;
1616
use RetailCrm\Api\Model\Filter\Tasks\TaskFilter;
1717
use RetailCrm\Api\Model\Filter\Tasks\TaskHistoryFilter;
18+
use RetailCrm\Api\Model\Request\Tasks\TaskGetCommentsRequest;
1819
use RetailCrm\Api\Model\Request\Tasks\TaskHistoryRequest;
1920
use RetailCrm\Api\Model\Request\Tasks\TasksCreateRequest;
2021
use RetailCrm\Api\Model\Request\Tasks\TasksRequest;
@@ -215,16 +216,62 @@ public function testHistory(): void
215216
->reply()
216217
->withBody($json);
217218

218-
$client = TestClientFactory::createClient($mock->getClient());
219+
$client = TestClientFactory::createClient($mock->getClient());
219220

220-
$request = new TaskHistoryRequest();
221-
$request->limit = 100;
222-
$request->page = 1;
223-
$request->filter = new TaskHistoryFilter();
221+
$request = new TaskHistoryRequest();
222+
$request->limit = 100;
223+
$request->page = 1;
224+
$request->filter = new TaskHistoryFilter();
224225
$request->filter->sinceId = 1;
225226

226227
$response = $client->tasks->history($request);
227228

228229
self::assertModelEqualsToResponse($json, $response, true);
229230
}
231+
232+
public function testGetComments(): void
233+
{
234+
$json = <<<'EOF'
235+
{
236+
"success": true,
237+
"pagination": {
238+
"limit": 20,
239+
"totalCount": 4,
240+
"currentPage": 1,
241+
"totalPageCount": 1
242+
},
243+
"comments": [
244+
{
245+
"id": 1150,
246+
"creator": 1,
247+
"text": "Тест 2",
248+
"createdAt": "2024-02-05 16:58:23",
249+
"updatedAt": "2024-02-05 16:58:23"
250+
},
251+
{
252+
"id": 1149,
253+
"creator": 1,
254+
"text": "Тест 1",
255+
"createdAt": "2024-02-05 16:58:19",
256+
"updatedAt": "2024-02-05 16:58:19"
257+
}
258+
]
259+
}
260+
EOF;
261+
262+
$mock = static::createApiMockBuilder('tasks/1/comments');
263+
$mock->matchMethod(RequestMethod::GET)
264+
->reply()
265+
->withBody($json);
266+
267+
$client = TestClientFactory::createClient($mock->getClient());
268+
269+
$request = new TaskGetCommentsRequest();
270+
$request->limit = 100;
271+
$request->page = 1;
272+
273+
$response = $client->tasks->getComments(1, $request);
274+
275+
self::assertModelEqualsToResponse($json, $response);
276+
}
230277
}

0 commit comments

Comments
 (0)