Skip to content

Commit

Permalink
Support for Contacts (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
briwa authored Apr 12, 2023
1 parent b79eb50 commit 3b70840
Show file tree
Hide file tree
Showing 8 changed files with 1,099 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ api.ListCustomers(&cm.ListCustomersParams{})
api.UpdateCustomer(&cm.NewCustomer{}, "customerUUID")
api.MergeCustomers(&cm.MergeCustomersParams{})
api.ConnectSubscriptions("customerUUID", []cm.Subscription{})
api.ListCustomersContact(&cm.ListContactsParams{}, "customerUUID")
api.CreateCustomersContact(&cm.NewContact{}, "customerUUID")
```

#### [Contacts](https://dev.chartmogul.com/reference/contacts)

```go
api.CreateContact(&cm.NewContact{})
api.RetrieveContact("customerUUID")
api.ListContacts(&cm.ListContactsParams{})
api.UpdateContact(&cm.UpdateContact{}, "contact")
api.DeleteContact("customerUUID")
api.MergeContacts("intoContactUUID", "fromContactUUID")
```

#### [Plans](https://dev.chartmogul.com/reference#plans)
Expand Down
17 changes: 17 additions & 0 deletions chartmogul.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ type IApi interface {
DeleteCustomer(customerUUID string) error
DeleteCustomerInvoices(dataSourceUUID, customerUUID string) error
DeleteCustomerInvoicesV2(dataSourceUUID, customerUUID string, DeleteCustomerInvoicesParams *DeleteCustomerInvoicesParams) error
ListCustomersContacts(ListContactsParams *ListContactsParams, customerUUID string) (*Contacts, error)
CreateCustomersContact(newContact *NewContact, customerUUID string) (*Contact, error)

// Contacts
CreateContact(newContact *NewContact) (*Contact, error)
RetrieveContact(contactUUID string) (*Contact, error)
UpdateContact(Contact *UpdateContact, contactUUID string) (*Contact, error)
ListContacts(ListContactsParams *ListContactsParams) (*Contacts, error)
DeleteContact(contactUUID string) error
MergeContacts(intoContactUUID string, fromContactUUID string) (*Contact, error)

// - Cusomer Attributes
RetrieveCustomersAttributes(customerUUID string) (*Attributes, error)
Expand Down Expand Up @@ -157,6 +167,13 @@ type AnchorCursor struct {
StartAfter string `json:"start-after,omitempty"`
}

// PaginationWithCursor is the new standard for cursor with pagination.
type PaginationWithCursor struct {
PerPage uint32 `json:"per_page,omitempty"`
// Cursor is a reference to get the next set of entries.
Cursor string `json:"cursor,omitempty"`
}

type MetaCursor struct {
NextKey uint64 `json:"next_key,omitempty"`
PrevKey uint64 `json:"prev_key,omitempty"`
Expand Down
133 changes: 133 additions & 0 deletions contacts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package chartmogul

import "strings"

// Contact is the contact as represented in the API.
type Contact struct {
UUID string `json:"uuid,omitempty"`
// Basic info
CustomerExternalID string `json:"customer_external_id,omitempty"`
CustomerUUID string `json:"customer_uuid,omitempty"`
DataSourceUUID string `json:"data_source_uuid,omitempty"`
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name,omitempty"`
LinkedIn string `json:"linked_in,omitempty"`
Notes string `json:"notes,omitempty"`
Phone string `json:"phone,omitempty"`
Position uint32 `json:"position,omitempty"`
Title string `json:"title,omitempty"`
Twitter string `json:"twitter,omitempty"`
Custom map[string]interface{} `json:"custom,omitempty"`
}

// UpdateContact allows updating contact on the update endpoint.
type UpdateContact struct {
CustomerExternalID string `json:"customer_external_id,omitempty"`
DataSourceUUID string `json:"data_source_uuid,omitempty"`
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name,omitempty"`
LinkedIn string `json:"linked_in,omitempty"`
Notes string `json:"notes,omitempty"`
Phone string `json:"phone,omitempty"`
Position uint32 `json:"position,omitempty"`
Title string `json:"title,omitempty"`
Twitter string `json:"twitter,omitempty"`
Custom []Custom `json:"custom,omitempty"`
}

// NewContact allows creating contact on a new endpoint.
type NewContact struct {
// Obligatory
CustomerUUID string `json:"customer_uuid,omitempty"`
DataSourceUUID string `json:"data_source_uuid,omitempty"`

//Optional
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name,omitempty"`
LinkedIn string `json:"linked_in,omitempty"`
Notes string `json:"notes,omitempty"`
Phone string `json:"phone,omitempty"`
Position uint32 `json:"position,omitempty"`
Title string `json:"title,omitempty"`
Twitter string `json:"twitter,omitempty"`
Custom []Custom `json:"custom,omitempty"`
}

type Custom struct {
Key string `json:"key,omitempty"`
Value string `json:"value,omitempty"`
}

// ListContactsParams = parameters for listing contacts in API.
type ListContactsParams struct {
CustomerUUID string `json:"customer_uuid,omitempty"`
DataSourceUUID string `json:"data_source_uuid,omitempty"`
PaginationWithCursor
}

// Contacts is result of listing contacts in API.
type Contacts struct {
Entries []*Contact `json:"entries,omitempty"`
Cursor string `json:"cursor,omitempty"`
PerPage uint32 `json:"per_page"`
HasMore bool `json:"has_more,omitempty"`
}

const (
singleContactEndpoint = "contacts/:uuid"
contactsEndpoint = "contacts"
mergeContactsEndpoint = "contacts/:into_contact_uuid/merge/:from_contact_uuid"
)

// CreateContact loads the contact to Chartmogul
//
// See https://dev.chartmogul.com/reference/create-a-contact-contacts
func (api API) CreateContact(newContact *NewContact) (*Contact, error) {
result := &Contact{}
return result, api.create(contactsEndpoint, newContact, result)
}

// RetrieveContact returns one contact as in API.
//
// See https://dev.chartmogul.com/reference/retrieve-a-contact
func (api API) RetrieveContact(contactUUID string) (*Contact, error) {
result := &Contact{}
return result, api.retrieve(singleContactEndpoint, contactUUID, result)
}

// UpdateContact updates one contact in API.
//
// See https://dev.chartmogul.com/reference/retrieve-a-contact
func (api API) UpdateContact(input *UpdateContact, contactUUID string) (*Contact, error) {
output := &Contact{}
return output, api.update(singleContactEndpoint, contactUUID, input, output)
}

// ListContacts lists all Contacts
//
// See https://dev.chartmogul.com/reference/list-all-contacts
func (api API) ListContacts(listContactsParams *ListContactsParams) (*Contacts, error) {
result := &Contacts{}
query := make([]interface{}, 0, 1)
if listContactsParams != nil {
query = append(query, *listContactsParams)
}
return result, api.list(contactsEndpoint, result, query...)
}

// MergeContact merges two contacts.
//
// See https://dev.chartmogul.com/reference/merge-contacts
func (api API) MergeContacts(intoContactUUID string, fromContactUUID string) (*Contact, error) {
result := &Contact{}
temp_path := strings.Replace(mergeContactsEndpoint, ":into_contact_uuid", intoContactUUID, 1)
path := strings.Replace(temp_path, ":from_contact_uuid", fromContactUUID, 1)
return result, api.create(path, nil, result)
}

// DeleteContact deletes one contact by UUID.
//
// See https://dev.chartmogul.com/reference/delete-a-contact
func (api API) DeleteContact(contactUUID string) error {
return api.delete(singleContactEndpoint, contactUUID)
}
Loading

0 comments on commit 3b70840

Please sign in to comment.