diff --git a/src/Model/Entity/Tasks/TaskComment.php b/src/Model/Entity/Tasks/TaskComment.php new file mode 100644 index 0000000..0ec4c1b --- /dev/null +++ b/src/Model/Entity/Tasks/TaskComment.php @@ -0,0 +1,31 @@ +") + * @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..15d09d4 --- /dev/null +++ b/src/Model/Filter/Tasks/TaskHistoryFilter.php @@ -0,0 +1,54 @@ +") + * @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..45fc7a6 --- /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..d9aaf42 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,67 @@ 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 = 1; + * + * 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; + } } diff --git a/tests/src/ResourceGroup/TasksTest.php b/tests/src/ResourceGroup/TasksTest.php index 0856bd3..a21f10d 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": 1, + "text": "", + "commentary": "", + "createdAt": "2023-03-22 19:00:29", + "complete": false, + "performer": 2, + "performerType": "user", + "customer":{ + "type": "customer", + "id": 1 + } + } + } + ] +} +EOF; + + $mock = static::createApiMockBuilder('tasks/history'); + $mock->matchMethod(RequestMethod::GET) + ->reply() + ->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, true); + } }