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

feat(conversations): Added "missing" functionality to get truncated threads #317

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Conversations/ConversationFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ class ConversationFilters
*/
private $query;

/**
* @var string
*/
public $embed;

public function getParams(): array
{
$params = [
Expand All @@ -78,6 +83,7 @@ public function getParams(): array
'sortField' => $this->sortField,
'sortOrder' => $this->sortOrder,
'query' => $this->query,
'embed' => $this->embed,
];

if (\is_array($this->tag)) {
Expand Down Expand Up @@ -240,4 +246,15 @@ public function withQuery(string $query): ConversationFilters
return $filters;
}

public function withEmbed(string $embed): ConversationFilters
{
Assert::oneOf($embed, [
'threads',
]);

$filters = clone $this;
$filters->embed = $embed;

return $filters;
}
}
4 changes: 3 additions & 1 deletion tests/Conversations/ConversationFiltersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public function testGetParams()
->sortField('createdAt')
->sortOrder('asc')
->withQuery('query')
->byCustomField(123, 'blue');
->byCustomField(123, 'blue')
->withEmbed('threads');

$this->assertSame([
'mailbox' => 1,
Expand All @@ -44,6 +45,7 @@ public function testGetParams()
'sortField' => 'createdAt',
'sortOrder' => 'asc',
'query' => 'query',
'embed' => 'threads',
'tag' => 'testing',
'customFieldsByIds' => '123:blue',
], $filters->getParams());
Expand Down
16 changes: 16 additions & 0 deletions tests/Conversations/ConversationIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,22 @@ public function testGetConversationPreloadsThreads()
]);
}

public function testGetConversationWithEmbedThreads()
{
$this->stubResponse($this->getResponse(200, ConversationPayloads::getConversations(1, 10, true)));

$filters = (new ConversationFilters())
->withEmbed('threads');

$conversations = $this->client->conversations()->list($filters);

$this->assertCount(10, $conversations);
$this->assertInstanceOf(Conversation::class, $firstConversation = $conversations[0]);
$this->assertInstanceOf(CustomerThread::class, $firstConversation->getThreads()->toArray()[0]);

$this->verifySingleRequest('https://api.helpscout.net/v2/conversations?embed=threads');
}

public function testListConversations()
{
$this->stubResponse(
Expand Down
65 changes: 60 additions & 5 deletions tests/Payloads/ConversationPayloads.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ public static function getConversation(int $id): string
return json_encode(static::conversation($id));
}

public static function getConversations(int $pageNumber, int $totalElements): string
public static function getConversations(int $pageNumber, int $totalElements, bool $embedThread = false): string
{
$pageSize = 10;
$pageElements = min($totalElements, $pageSize);
$totalPages = ceil($totalElements / $pageSize);

// Create embedded resources
$conversations = array_map(function ($id) {
return static::conversation($id);
$conversations = array_map(function ($id) use ($embedThread) {
return static::conversation($id, $embedThread);
}, range(1, $pageElements));

$data = [
Expand Down Expand Up @@ -60,9 +60,9 @@ public static function getConversations(int $pageNumber, int $totalElements): st
return json_encode($data);
}

private static function conversation(int $id): array
private static function conversation(int $id, bool $embedThread = false): array
{
return [
$conversation = [
'id' => $id,
'number' => 15473,
'threads' => 2,
Expand Down Expand Up @@ -144,6 +144,61 @@ private static function conversation(int $id): array
],
],
];

if ($embedThread) {
$conversation['_embedded']['threads'] = [
[
'id' => 1,
'type' => 'customer',
'status' => 'active',
'state' => 'published',
'action' => [
'type' => 'default',
'associatedEntities' => [],
],
'body' => 'This is a test',
'source' => [
'type' => 'email',
'via' => 'user',
],
'customer' => [
'id' => 472611182,
'first' => 'John',
'last' => 'Doe',
'photoUrl' => 'https://d33v4339jhl8k0.cloudfront.net/customer-avatar/05.png',
'email' => '[email protected]',
],
'createdBy' => [
'id' => 1,
'type' => 'user',
'first' => 'John',
'last' => 'Doe',
'email' => '[email protected]',
'to' => [],
'cc' => [],
'bcc' => [],
'createdAt' => '2017-04-21T14:39:56Z',
],
'assignedTo' => [
'id' => 12,
'first' => 'Help',
'last' => 'Scout',
'email' => '[email protected]',
],
'savedReplyId' => 0,
'_embedded' => [
'attachments' => [],
],
'_links' => [
'createdByUser' => [
'href' => 'https://api.helpscout.net/v2/users/1',
]
]
],
];
}

return $conversation;
}

public static function getThreads(int $conversationId): string
Expand Down