Skip to content

Commit

Permalink
Creating Customer with Conversation or Thread (#120)
Browse files Browse the repository at this point in the history
* Customer id and email now included in extraction

* No longer including empty array values for extracted Customers

* Now using generic Customer extraction for easier Customer creation

* Updating method name

* Added example for how to create a Customer

* Removing null values
  • Loading branch information
bkuhl authored Apr 3, 2019
1 parent 1069006 commit ee73ec2
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 49 deletions.
2 changes: 1 addition & 1 deletion examples/conversations.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
->withSortField('createdAt')
->withSortOrder('asc')
->withQuery('query')
->withCustomField(123, 'blue');
->withCustomFieldById(123, 'blue');

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

Expand Down
12 changes: 12 additions & 0 deletions examples/customers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@

use HelpScout\Api\ApiClientFactory;
use HelpScout\Api\Customers\CustomerFilters;
use HelpScout\Api\Customers\Customer;
use HelpScout\Api\Customers\Entry\Email;

$client = ApiClientFactory::createClient();
$client = $client->useClientCredentials($appId, $appSecret);

// Create Customer
$customer = new Customer();
$customer->setFirstName('John');
$customer->setLastName('Smith');
$customer->addEmail(new Email([
'email' => "[email protected]",
'type' => 'work',
]));
$client->customers()->create($customer);

// GET customers
$customer = $client->customers()->get(161694345);

Expand Down
2 changes: 1 addition & 1 deletion src/Conversations/Conversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public function extract(): array
}

if ($this->hasCustomer()) {
$data['customer'] = $this->getCustomerDataForEntity();
$data['customer'] = $this->getCustomer()->extract();
}

$assignee = $this->getAssignee();
Expand Down
2 changes: 1 addition & 1 deletion src/Conversations/Threads/ChatThread.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function extract(): array
$data['type'] = self::TYPE;

if ($this->hasCustomer()) {
$data['customer'] = $this->getCustomerDataForEntity();
$data['customer'] = $this->getCustomer()->extract();
}

return $data;
Expand Down
2 changes: 1 addition & 1 deletion src/Conversations/Threads/CustomerThread.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function extract(): array
$data['type'] = self::TYPE;

if ($this->hasCustomer()) {
$data['customer'] = $this->getCustomerDataForEntity();
$data['customer'] = $this->getCustomer()->extract();
}

return $data;
Expand Down
2 changes: 1 addition & 1 deletion src/Conversations/Threads/PhoneThread.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function extract(): array
$data['type'] = self::TYPE;

if ($this->hasCustomer()) {
$data['customer'] = $this->getCustomerDataForEntity();
$data['customer'] = $this->getCustomer()->extract();
}

return $data;
Expand Down
2 changes: 1 addition & 1 deletion src/Conversations/Threads/ReplyThread.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function extract(): array
$data['draft'] = $this->isDraft();

if ($this->hasCustomer()) {
$data['customer'] = $this->getCustomerDataForEntity();
$data['customer'] = $this->getCustomer()->extract();
}

// When creating threads "user" is expected to be numeric rather
Expand Down
11 changes: 0 additions & 11 deletions src/Conversations/Threads/Support/HasCustomer.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,4 @@ protected function hasCustomer(): bool
{
return $this->getCustomer() instanceof Customer;
}

protected function getCustomerDataForEntity(): array
{
$customer = $this->getCustomer();
$customerData = [
'id' => $customer->getId(),
'email' => $customer->getFirstEmail(),
];

return array_filter($customerData);
}
}
7 changes: 5 additions & 2 deletions src/Customers/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,9 @@ public function hydrate(array $data, array $embedded = [])
*/
public function extract(): array
{
return [
// ensure no empty values are included in the extraction for cleaner debugging
return array_filter([
'id' => $this->getId(),
'firstName' => $this->getFirstName(),
'lastName' => $this->getLastName(),
'gender' => $this->getGender(),
Expand All @@ -210,7 +212,8 @@ public function extract(): array
'photoUrl' => $this->getPhotoUrl(),
'background' => $this->getBackground(),
'age' => $this->getAge(),
];
'email' => $this->getFirstEmail(),
]);
}

/**
Expand Down
17 changes: 1 addition & 16 deletions tests/Customers/CustomerClientIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,6 @@ public function testCreateCustomer()
[
'firstName' => 'Big',
'lastName' => 'Bird',
'gender' => null,
'jobTitle' => null,
'location' => null,
'organization' => null,
'photoType' => null,
'photoUrl' => null,
'background' => null,
'age' => null,
]
);
}
Expand All @@ -59,16 +51,9 @@ public function testUpdateCustomer()
'https://api.helpscout.net/v2/customers/12',
'PUT',
[
'id' => 12,
'firstName' => 'Big',
'lastName' => 'Bird',
'gender' => null,
'jobTitle' => null,
'location' => null,
'organization' => null,
'photoType' => null,
'photoUrl' => null,
'background' => null,
'age' => null,
]
);
}
Expand Down
37 changes: 23 additions & 14 deletions tests/Customers/CustomerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,42 +247,51 @@ public function testExtract()
$customer->setLocation('US');
$customer->setOrganization('Sesame Street');
$customer->setPhotoType('unknown');
$customer->setPhotoUrl('');
$customer->setPhotoUrl('https://photos.me');
$customer->setBackground('Big yellow bird');
$customer->setAge('52');

$this->assertSame([
'id' => 12,
'firstName' => 'Big',
'lastName' => 'Bird',
'gender' => 'unknown',
'jobTitle' => 'Entertainer',
'location' => 'US',
'organization' => 'Sesame Street',
'photoType' => 'unknown',
'photoUrl' => '',
'photoUrl' => 'https://photos.me',
'background' => 'Big yellow bird',
'age' => '52',
], $customer->extract());
}

public function testExtractNewEntity()
/**
* Commonly in v2 of the API we see scenarios where if a "Customer" has an email or id it'll use the existing
* Customer associated with those, otherwise it'll create a new Customer. When extracting a Customer it'll
* most likely be used for Creating something, so including email as a primary attribute this cleaner.
*/
public function testExtractsEmailAsAttribute()
{
$customer = new Customer();

$this->assertSame([
'firstName' => null,
'lastName' => null,
'gender' => null,
'jobTitle' => null,
'location' => null,
'organization' => null,
'photoType' => null,
'photoUrl' => null,
'background' => null,
'age' => null,
$email = new Email();
$email->setValue('[email protected]');

$customer->addEmail($email);

$this->assertArraySubset([
'email' => '[email protected]',
], $customer->extract());
}

public function testExtractNewEntity()
{
$customer = new Customer();

$this->assertSame([], $customer->extract());
}

public function testAddChat()
{
$customer = new Customer();
Expand Down

0 comments on commit ee73ec2

Please sign in to comment.