From 4f1537307472556371ad9deb041a16a72c153df6 Mon Sep 17 00:00:00 2001 From: Alex Komarichev Date: Wed, 28 Feb 2024 14:30:01 +0300 Subject: [PATCH] working with tags --- src/Bot/Client.php | 42 ++++++++++++ src/Bot/Model/Entity/Tag.php | 78 ++++++++++++++++++++++ src/Bot/Model/Request/DialogTagRequest.php | 78 ++++++++++++++++++++++ tests/Bot/Tests/DialogsTest.php | 60 +++++++++++++++++ 4 files changed, 258 insertions(+) create mode 100644 src/Bot/Model/Entity/Tag.php create mode 100644 src/Bot/Model/Request/DialogTagRequest.php diff --git a/src/Bot/Client.php b/src/Bot/Client.php index 24ee1be..fe0b59d 100644 --- a/src/Bot/Client.php +++ b/src/Bot/Client.php @@ -313,6 +313,48 @@ class Client return $adapter->getResponseModel($response); } + /** + * Add tag to dialog + * + * @param Model\Request\DialogTagRequest $request Request parameters + * + * @return \RetailCrm\Mg\Bot\Model\ModelInterface + * @throws \Exception + */ + public function dialogAddTag(Model\Request\DialogTagRequest $request) + { + $response = $this->client->makeRequest( + sprintf("/dialogs/%d/tags/add", $request->getDialogId()), + HttpClient::METHOD_PATCH, + $request + ); + + $adapter = new ModelAdapter(ErrorOnlyResponse::class); + + return $adapter->getResponseModel($response); + } + + /** + * Delete tag from dialog + * + * @param Model\Request\DialogTagRequest $request Request parameters + * + * @return \RetailCrm\Mg\Bot\Model\ModelInterface + * @throws \Exception + */ + public function dialogDeleteTag(Model\Request\DialogTagRequest $request) + { + $response = $this->client->makeRequest( + sprintf("/dialogs/%d/tags/delete", $request->getDialogId()), + HttpClient::METHOD_PATCH, + $request + ); + + $adapter = new ModelAdapter(ErrorOnlyResponse::class); + + return $adapter->getResponseModel($response); + } + /** * Returns filtered members list * diff --git a/src/Bot/Model/Entity/Tag.php b/src/Bot/Model/Entity/Tag.php new file mode 100644 index 0000000..b22e616 --- /dev/null +++ b/src/Bot/Model/Entity/Tag.php @@ -0,0 +1,78 @@ +name; + } + + /** + * @param string $name + * @return void + */ + public function setName(string $name): void + { + $this->name = $name; + } + + /** + * @return string|null + */ + public function getColorCode(): ?string + { + return $this->colorCode; + } + + /** + * @param string $colorCode + * @return void + */ + public function setColorCode(string $colorCode): void + { + $this->colorCode = $colorCode; + } +} \ No newline at end of file diff --git a/src/Bot/Model/Request/DialogTagRequest.php b/src/Bot/Model/Request/DialogTagRequest.php new file mode 100644 index 0000000..26e45c6 --- /dev/null +++ b/src/Bot/Model/Request/DialogTagRequest.php @@ -0,0 +1,78 @@ +dialogId; + } + + /** + * @param int $dialogId + * @return void + */ + public function setDialogId(int $dialogId): void + { + $this->dialogId = $dialogId; + } + + /** + * @return Tag[] + */ + public function getTags(): array + { + return $this->tags; + } + + /** + * @param Tag[] $tags + * @return void + */ + public function setTags(array $tags): void + { + $this->tags = $tags; + } + +} \ No newline at end of file diff --git a/tests/Bot/Tests/DialogsTest.php b/tests/Bot/Tests/DialogsTest.php index 08ee4d3..de990b3 100644 --- a/tests/Bot/Tests/DialogsTest.php +++ b/tests/Bot/Tests/DialogsTest.php @@ -14,7 +14,9 @@ namespace RetailCrm\Mg\Bot\Tests; use InvalidArgumentException; use RetailCrm\Common\Exception\NotFoundException; use RetailCrm\Mg\Bot\Model\Entity\Responsible; +use RetailCrm\Mg\Bot\Model\Entity\Tag; use RetailCrm\Mg\Bot\Model\Request\DialogAssignRequest; +use RetailCrm\Mg\Bot\Model\Request\DialogTagRequest; use RetailCrm\Mg\Bot\Model\Response\ErrorOnlyResponse; use RetailCrm\Mg\Bot\Test\TestCase; @@ -145,4 +147,62 @@ class DialogsTest extends TestCase self::assertTrue($response->isSuccessful()); self::assertEmpty($response->getErrors()); } + + /** + * @group("dialogs") + * @throws \Exception + */ + public function testDialogAddTag() + { + $client = self::getApiClient( + null, + null, + false, + $this->getResponse('{}') + ); + + $tags[0] = new Tag(); + $tags[0]->setName('tag1'); + $tags[0]->setColorCode('red'); + + $tags[1] = new Tag(); + $tags[1]->setName('tag2'); + + $request = new DialogTagRequest(); + $request->setDialogId(60); + $request->setTags($tags); + + $response = $client->dialogAddTag($request); + + self::assertInstanceOF(ErrorOnlyResponse::class, $response); + self::assertTrue($response->isSuccessful()); + self::assertEmpty($response->getErrors()); + } + + /** + * @group("dialogs") + * @throws \Exception + */ + public function testDialogDeleteTag() + { + $client = self::getApiClient( + null, + null, + false, + $this->getResponse('{}') + ); + + $tags[0] = new Tag(); + $tags[0]->setName('tag1'); + + $request = new DialogTagRequest(); + $request->setDialogId(60); + $request->setTags($tags); + + $response = $client->dialogDeleteTag($request); + + self::assertInstanceOF(ErrorOnlyResponse::class, $response); + self::assertTrue($response->isSuccessful()); + self::assertEmpty($response->getErrors()); + } }