diff --git a/fixture/GET/user_related.json b/fixture/GET/user_related.json new file mode 100644 index 00000000..692d1230 --- /dev/null +++ b/fixture/GET/user_related.json @@ -0,0 +1,8 @@ +{ + "user_related": { + "assigned_tickets": 5, + "ccd_tickets": 3, + "organization_subscriptions": 1, + "requested_tickets": 10 + } +} diff --git a/zendesk/mock/client.go b/zendesk/mock/client.go index d7381e7c..5127e582 100644 --- a/zendesk/mock/client.go +++ b/zendesk/mock/client.go @@ -999,6 +999,21 @@ func (mr *ClientMockRecorder) GetUserFields(arg0, arg1 interface{}) *gomock.Call return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUserFields", reflect.TypeOf((*Client)(nil).GetUserFields), arg0, arg1) } +// GetUserRelated mocks base method. +func (m *Client) GetUserRelated(arg0 context.Context, arg1 int64) (zendesk.UserRelated, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetUserRelated", arg0, arg1) + ret0, _ := ret[0].(zendesk.UserRelated) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetUserRelated indicates an expected call of GetUserRelated. +func (mr *ClientMockRecorder) GetUserRelated(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUserRelated", reflect.TypeOf((*Client)(nil).GetUserRelated), arg0, arg1) +} + // GetUserTags mocks base method. func (m *Client) GetUserTags(arg0 context.Context, arg1 int64) ([]zendesk.Tag, error) { m.ctrl.T.Helper() diff --git a/zendesk/user.go b/zendesk/user.go index 1e62eda1..410eb987 100644 --- a/zendesk/user.go +++ b/zendesk/user.go @@ -91,6 +91,16 @@ 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) @@ -98,6 +108,7 @@ type UserAPI interface { 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 @@ -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 +} diff --git a/zendesk/user_test.go b/zendesk/user_test.go index 27af5acd..b6944393 100644 --- a/zendesk/user_test.go +++ b/zendesk/user_test.go @@ -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) + } +}