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

Conversation update entities any collection #126

Merged
merged 5 commits into from Apr 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 31 additions & 10 deletions src/Conversations/ConversationsEndpoint.php
Expand Up @@ -5,6 +5,7 @@
namespace HelpScout\Api\Conversations;

use HelpScout\Api\Endpoint;
use HelpScout\Api\Entity\Collection;
use HelpScout\Api\Entity\PagedCollection;
use HelpScout\Api\Entity\Patch;
use HelpScout\Api\Http\Hal\HalPagedResources;
Expand Down Expand Up @@ -47,13 +48,21 @@ public function create(Conversation $conversation): ?int
/**
* Updates the custom field values for a given conversation. Ommitted fields are removed.
*
* @param int $conversationId
* @param CustomField[] $customFields
* @param int $conversationId
* @param CustomField[]|array|Collection|CustomFieldsCollection $customFields
*/
public function updateCustomFields(int $conversationId, array $customFields): void
public function updateCustomFields(int $conversationId, $customFields): void
{
$customFieldsCollection = new CustomFieldsCollection();
$customFieldsCollection->setCustomFields($customFields);
if ($customFields instanceof CustomFieldsCollection) {
$customFieldsCollection = $customFields;
} else {
if ($customFields instanceof Collection) {
$customFields = $customFields->toArray();
}

$customFieldsCollection = new CustomFieldsCollection();
$customFieldsCollection->setCustomFields($customFields);
}

$this->restClient->updateResource(
$customFieldsCollection,
Expand All @@ -65,13 +74,25 @@ public function updateCustomFields(int $conversationId, array $customFields): vo
* Updates the tags for a given conversation.
* Omitted tags are removed.
*
* @param int $conversationId
* @param array $tags
* @param int $conversationId
* @param array|Collection|TagsCollection $tags
*/
public function updateTags(int $conversationId, array $tags): void
public function updateTags(int $conversationId, $tags): void
{
$tagsCollection = new TagsCollection();
$tagsCollection->setTags($tags);
if ($tags instanceof TagsCollection) {
$tagsCollection = $tags;
} else {
if ($tags instanceof Collection) {
$tagNames = [];
foreach ($tags as $tag) {
$tagNames[] = $tag->getName();
}
$tags = $tagNames;
}

$tagsCollection = new TagsCollection();
$tagsCollection->setTags($tags);
}

$this->restClient->updateResource(
$tagsCollection,
Expand Down
69 changes: 68 additions & 1 deletion tests/Conversations/ConversationIntegrationTest.php
Expand Up @@ -13,6 +13,7 @@
use HelpScout\Api\Customers\Customer;
use HelpScout\Api\Entity\Collection;
use HelpScout\Api\Mailboxes\Mailbox;
use HelpScout\Api\Tags\Tag;
use HelpScout\Api\Tags\TagsCollection;
use HelpScout\Api\Tests\ApiClientIntegrationTestCase;
use HelpScout\Api\Tests\Payloads\ConversationPayloads;
Expand Down Expand Up @@ -430,7 +431,32 @@ public function testUpdatesCustomFields()
);
}

public function testUpdatesTags()
public function testUpdatesCustomFieldsWithEntityCollectionOfCustomFields()
{
$this->stubResponse(
$this->getResponse(204)
);

$customField = new CustomField();
$customField->setId(10524);
$customField->setValue(new \DateTime('today'));

$customFields = new Collection([$customField]);

$this->client->conversations()->updateCustomFields(12, $customFields);

$fields = new CustomFieldsCollection();

$fields->setCustomFields($customFields->toArray());

$this->verifyRequestWithData(
'https://api.helpscout.net/v2/conversations/12/fields',
'PUT',
$fields->extract()
);
}

public function testUpdatesTagsWithArrayOfTagNames()
{
$this->stubResponse(
$this->getResponse(204)
Expand All @@ -446,4 +472,45 @@ public function testUpdatesTags()
$tags->extract()
);
}

public function testUpdatesTagsWithTagCollection()
{
$this->stubResponse(
$this->getResponse(204)
);

$tags = new TagsCollection();
$tags->setTags(['Support']);

$this->client->conversations()->updateTags(14, $tags);

$this->verifyRequestWithData(
'https://api.helpscout.net/v2/conversations/14/tags',
'PUT',
$tags->extract()
);
}

public function testUpdatesTagsWithEntityCollectionOfTags()
{
$this->stubResponse(
$this->getResponse(204)
);

$tag = new Tag();
$tag->setName('Support');
$tags = new Collection([$tag]);

$this->client->conversations()->updateTags(14, $tags);

$this->verifyRequestWithData(
'https://api.helpscout.net/v2/conversations/14/tags',
'PUT',
[
'tags' => [
'Support',
],
]
);
}
}