Skip to content

Commit

Permalink
Add support for user related information
Browse files Browse the repository at this point in the history
  • Loading branch information
jcmuller committed Oct 28, 2021
1 parent 9c7dd24 commit cb1d815
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
8 changes: 8 additions & 0 deletions fixture/GET/user_related.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"user_related": {
"assigned_tickets": 5,
"ccd_tickets": 3,
"organization_subscriptions": 1,
"requested_tickets": 10
}
}
15 changes: 15 additions & 0 deletions zendesk/mock/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions zendesk/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,24 @@ type GetManyUsersOptions struct {
IDs string `json:"ids,omitempty" url:"ids,omitempty"`
}

// UserRelated contains user related data
//
// ref: https://developer.zendesk.com/api-reference/ticketing/users/users/#show-user-related-information
type UserRelated struct {
AssignedTickets int64 `json:"assigned_tickets"`
RequestedTickets int64 `json:"requested_tickets"`
CCDTickets int64 `json:"ccd_tickets"`
OrganizationSubscriptions int64 `json:"organization_subscriptions"`
}

// UserAPI an interface containing all user related methods
type UserAPI interface {
GetManyUsers(ctx context.Context, opts *GetManyUsersOptions) ([]User, Page, error)
GetUsers(ctx context.Context, opts *UserListOptions) ([]User, Page, error)
GetUser(ctx context.Context, userID int64) (User, error)
CreateUser(ctx context.Context, user User) (User, error)
UpdateUser(ctx context.Context, userID int64, user User) (User, error)
GetUserRelated(ctx context.Context, userID int64) (UserRelated, error)
}

// GetUsers fetch user list
Expand Down Expand Up @@ -223,3 +234,22 @@ func (z *Client) UpdateUser(ctx context.Context, userID int64, user User) (User,
}
return result.User, nil
}

// GetUserRelated retrieves user related user information
// ref: https://developer.zendesk.com/api-reference/ticketing/users/users/#show-user-related-information
func (z *Client) GetUserRelated(ctx context.Context, userID int64) (UserRelated, error) {
var data struct {
UserRelated UserRelated `json:"user_related"`
}

body, err := z.get(ctx, fmt.Sprintf("/users/%d/related.json", userID))
if err != nil {
return UserRelated{}, err
}

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

return data.UserRelated, nil
}
16 changes: 16 additions & 0 deletions zendesk/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,19 @@ func TestUpdateUserFailure(t *testing.T) {
t.Fatal("Client did not return error when api failed")
}
}

func TestGetUserRelated(t *testing.T) {
mockAPI := newMockAPIWithStatus(http.MethodGet, "user_related.json", http.StatusOK)
client := newTestClient(mockAPI)
defer mockAPI.Close()

userRelated, err := client.GetUserRelated(ctx, 369531345753)
if err != nil {
t.Fatalf("Failed to get user related information: %s", err)
}

expectedAssignedTickets := int64(5)
if userRelated.AssignedTickets != expectedAssignedTickets {
t.Fatalf("Returned user does not have the expected assigned tickets %d. It is %d", expectedAssignedTickets, userRelated.AssignedTickets)
}
}

0 comments on commit cb1d815

Please sign in to comment.