Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

working with tags #42

Merged
merged 5 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/Bot/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,48 @@ public function dialogClose(string $request)
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
*
Expand Down
78 changes: 78 additions & 0 deletions src/Bot/Model/Entity/Tag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/**
* PHP version 7.1
*
* Tag entity
*
* @package RetailCrm\Mg\Bot\Model\Entity
*/

namespace RetailCrm\Mg\Bot\Model\Entity;

use JMS\Serializer\Annotation\Accessor;
use JMS\Serializer\Annotation\SkipWhenEmpty;
use JMS\Serializer\Annotation\Type;
use RetailCrm\Mg\Bot\Model\ModelInterface;
use Symfony\Component\Validator\Constraints as Assert;

/**
* Tag class
*
* @package RetailCrm\Mg\Bot\Model\Entity
*/
class Tag implements ModelInterface
{
/**
* @var string $name
*
* @Type("string")
* @Accessor(getter="getName",setter="setName")
*
* @Assert\NotBlank
*/
private $name;

/**
* @var string $colorCode
*
* @Type("string")
* @Accessor(getter="getColorCode",setter="setColorCode")
* @SkipWhenEmpty()
*/
private $colorCode;

/**
* @return string
*/
public function getName(): string
{
return $this->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;
}
}
78 changes: 78 additions & 0 deletions src/Bot/Model/Request/DialogTagRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/**
* PHP version 7.1
*
* Dialog add or delete tag request
*
* @package RetailCrm\Mg\Bot\Model\Request
*/

namespace RetailCrm\Mg\Bot\Model\Request;

use JMS\Serializer\Annotation\Accessor;
use JMS\Serializer\Annotation\SkipWhenEmpty;
use JMS\Serializer\Annotation\Type;
use RetailCrm\Mg\Bot\Model\Entity\Tag;
use RetailCrm\Mg\Bot\Model\ModelInterface;

/**
* DialogTagRequest class
*
* @package RetailCrm\Mg\Bot\Model\Request
*/
class DialogTagRequest implements ModelInterface
{
/**
* @var int $dialogId
*
* @Type("int")
* @Accessor(getter="getDialogId", setter="setDialogId")
* @SkipWhenEmpty()
*/
private $dialogId;

/**
* @var Tag[] $tags
*
* @Type("array")
* @Accessor(getter="getTags", setter="setTags")
* @SkipWhenEmpty()
*/
private $tags;

/**
* @return int
*/
public function getDialogId(): int
{
return $this->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;
}

}
60 changes: 60 additions & 0 deletions tests/Bot/Tests/DialogsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
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;

Expand Down Expand Up @@ -145,4 +147,62 @@ public function testDialogClose()
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());
}
Chupocabra marked this conversation as resolved.
Show resolved Hide resolved
}
Loading