Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods for company/contact attach/detach #133

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,15 @@ savedUser, err := ic.Users.Save(&user)
Adding a Company:

```go
companyList := intercom.CompanyList{
Companies: []intercom.Company{
{CompanyID: "5"},
},
}
user := intercom.User{
UserID: "27",
Companies: &companyList,
company := intercom.Company{
{CompanyID: "5"}
contact := intercom.Contact{
{ContactID: "6"}
}
savedContact, err := ic.Contacts.AttachContact(&contact, &company)
```

Removing is similar, but adding a `Remove: intercom.Bool(true)` attribute to a company.
Removing is similar, but use the `ic.Contacts.DetachContact(&contact, &company)` method instead.

#### Find

Expand Down
9 changes: 9 additions & 0 deletions company_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,12 @@ func (api CompanyAPI) getPlanName(company *Company) string {
}
return company.Plan.Name
}

func unmarshalToCompany(data []byte, err error) (Company, error) {
savedCompany := Company{}
if err != nil {
return savedCompany, err
}
err = json.Unmarshal(data, &savedCompany)
return savedCompany, err
}
16 changes: 13 additions & 3 deletions contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ type ContactService struct {

// ContactList holds a list of Contacts and paging information
type ContactList struct {
Pages PageParams
Contacts []Contact
Pages PageParams
Contacts []Contact
ScrollParam string `json:"scroll_param,omitempty"`
}

Expand Down Expand Up @@ -69,7 +69,7 @@ func (c *ContactService) List(params PageParams) (ContactList, error) {

// List all Contacts for App via Scroll API
func (c *ContactService) Scroll(scrollParam string) (ContactList, error) {
return c.Repository.scroll(scrollParam)
return c.Repository.scroll(scrollParam)
}

// ListByEmail looks up a list of Contacts by their Email.
Expand Down Expand Up @@ -97,6 +97,16 @@ func (c *ContactService) Update(contact *Contact) (Contact, error) {
return c.Repository.update(contact)
}

// AttachContact to Company
func (c *ContactService) AttachContact(contact *Contact, company *Company) (Company, error) {
return c.Repository.attachContact(contact, company)
}

// DetachContact from Company
func (c *ContactService) DetachContact(contact *Contact, company *Company) (Company, error) {
return c.Repository.detachContact(contact, company)
}

// Convert Contact to User
func (c *ContactService) Convert(contact *Contact, user *User) (User, error) {
return c.Repository.convert(contact, user)
Expand Down
37 changes: 29 additions & 8 deletions contact_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ type ContactRepository interface {
update(*Contact) (Contact, error)
convert(*Contact, *User) (User, error)
delete(id string) (Contact, error)
attachContact(*Contact, *Company) (Company, error)
detachContact(*Contact, *Company) (Company, error)
}

type requestContactAttachemntBody struct {
ID string `json:"id,omitempty"`
}

// ContactAPI implements ContactRepository
Expand Down Expand Up @@ -49,21 +55,30 @@ func (api ContactAPI) list(params contactListParams) (ContactList, error) {
}

func (api ContactAPI) scroll(scrollParam string) (ContactList, error) {
contactList := ContactList{}
params := scrollParams{ ScrollParam: scrollParam }
data, err := api.httpClient.Get("/contacts/scroll", params)
if err != nil {
return contactList, err
}
err = json.Unmarshal(data, &contactList)
return contactList, err
contactList := ContactList{}
params := scrollParams{ScrollParam: scrollParam}
data, err := api.httpClient.Get("/contacts/scroll", params)
if err != nil {
return contactList, err
}
err = json.Unmarshal(data, &contactList)
return contactList, err
}

func (api ContactAPI) create(contact *Contact) (Contact, error) {
requestContact := api.buildRequestContact(contact)
return unmarshalToContact(api.httpClient.Post("/contacts", &requestContact))
}

func (api ContactAPI) attachContact(contact *Contact, company *Company) (Company, error) {
requestContact := api.buildRequestContactAttachment(company)
return unmarshalToCompany(api.httpClient.Post(fmt.Sprintf("/contacts/%s/companies", contact.ID), &requestContact))
}

func (api ContactAPI) detachContact(contact *Contact, company *Company) (Company, error) {
return unmarshalToCompany(api.httpClient.Post(fmt.Sprintf("/contacts/%s/companies/%s", contact.ID, company.ID), nil))
}

func (api ContactAPI) update(contact *Contact) (Contact, error) {
requestContact := api.buildRequestContact(contact)
return unmarshalToContact(api.httpClient.Post("/contacts", &requestContact))
Expand Down Expand Up @@ -120,6 +135,12 @@ func (api ContactAPI) buildRequestContact(contact *Contact) requestUser {
}
}

func (api ContactAPI) buildRequestContactAttachment(company *Company) requestContactAttachemntBody {
return requestContactAttachemntBody{
ID: company.ID,
}
}

func (api ContactAPI) getCompaniesToSendFromContact(contact *Contact) []UserCompany {
if contact.Companies == nil {
return []UserCompany{}
Expand Down
22 changes: 22 additions & 0 deletions contact_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,25 @@ func TestContactAPIDelete(t *testing.T) {
t.Errorf("Expected UserID %s, got %s", "123", returned.UserID)
}
}

func TestContactAPIAttach(t *testing.T) {
http := TestUserHTTPClient{fixtureFilename: "fixtures/contact_attachment.json", expectedURI: "/contacts/b123d/companies", t: t}
api := ContactAPI{httpClient: &http}
contact := &Contact{ID: "b123d"}
company := &Company{ID: "46adad3f09126dca", Name: "My Co", CompanyID: "aa123"}
returned, _ := api.attachContact(contact, company)
if returned.ID != "46adad3f09126dca" {
t.Errorf("Expected ID %s, got %s", "46adad3f09126dca", returned.ID)
}
}

func TestContactAPIDetach(t *testing.T) {
http := TestUserHTTPClient{fixtureFilename: "fixtures/contact_attachment.json", expectedURI: "/contacts/b123d/companies/46adad3f09126dca", t: t}
api := ContactAPI{httpClient: &http}
contact := &Contact{ID: "b123d"}
company := &Company{ID: "46adad3f09126dca", Name: "My Co", CompanyID: "aa123"}
returned, _ := api.detachContact(contact, company)
if returned.ID != "46adad3f09126dca" {
t.Errorf("Expected ID %s, got %s, %s", "46adad3f09126dca", returned.ID, company)
}
}
8 changes: 8 additions & 0 deletions contact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,11 @@ func (t TestContactAPI) convert(c *Contact, u *User) (User, error) {
func (t TestContactAPI) delete(id string) (Contact, error) {
return Contact{ID: id}, nil
}

func (t TestContactAPI) attachContact(c *Contact, co *Company) (Company, error) {
return Company{ID: co.ID}, nil
}

func (t TestContactAPI) detachContact(c *Contact, co *Company) (Company, error) {
return Company{ID: co.ID}, nil
}
3 changes: 3 additions & 0 deletions fixtures/contact_attachment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"id": "46adad3f09126dca"
}