From ee73ec2f9befdc8b2756e29a3bc3d5182fa3508c Mon Sep 17 00:00:00 2001 From: Ben Kuhl Date: Wed, 3 Apr 2019 07:15:49 -0400 Subject: [PATCH] Creating Customer with Conversation or Thread (#120) * 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 --- examples/conversations.php | 2 +- examples/customers.php | 12 ++++++ src/Conversations/Conversation.php | 2 +- src/Conversations/Threads/ChatThread.php | 2 +- src/Conversations/Threads/CustomerThread.php | 2 +- src/Conversations/Threads/PhoneThread.php | 2 +- src/Conversations/Threads/ReplyThread.php | 2 +- .../Threads/Support/HasCustomer.php | 11 ------ src/Customers/Customer.php | 7 +++- .../CustomerClientIntegrationTest.php | 17 +-------- tests/Customers/CustomerTest.php | 37 ++++++++++++------- 11 files changed, 47 insertions(+), 49 deletions(-) diff --git a/examples/conversations.php b/examples/conversations.php index 162ed99f..2a11ae4e 100644 --- a/examples/conversations.php +++ b/examples/conversations.php @@ -32,7 +32,7 @@ ->withSortField('createdAt') ->withSortOrder('asc') ->withQuery('query') - ->withCustomField(123, 'blue'); + ->withCustomFieldById(123, 'blue'); $conversations = $client->conversations()->list($filters); diff --git a/examples/customers.php b/examples/customers.php index 1e588105..01fe7097 100644 --- a/examples/customers.php +++ b/examples/customers.php @@ -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' => "my-customer@their-business.com", + 'type' => 'work', +])); +$client->customers()->create($customer); + // GET customers $customer = $client->customers()->get(161694345); diff --git a/src/Conversations/Conversation.php b/src/Conversations/Conversation.php index cfd2a171..f761e716 100644 --- a/src/Conversations/Conversation.php +++ b/src/Conversations/Conversation.php @@ -285,7 +285,7 @@ public function extract(): array } if ($this->hasCustomer()) { - $data['customer'] = $this->getCustomerDataForEntity(); + $data['customer'] = $this->getCustomer()->extract(); } $assignee = $this->getAssignee(); diff --git a/src/Conversations/Threads/ChatThread.php b/src/Conversations/Threads/ChatThread.php index 15b4c277..105656b5 100644 --- a/src/Conversations/Threads/ChatThread.php +++ b/src/Conversations/Threads/ChatThread.php @@ -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; diff --git a/src/Conversations/Threads/CustomerThread.php b/src/Conversations/Threads/CustomerThread.php index 1e231a28..c20f664a 100644 --- a/src/Conversations/Threads/CustomerThread.php +++ b/src/Conversations/Threads/CustomerThread.php @@ -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; diff --git a/src/Conversations/Threads/PhoneThread.php b/src/Conversations/Threads/PhoneThread.php index b447c9d9..a404f3c4 100644 --- a/src/Conversations/Threads/PhoneThread.php +++ b/src/Conversations/Threads/PhoneThread.php @@ -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; diff --git a/src/Conversations/Threads/ReplyThread.php b/src/Conversations/Threads/ReplyThread.php index 9f3ccaf6..0edc5403 100644 --- a/src/Conversations/Threads/ReplyThread.php +++ b/src/Conversations/Threads/ReplyThread.php @@ -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 diff --git a/src/Conversations/Threads/Support/HasCustomer.php b/src/Conversations/Threads/Support/HasCustomer.php index 3e970210..cb19de36 100644 --- a/src/Conversations/Threads/Support/HasCustomer.php +++ b/src/Conversations/Threads/Support/HasCustomer.php @@ -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); - } } diff --git a/src/Customers/Customer.php b/src/Customers/Customer.php index 5ddb6303..55d3a6fd 100644 --- a/src/Customers/Customer.php +++ b/src/Customers/Customer.php @@ -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(), @@ -210,7 +212,8 @@ public function extract(): array 'photoUrl' => $this->getPhotoUrl(), 'background' => $this->getBackground(), 'age' => $this->getAge(), - ]; + 'email' => $this->getFirstEmail(), + ]); } /** diff --git a/tests/Customers/CustomerClientIntegrationTest.php b/tests/Customers/CustomerClientIntegrationTest.php index 5d162dae..2dcf72a3 100644 --- a/tests/Customers/CustomerClientIntegrationTest.php +++ b/tests/Customers/CustomerClientIntegrationTest.php @@ -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, ] ); } @@ -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, ] ); } diff --git a/tests/Customers/CustomerTest.php b/tests/Customers/CustomerTest.php index 6edca052..e2d128cf 100644 --- a/tests/Customers/CustomerTest.php +++ b/tests/Customers/CustomerTest.php @@ -247,11 +247,12 @@ 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', @@ -259,30 +260,38 @@ public function testExtract() '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('tester@mysite.com'); + + $customer->addEmail($email); + + $this->assertArraySubset([ + 'email' => 'tester@mysite.com', ], $customer->extract()); } + public function testExtractNewEntity() + { + $customer = new Customer(); + + $this->assertSame([], $customer->extract()); + } + public function testAddChat() { $customer = new Customer();