Skip to content

Commit

Permalink
Go client notes and call logs API support[sc-55271]
Browse files Browse the repository at this point in the history
  • Loading branch information
SoeunSona committed Dec 13, 2023
1 parent aedc573 commit c18d0b1
Show file tree
Hide file tree
Showing 8 changed files with 858 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ api.MergeCustomers(&cm.MergeCustomersParams{})
api.ConnectSubscriptions("customerUUID", []cm.Subscription{})
api.ListCustomersContact(&cm.ListContactsParams{}, "customerUUID")
api.CreateCustomersContact(&cm.NewContact{}, "customerUUID")
api.ListCustomerNotes(&cm.ListCustomerNotesParams{}, "customerUUID")
api.CreateCustomerNote(&cm.NewCustomerNote{}, "customerUUID")
```

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

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


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

```go
Expand Down
9 changes: 9 additions & 0 deletions chartmogul.go
Original file line number Diff line number Diff line change
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) (*CustomerNotes, error)
CreateCustomerNote(newCustomerNote *NewNote, customerUUID string) (*CustomerNote, 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(newCustomerNote *NewNote) (*CustomerNote, error)
RetrieveNote(customerNoteUUID string) (*CustomerNote, error)
UpdateNote(CustomerNote *UpdateNote, customerNoteUUID string) (*CustomerNote, error)
DeleteNote(customerNoteUUID string) error
ListNotes(ListNotesParams *ListNotesParams) (*CustomerNotes, error)

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

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

// CustomerNote is the customer note as represented in the API.
type CustomerNote struct {
UUID string `json:"uuid,omitempty"`
// Basic info
CustomerUUID string `json:"customer_uuid,omitempty"`
Type string `json:"type,omitempty"`
Text string `json:"text,omitempty"`
Author string `json:"author,omitempty"`
CallDuration uint32 `json:"call_duration,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
}

// UpdateNote allows updating note on the update endpoint.
type UpdateNote struct {
Text string `json:"text,omitempty"`
AuthorEmail string `json:"author_email,omitempty"`
CallDuration uint32 `json:"call_duration,omitempty"`
CreatedAt string `json:"create_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
}

// NewNote allows creating note on a new endpoint.
type NewNote struct {
// Obligatory
CustomerUUID string `json:"customer_uuid,omitempty"`
Type string `json:"type,omitempty"`

//Optional
AuthorEmail string `json:"author_email,omitempty"`
Text string `json:"text,omitempty"`
CallDuration uint32 `json:"call_duration,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
}

// ListCustomerNoteParams = parameters for listing customer notes in API.
type ListNotesParams struct {
CustomerUUID string `json:"customer_uuid,omitempty"`
PaginationWithCursor
}

// CustomerNotes is result of listing customer notes in API.
type CustomerNotes struct {
Entries []*CustomerNote `json:"entries,omitempty"`
Cursor string `json:"cursor,omitempty"`
HasMore bool `json:"has_more,omitempty"`
}

const (
singleCustomerNoteEndpoint = "customer_notes/:uuid"
customerNotesEndpoint = "customer_notes"
)

// CreateCustomerNote loads the customer note to Chartmogul
//
// See https://dev.chartmogul.com/reference/create-a-customer-note
func (api API) CreateNote(input *NewNote) (*CustomerNote, error) {
result := &CustomerNote{}
return result, api.create(customerNotesEndpoint, input, result)
}

// RetrieveCustomerNote returns one customer note as in API.
//
// See https://dev.chartmogul.com/reference/retrieve-a-customer-note
func (api API) RetrieveNote(customerNoteUUID string) (*CustomerNote, error) {
result := &CustomerNote{}
return result, api.retrieve(singleCustomerNoteEndpoint, customerNoteUUID, result)
}

// UpdateNote updates one customer note in API.
//
// See https://dev.chartmogul.com/reference/retrieve-a-customer-note
func (api API) UpdateNote(input *UpdateNote, customerNoteUUID string) (*CustomerNote, error) {
output := &CustomerNote{}
return output, api.update(singleCustomerNoteEndpoint, customerNoteUUID, input, output)
}

// ListNotes lists all CustomerNotes
//
// See https://dev.chartmogul.com/reference/list-all-customer-notes
func (api API) ListNotes(listNotesParams *ListNotesParams) (*CustomerNotes, error) {
result := &CustomerNotes{}
query := make([]interface{}, 0, 1)
if listNotesParams != nil {
query = append(query, *listNotesParams)
}
return result, api.list(customerNotesEndpoint, result, query...)
}

// DeleteNote deletes one customer note by UUID.
//
// See https://dev.chartmogul.com/reference/delete-a-customer-note
func (api API) DeleteNote(customerNoteUUID string) error {
return api.delete(singleCustomerNoteEndpoint, customerNoteUUID)
}
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{PaginationWithCursor: PaginationWithCursor{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")
}
}
23 changes: 23 additions & 0 deletions customers.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ const (
searchCustomersEndpoint = "customers/search"
mergeCustomersEndpoint = "customers/merges"
customerContactsEndpoint = "customers/:uuid/contacts"
customerNotesEndpoin = "customer_notes"
)

// CreateCustomer loads the customer to Chartmogul. New endpoint - with attributes.
Expand Down Expand Up @@ -260,3 +261,25 @@ 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) (*CustomerNotes, error) {
result := &CustomerNotes{}
query := make([]interface{}, 0, 1)
if listCustomerNotesParams != nil {
query = append(query, *listCustomerNotesParams)
}
path := customerNotesEndpoint + "?customer_uuid=" + customerUUID
return result, api.list(path, result, query...)
}

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

0 comments on commit c18d0b1

Please sign in to comment.