Skip to content

Commit

Permalink
Merge pull request #111 from pastelInc/company-users
Browse files Browse the repository at this point in the history
Add company users API
  • Loading branch information
jonnyom authored Feb 12, 2019
2 parents 29c294d + 1fb3ad5 commit a7c3571
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 1 deletion.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,17 @@ companyList, err := ic.Companies.ListBySegment("segmentID123", intercom.PagePara
companyList, err := ic.Companies.ListByTag("42", intercom.PageParams{})
```

#### ListUsers

```go
userList, err := ic.Companies.ListUsersByID("46adad3f09126dca", intercom.PageParams{})
userList.Users // []User
```

```go
userList, err := ic.Companies.ListUsersByCompanyID("27", intercom.PageParams{})
```

### Events

#### Save
Expand Down
21 changes: 21 additions & 0 deletions company.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ type companyListParams struct {
TagID string `url:"tag_id,omitempty"`
}

type companyUserListParams struct {
ID string `url:"-"`
CompanyID string `url:"company_id,omitempty"`
Type string `url:"type,omitempty"`
PageParams
}

// FindByID finds a Company using their Intercom ID
func (c *CompanyService) FindByID(id string) (Company, error) {
return c.findWithIdentifiers(CompanyIdentifiers{ID: id})
Expand Down Expand Up @@ -89,6 +96,20 @@ func (c *CompanyService) ListByTag(tagID string, params PageParams) (CompanyList
return c.Repository.list(companyListParams{PageParams: params, TagID: tagID})
}

// List Company Users by ID
func (c *CompanyService) ListUsersByID(id string, params PageParams) (UserList, error) {
return c.listUsersWithIdentifiers(id, companyUserListParams{PageParams: params})
}

// List Company Users by CompanyID
func (c *CompanyService) ListUsersByCompanyID(companyID string, params PageParams) (UserList, error) {
return c.listUsersWithIdentifiers("", companyUserListParams{CompanyID: companyID, Type: "user", PageParams: params})
}

func (c *CompanyService) listUsersWithIdentifiers(id string, params companyUserListParams) (UserList, error) {
return c.Repository.listUsers(id, params)
}

// List all Companies for App via Scroll API
func (c *CompanyService) Scroll(scrollParam string) (CompanyList, error) {
return c.Repository.scroll(scrollParam)
Expand Down
23 changes: 22 additions & 1 deletion company_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
type CompanyRepository interface {
find(CompanyIdentifiers) (Company, error)
list(companyListParams) (CompanyList, error)
listUsers(string, companyUserListParams) (UserList, error)
scroll(scrollParam string) (CompanyList, error)
save(*Company) (Company, error)
}
Expand Down Expand Up @@ -61,9 +62,29 @@ func (api CompanyAPI) list(params companyListParams) (CompanyList, error) {
return companyList, err
}

func (api CompanyAPI) listUsers(id string, params companyUserListParams) (UserList, error) {
companyUserList := UserList{}
data, err := api.getClientForListUsers(id, params)
if err != nil {
return companyUserList, err
}
err = json.Unmarshal(data, &companyUserList)
return companyUserList, err
}

func (api CompanyAPI) getClientForListUsers(id string, params companyUserListParams) ([]byte, error) {
switch {
case id != "":
return api.httpClient.Get(fmt.Sprintf("/companies/%s/users", id), params)
case params.CompanyID != "", params.Type == "user":
return api.httpClient.Get("/companies", params)
}
return nil, errors.New("Missing Company Identifier")
}

func (api CompanyAPI) scroll(scrollParam string) (CompanyList, error) {
companyList := CompanyList{}
params := scrollParams{ScrollParam: scrollParam }
params := scrollParams{ScrollParam: scrollParam}
data, err := api.httpClient.Get("/companies/scroll", params)
if err != nil {
return companyList, err
Expand Down
14 changes: 14 additions & 0 deletions company_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ func TestCompanyAPIFind(t *testing.T) {
}
}

func TestCompanyAPIListUsers(t *testing.T) {
http := TestCompanyHTTPClient{fixtureFilename: "fixtures/users.json", expectedURI: "/companies/54c42ed71623d8caa/users", t: t}
api := CompanyAPI{httpClient: &http}
params := companyUserListParams{Type: "user"}
companyUserList, err := api.listUsers("54c42ed71623d8caa", params)
if err != nil {
t.Errorf("Error parsing fixture %s", err)
}
users := companyUserList.Users
if users[0].ID != "54c42e7ea7a765fa7" {
t.Errorf("ID was %s, expected 54c42e7ea7a765fa7", users[0].ID)
}
}

func TestCompanyAPIFindByName(t *testing.T) {
http := TestCompanyHTTPClient{fixtureFilename: "fixtures/company.json", expectedURI: "/companies", t: t}
api := CompanyAPI{httpClient: &http}
Expand Down
20 changes: 20 additions & 0 deletions company_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ func TestCompanyList(t *testing.T) {
}
}

func TestCompanyListUsersByID(t *testing.T) {
companyUserList, _ := (&CompanyService{Repository: TestCompanyAPI{t: t}}).ListUsersByID("46adad3f09126dca", PageParams{})
users := companyUserList.Users
if users[0].Companies.Companies[0].ID != "46adad3f09126dca" {
t.Errorf("User not listed")
}
}

func TestCompanyListUsersByCompanyID(t *testing.T) {
companyUserList, _ := (&CompanyService{Repository: TestCompanyAPI{t: t}}).ListUsersByCompanyID("134d", PageParams{})
users := companyUserList.Users
if users[0].Companies.Companies[0].CompanyID != "134d" {
t.Errorf("User not listed")
}
}

func TestCompanySave(t *testing.T) {
companyService := CompanyService{Repository: TestCompanyAPI{t: t}}
company := Company{ID: "46adad3f09126dca", CustomAttributes: map[string]interface{}{"is_cool": true}}
Expand All @@ -51,6 +67,10 @@ func (t TestCompanyAPI) list(params companyListParams) (CompanyList, error) {
return CompanyList{Companies: []Company{Company{ID: "46adad3f09126dca", Name: "My Co", CompanyID: "aa123"}}}, nil
}

func (t TestCompanyAPI) listUsers(id string, params companyUserListParams) (UserList, error) {
return UserList{Users: []User{User{Companies: &CompanyList{Companies: []Company{Company{ID: id, CompanyID: params.CompanyID}}}}}}, nil
}

func (t TestCompanyAPI) scroll(scrollParam string) (CompanyList, error) {
return CompanyList{Companies: []Company{Company{ID: "46adad3f09126dca", Name: "My Co", CompanyID: "aa123"}}}, nil
}
Expand Down

0 comments on commit a7c3571

Please sign in to comment.