Skip to content

Commit

Permalink
feat: Add Customer Note and Call Log for ChartMogul PHP (#116)
Browse files Browse the repository at this point in the history
* feat: Add Customer Note and Call Log for ChartMogul PHP [sc-55269]

https://app.shortcut.com/chartmogul/story/55269/php-client-notes-and-call-logs-api-support

---------

Co-authored-by: SoeunSona <78331453+SoeunSona@users.noreply.github.com>
  • Loading branch information
wh1tew0lf and SoeunSona committed Dec 13, 2023
1 parent e44fc93 commit dc1a83d
Show file tree
Hide file tree
Showing 8 changed files with 405 additions and 35 deletions.
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,60 @@ $new_customer = $customer->createContact([
]);
```

**List Customer Notes from a customer**
```php
$customer = ChartMogul\Customer::retrieve($uuid);
$customer_notes = $customer->notes([
'cursor' => 'aabbccdd...'
]);
```

**Create a Customer Note from a customer**
```php
$customer = ChartMogul\Customer::retrieve($uuid);
$new_customer_note = $customer->createNote([
'type' => 'note',
'text' => 'This is a note'
]);
```

### Customer Notes

**List Customer Notes**
```php
$customer_notes = ChartMogul\CustomerNote::all([
'customer_uuid' => $uuid,
'cursor' => 'aabbccdd...'
])
```

**Create a Customer Note**
```php
$customer_note = ChartMogul\CustomerNote::create([
'customer_uuid': $uuid,
'type' => 'note',
'text' => 'This is a note'
])
```

**Get a Customer Note**
```php
$customer_note = ChartMogul\CustomerNote::retrieve($note_uuid)
```

**Update a Customer Note**
```php
$updated_customer_note = ChartMogul\CustomerNote::update($note_uuid, [
'text' => 'This is a new note'
]);
```

**Delete a Customer Note**
```php
$customer_note = ChartMogul\CustomerNote::retrieve($note_uuid)
$customer_note->destroy();
```

### Contacts

**List Contacts**
Expand Down
36 changes: 2 additions & 34 deletions src/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace ChartMogul;

use ChartMogul\Resource\AbstractResource;
use ChartMogul\Resource\CollectionWithCursor;
use ChartMogul\Http\ClientInterface;
use ChartMogul\Service\AllTrait;
use ChartMogul\Service\UpdateTrait;
use ChartMogul\Service\CreateTrait;
use ChartMogul\Service\DestroyTrait;
use ChartMogul\Service\GetTrait;
use ChartMogul\Service\FromArrayTrait;

/**
* @property-read string $uuid
Expand All @@ -34,6 +34,7 @@ class Contact extends AbstractResource
use GetTrait;
use DestroyTrait;
use UpdateTrait;
use FromArrayTrait;

/**
* @ignore
Expand Down Expand Up @@ -77,37 +78,4 @@ public static function merge($into, $from, ClientInterface $client = null)

return new Contact($result, $client);
}

/**
* Overrides fromArray so that it will return a collection with cursor instead.
*
* @param array $data
* @param ClientInterface|null $client
* @return CollectionWithCursor|static
*/
public static function fromArray(array $data, ClientInterface $client = null)
{
if (isset($data[static::ROOT_KEY])) {
$array = new CollectionWithCursor(
array_map(
function ($data) use ($client) {
return static::fromArray($data, $client);
},
$data[static::ROOT_KEY]
)
);

if (isset($data["cursor"])) {
$array->cursor = $data["cursor"];
}

if (isset($data["has_more"])) {
$array->has_more = $data["has_more"];
}

return $array;
}

return new static($data, $client);
}
}
28 changes: 28 additions & 0 deletions src/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,32 @@ public function createContact(array $data = [])

return new Contact($result, $client);
}

/**
* Find all customer notes in a customer
*
* @param array $options
* @return CollectionWithCursor
*/
public function notes(array $options = [])
{
$client = $this->getClient();
$result = $client->send("/v1/customer_notes", "GET", [$options, "customer_uuid" => $this->uuid]);

return CustomerNote::fromArray($result, $client);
}

/**
* Creates a customer note from the customer.
*
* @param array $data
* @return CustomerNote
*/
public function createNote(array $data = [])
{
$client = $this->getClient();
$result = $client->send("/v1/customer_notes", "POST", [$data, "customer_uuid" => $this->uuid]);

return new CustomerNote($result, $client);
}
}
51 changes: 51 additions & 0 deletions src/CustomerNote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace ChartMogul;

use ChartMogul\Resource\AbstractResource;
use ChartMogul\Service\AllTrait;
use ChartMogul\Service\CreateTrait;
use ChartMogul\Service\UpdateTrait;
use ChartMogul\Service\DestroyTrait;
use ChartMogul\Service\GetTrait;
use ChartMogul\Service\FromArrayTrait;

/**
* @property-read string $uuid
* @property-read string $customer_uuid
* @property-read string $type
* @property-read string $text
* @property-read integer $call_duration
* @property-read string $author
* @property-read string $created_at
* @property-read string $updated_at
*/
class CustomerNote extends AbstractResource
{
use AllTrait;
use CreateTrait;
use GetTrait;
use DestroyTrait;
use UpdateTrait;
use FromArrayTrait;

/**
* @ignore
*/
public const RESOURCE_NAME = 'CustomerNote';
/**
* @ignore
*/
public const RESOURCE_PATH = '/v1/customer_notes';
public const RESOURCE_ID = 'note_uuid';
public const ROOT_KEY = 'entries';

protected $uuid;
protected $customer_uuid;
protected $type;
protected $text;
protected $call_duration;
protected $author;
protected $created_at;
protected $updated_at;
}
2 changes: 1 addition & 1 deletion src/Http/Retry.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private function retryHTTPStatus($status)

protected function shouldRetry($attempt, $maxAttempts, ResponseInterface $response = null, \Exception $ex = null)
{
if ($attempt >= $maxAttempts && ! is_null($ex)) {
if ($attempt >= $maxAttempts && !is_null($ex)) {
throw $ex;
}

Expand Down
45 changes: 45 additions & 0 deletions src/Service/FromArrayTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace ChartMogul\Service;

use ChartMogul\Resource\CollectionWithCursor;
use ChartMogul\Http\ClientInterface;

/**
* @codeCoverageIgnore
*/
trait FromArrayTrait
{
/**
* Overrides fromArray so that it will return a collection with cursor instead.
*
* @param array $data
* @param ClientInterface|null $client
* @return CollectionWithCursor|static
*/
public static function fromArray(array $data, ClientInterface $client = null)
{
if (isset($data[static::ROOT_KEY])) {
$array = new CollectionWithCursor(
array_map(
function ($data) use ($client) {
return static::fromArray($data, $client);
},
$data[static::ROOT_KEY]
)
);

if (isset($data['cursor'])) {
$array->cursor = $data['cursor'];
}

if (isset($data['has_more'])) {
$array->has_more = $data['has_more'];
}

return $array;
}

return new static($data, $client);
}
}

0 comments on commit dc1a83d

Please sign in to comment.