diff --git a/README.md b/README.md index 5a177c0..fd475e2 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/company_api.go b/company_api.go index 2e43b14..6a8a4a5 100644 --- a/company_api.go +++ b/company_api.go @@ -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 +} diff --git a/contact.go b/contact.go index 2bff906..39fe4d8 100644 --- a/contact.go +++ b/contact.go @@ -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"` } @@ -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. @@ -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) diff --git a/contact_api.go b/contact_api.go index f42384a..1d68694 100644 --- a/contact_api.go +++ b/contact_api.go @@ -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 @@ -49,14 +55,14 @@ 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) { @@ -64,6 +70,15 @@ func (api ContactAPI) create(contact *Contact) (Contact, error) { 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)) @@ -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{} diff --git a/contact_api_test.go b/contact_api_test.go index 1fe5f50..d57d63e 100644 --- a/contact_api_test.go +++ b/contact_api_test.go @@ -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) + } +} diff --git a/contact_test.go b/contact_test.go index 88f593b..2b2a525 100644 --- a/contact_test.go +++ b/contact_test.go @@ -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 +} diff --git a/fixtures/contact_attachment.json b/fixtures/contact_attachment.json new file mode 100644 index 0000000..3700795 --- /dev/null +++ b/fixtures/contact_attachment.json @@ -0,0 +1,3 @@ +{ + "id": "46adad3f09126dca" +} \ No newline at end of file