Skip to content

Commit

Permalink
Merge pull request #126 from helpscout/conversation-update-entities-a…
Browse files Browse the repository at this point in the history
…ny-collection

Conversation update entities any collection
  • Loading branch information
bkuhl authored Apr 9, 2019
2 parents ebd04be + 48d5618 commit 06e3a3d
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 11 deletions.
41 changes: 31 additions & 10 deletions src/Conversations/ConversationsEndpoint.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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',
],
]
);
}
}

0 comments on commit 06e3a3d

Please sign in to comment.