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 users#ListSorted endpoint for sorting results #50

Closed
wants to merge 4 commits into from
Closed
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
6 changes: 6 additions & 0 deletions scroll.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package intercom

type ScrollParams struct {
Param string `json:"scroll_param" url:"scroll_param,omitempty"`
Type string `json:"type" url:"type,omitempty"`
}
17 changes: 17 additions & 0 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ type UserList struct {
Users []User
}

// UserScroll holds a list of Users and scrolling information.
type UserScroll struct {
ScrollParams
Users []User
}

// User represents a User within Intercom.
// Not all of the fields are writeable to the API, non-writeable fields are
// stripped out from the request. Please see the API documentation for details.
Expand Down Expand Up @@ -86,6 +92,7 @@ type userListParams struct {
PageParams
SegmentID string `url:"segment_id,omitempty"`
TagID string `url:"tag_id,omitempty"`
Sort string `url:"sort,omitempty"`
}

// FindByID looks up a User by their Intercom ID.
Expand Down Expand Up @@ -122,6 +129,16 @@ func (u *UserService) ListByTag(tagID string, params PageParams) (UserList, erro
return u.Repository.list(userListParams{PageParams: params, TagID: tagID})
}

// List Users Sorted.
func (u *UserService) ListSorted(sortBy string, params PageParams) (UserList, error) {
return u.Repository.list(userListParams{PageParams: params, Sort: sortBy})
}

// Scroll lists all Users for App.
func (u *UserService) Scroll(scrollParam string) (UserScroll, error) {
return u.Repository.scroll(scrollParam)
}

// Save a User, creating or updating them.
func (u *UserService) Save(user *User) (User, error) {
return u.Repository.save(user)
Expand Down
18 changes: 18 additions & 0 deletions user_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
type UserRepository interface {
find(UserIdentifiers) (User, error)
list(userListParams) (UserList, error)
scroll(scrollParam string) (UserScroll, error)
save(*User) (User, error)
delete(id string) (User, error)
}
Expand Down Expand Up @@ -62,6 +63,23 @@ func (api UserAPI) list(params userListParams) (UserList, error) {
return userList, err
}

func (api UserAPI) scroll(scrollParam string) (UserScroll, error) {
userScroll := UserScroll{}

data, err := api.httpClient.Get("/users/scroll", map[string]string{
"scroll_param": scrollParam,
})
if err != nil {
return userScroll, err
}

if err = json.Unmarshal(data, &userScroll); err != nil {
return userScroll, err
}

return userScroll, err
}

func (api UserAPI) save(user *User) (User, error) {
return unmarshalToUser(api.httpClient.Post("/users", RequestUserMapper{}.ConvertUser(user)))
}
Expand Down
37 changes: 37 additions & 0 deletions user_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package intercom

import (
"errors"
"testing"
)

Expand Down Expand Up @@ -33,6 +34,18 @@ func TestUserList(t *testing.T) {
}
}

func TestUserScroll(t *testing.T) {
scroll1, _ := (&UserService{Repository: TestUserAPI{t: t}}).Scroll("")
if scroll1.Users[0].ID != "46adad3f09126dcb" {
t.Errorf("First user not listed")
}

scroll2, _ := (&UserService{Repository: TestUserAPI{t: t}}).Scroll(scroll1.Param)
if scroll2.Users[0].ID != "46adad3f09126dca" {
t.Errorf("First user not listed")
}
}

func TestUserSave(t *testing.T) {
userService := UserService{Repository: TestUserAPI{t: t}}
user := User{ID: "46adad3f09126dca", CustomAttributes: map[string]interface{}{"is_cool": true}}
Expand Down Expand Up @@ -72,6 +85,30 @@ func (t TestUserAPI) list(params userListParams) (UserList, error) {
return UserList{Users: []User{User{ID: "46adad3f09126dca", Email: "[email protected]", UserID: "aa123"}}}, nil
}

func (t TestUserAPI) scroll(scrollParam string) (UserScroll, error) {
if scrollParam == "" {
return UserScroll{
Users: []User{
User{ID: "46adad3f09126dcb", Email: "[email protected]", UserID: "aa12345"},
},
ScrollParams: ScrollParams{
Param: "139e9131-4147-4f1e-a64e-f41edd71e6bc",
Type: "user.list",
},
}, nil
}

if scrollParam == "139e9131-4147-4f1e-a64e-f41edd71e6bc" {
return UserScroll{
Users: []User{
User{ID: "46adad3f09126dca", Email: "[email protected]", UserID: "aa123"},
},
}, nil
}

return UserScroll{}, errors.New("Scroll not found")
}

func (t TestUserAPI) save(user *User) (User, error) {
if user.ID != "46adad3f09126dca" {
t.t.Errorf("User ID was %s, expected 46adad3f09126dca", user.ID)
Expand Down