Skip to content

Commit

Permalink
Go client notes and call logs API support[sc-55271] (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
SoeunSona committed Dec 19, 2023
1 parent 75136f3 commit 8f97991
Show file tree
Hide file tree
Showing 8 changed files with 860 additions and 2 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ go get github.com/chartmogul/chartmogul-go/v4
[Deprecation] - `account_token`/`secret_key` combo is deprecated. Please use API key for both fields.
Version 3.x will introduce a breaking change in authentication configuration. For more details, please visit: https://dev.chartmogul.com

Version 4.x will introduce a breaking change for pagination on List endpoints. The `cursor` object now expects a `per_page` and `cursor` parameter. `page` will no longer be accepted.
Version 4.x will introduce a breaking change for pagination on List endpoints. The `cursor` object now expects a `per_page` and `cursor` parameter. `page` will no longer be accepted.

First create the `API` struct by passing your API key, available from the administration section of your ChartMogul account.

Expand Down Expand Up @@ -111,6 +111,8 @@ api.MergeCustomers(&cm.MergeCustomersParams{})
api.ConnectSubscriptions("customerUUID", []cm.Subscription{})
api.ListCustomersContact(&cm.ListContactsParams{}, "customerUUID")
api.CreateCustomersContact(&cm.NewContact{}, "customerUUID")
api.ListCustomerNotes(&cm.ListNotesParams{}, "customerUUID")
api.CreateCustomerNote(&cm.NewNote{}, "customerUUID")
```

#### [Contacts](https://dev.chartmogul.com/reference/contacts)
Expand All @@ -124,6 +126,15 @@ api.DeleteContact("customerUUID")
api.MergeContacts("intoContactUUID", "fromContactUUID")
```

#### [Customer Notes](https://dev.chartmogul.com/reference/customer-notes)
```go
api.CreateNote(&cm.NewNote{})
api.RetrieveNote("noteUUID")
api.ListNote(&cm.ListNoteParams{})
api.UpdateNote(&cm.UpdateNote{}, "noteUUID")
api.DeleteNote("noteUUID")
```

#### [Plans](https://dev.chartmogul.com/reference#plans)

```go
Expand Down
11 changes: 10 additions & 1 deletion chartmogul.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const (
)

var (
url = "https://api.chartmogul.com/v1/%v"
url = "http://localhost:3000/api/v1/%v"
timeout = 30 * time.Second
)

Expand Down Expand Up @@ -96,6 +96,8 @@ type IApi interface {
DeleteCustomerInvoicesV2(dataSourceUUID, customerUUID string, DeleteCustomerInvoicesParams *DeleteCustomerInvoicesParams) error
ListCustomersContacts(ListContactsParams *ListContactsParams, customerUUID string) (*Contacts, error)
CreateCustomersContact(newContact *NewContact, customerUUID string) (*Contact, error)
ListCustomerNotes(ListNotesParams *ListNotesParams, customerUUID string) (*Notes, error)
CreateCustomerNote(newCustomerNote *NewNote, customerUUID string) (*Note, error)

// Contacts
CreateContact(newContact *NewContact) (*Contact, error)
Expand All @@ -105,6 +107,13 @@ type IApi interface {
DeleteContact(contactUUID string) error
MergeContacts(intoContactUUID string, fromContactUUID string) (*Contact, error)

// Customer Notes
CreateNote(newNote *NewNote) (*Note, error)
RetrieveNote(noteUUID string) (*Note, error)
UpdateNote(Note *UpdateNote, noteUUID string) (*Note, error)
DeleteNote(noteUUID string) error
ListNotes(ListNotesParams *ListNotesParams) (*Notes, error)

// - Cusomer Attributes
RetrieveCustomersAttributes(customerUUID string) (*Attributes, error)

Expand Down
215 changes: 215 additions & 0 deletions customer_notes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
package chartmogul

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/davecgh/go-spew/spew"
)

func TestListNotes(t *testing.T) {
server := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
t.Errorf("Unexpected method %v", r.Method)
}
if r.RequestURI != "/v/customer_notes?customer_uuid=cus_00000000-0000-0000-0000-000000000000&per_page=1" {
t.Errorf("Unexpected URI %v", r.RequestURI)
}
w.WriteHeader(http.StatusOK)
//nolint
w.Write([]byte(`{
"entries": [{
"uuid": "note_00000000-0000-0000-0000-000000000000",
"customer_uuid": "cus_00000000-0000-0000-0000-000000000000",
"type": "note",
"author": "John Doe (john@example.com)",
"text": "This is a note",
"call_duration": 0,
"created_at": "2015-06-09T19:20:30Z",
"updated_at": "2015-06-09T19:20:30Z"
}],
"has_more": false,
"cursor": "88abf99"
}`))
}))
defer server.Close()
SetURL(server.URL + "/v/%v")

tested := &API{
ApiKey: "token",
}
params := &ListNotesParams{Cursor: Cursor{PerPage: 1}, CustomerUUID: "cus_00000000-0000-0000-0000-000000000000"}
customer_notes, err := tested.ListNotes(params)

if err != nil {
spew.Dump(err)
t.Fatal("Not expected to fail")
}
if len(customer_notes.Entries) == 0 {
spew.Dump(customer_notes)
t.Fatal("Unexpected result")
}
}

func TestRetrieveNote(t *testing.T) {
server := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
t.Errorf("Unexpected method %v", r.Method)
}
if r.RequestURI != "/v/customer_notes/note_00000000-0000-0000-0000-000000000000" {
t.Errorf("Unexpected URI %v", r.RequestURI)
}
w.WriteHeader(http.StatusOK)
//nolint
w.Write([]byte(`{
"uuid": "note_00000000-0000-0000-0000-000000000000",
"customer_uuid": "cus_00000000-0000-0000-0000-000000000000",
"type": "note",
"author": "John Doe (john@example.com)",
"text": "This is a note",
"call_duration": 0,
"created_at": "2015-06-09T19:20:30Z",
"updated_at": "2015-06-09T19:20:30Z"
}`))
}))
defer server.Close()
SetURL(server.URL + "/v/%v")

tested := &API{
ApiKey: "token",
}
customer_note, err := tested.RetrieveNote("note_00000000-0000-0000-0000-000000000000")

if err != nil {
spew.Dump(err)
t.Fatal("Not expected to fail")
}
if customer_note == nil {
spew.Dump(customer_note)
t.Fatal("Unexpected result")
}
}

func TestCreateNote(t *testing.T) {
server := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Errorf("Unexpected method %v", r.Method)
}
if r.RequestURI != "/v/customer_notes" {
t.Errorf("Unexpected URI %v", r.RequestURI)
}
w.WriteHeader(http.StatusCreated)
//nolint
w.Write([]byte(`{
"uuid": "note_00000000-0000-0000-0000-000000000000",
"customer_uuid": "cus_00000000-0000-0000-0000-000000000000",
"type": "note",
"text": "This is a note",
"author": "John Doe (john@example.com)",
"call_duration": 0,
"created_at": "2015-06-09T19:20:30Z",
"updated_at": "2015-06-09T19:20:30Z"
}`))
}))
defer server.Close()
SetURL(server.URL + "/v/%v")

tested := &API{
ApiKey: "token",
}

customer_notes, err := tested.CreateNote(&NewNote{
CustomerUUID: "cus_00000000-0000-0000-0000-000000000000",
Type: "note",
Text: "This is a note",
AuthorEmail: "john@example.com",
CallDuration: 0,
})

if err != nil {
spew.Dump(err)
t.Fatal("Not expected to fail")
}
if customer_notes.UUID != "note_00000000-0000-0000-0000-000000000000" {
spew.Dump(customer_notes)
t.Fatal("Unexpected result")
}
}

func TestUpdateNote(t *testing.T) {
server := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
if r.Method != "PATCH" {
t.Errorf("Unexpected method %v", r.Method)
}
if r.RequestURI != "/v/customer_notes/note_00000000-0000-0000-0000-000000000000" {
t.Errorf("Unexpected URI %v", r.RequestURI)
}
w.WriteHeader(http.StatusOK)
//nolint
w.Write([]byte(`{
"uuid": "note_00000000-0000-0000-0000-000000000000",
"customer_uuid": "cus_00000000-0000-0000-0000-000000000000",
"type": "note",
"text": "This is a new note",
"author": "John Doe (john@example.com)",
"call_duration": 0,
"created_at": "2015-06-09T19:20:30Z",
"updated_at": "2015-06-09T19:20:30Z"
}`))
}))
defer server.Close()
SetURL(server.URL + "/v/%v")

tested := &API{
ApiKey: "token",
}

customer_note, err := tested.UpdateNote(&UpdateNote{
Text: "This is a new note",
}, "note_00000000-0000-0000-0000-000000000000")

if err != nil {
spew.Dump(err)
t.Fatal("Not expected to fail")
}
if customer_note.Text != "This is a new note" {
spew.Dump(customer_note)
t.Fatal("Unexpected result")
}
}

func TestDeleteNote(t *testing.T) {
server := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
if r.Method != "DELETE" {
t.Errorf("Unexpected method %v", r.Method)
}
if r.RequestURI != "/v/customer_notes/note_00000000-0000-0000-0000-000000000000" {
t.Errorf("Unexpected URI %v", r.RequestURI)
}
w.WriteHeader(http.StatusNoContent)
}))
defer server.Close()
SetURL(server.URL + "/v/%v")

tested := &API{
ApiKey: "token",
}

err := tested.DeleteNote("note_00000000-0000-0000-0000-000000000000")

if err != nil {
spew.Dump(err)
t.Fatal("Not expected to fail")
}
}
26 changes: 26 additions & 0 deletions customers.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,29 @@ func (api API) CreateCustomersContact(newContact *NewContact, customerUUID strin
path := strings.Replace(customerContactsEndpoint, ":uuid", customerUUID, 1)
return result, api.create(path, newContact, result)
}

// ListCustomerNotes
//
// See https://dev.chartmogul.com/reference/list-customer-notes
func (api API) ListCustomerNotes(listCustomerNotesParams *ListNotesParams, customerUUID string) (*Notes, error) {
result := &Notes{}
query := make([]interface{}, 0, 1)
if listCustomerNotesParams != nil {
if listCustomerNotesParams.CustomerUUID == "" {
listCustomerNotesParams.CustomerUUID = customerUUID
}
query = append(query, *listCustomerNotesParams)
}
return result, api.list(customerNotesEndpoint, result, query...)
}

// CreateCustomerNote
//
// See https://dev.chartmogul.com/reference/create-a-customer-note
func (api API) CreateCustomerNote(input *NewNote, customerUUID string) (*Note, error) {
result := &Note{}
if input.CustomerUUID == "" {
input.CustomerUUID = customerUUID
}
return result, api.create(customerNotesEndpoint, input, result)
}
Loading

0 comments on commit 8f97991

Please sign in to comment.