Skip to content

Commit

Permalink
feat: get user joined channel count
Browse files Browse the repository at this point in the history
  • Loading branch information
nasrul21 committed Oct 26, 2022
1 parent 3a7f9f7 commit 1048abc
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
1 change: 1 addition & 0 deletions chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
type Chat interface {
// user
GetUserUnreadMessages(ctx context.Context, params UserUnreadMessagesParams) (UserUnreadMessages, *errors.Error)
GetUserJoinedChannelCount(ctx context.Context, params UserJoinedChannelCountParams) (resp UserJoinedChannelCountResponse, err *errors.Error)
CreateUser(ctx context.Context, request CreateUserRequest) (resp CreateUserResponse, err *errors.Error)

// channel
Expand Down
9 changes: 9 additions & 0 deletions chat/chat_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,12 @@ func (c *ChatImpl) GetUserUnreadMessages(ctx context.Context, params UserUnreadM
}
return
}

func (c *ChatImpl) GetUserJoinedChannelCount(ctx context.Context, params UserJoinedChannelCountParams) (resp UserJoinedChannelCountResponse, err *errors.Error) {
url := fmt.Sprintf("/v3/users/%s/group_channel_count", params.UserID)
err = c.Client.Call(ctx, http.MethodGet, url, nil, nil, &resp)
if err != nil {
return
}
return
}
90 changes: 90 additions & 0 deletions chat/chat_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,93 @@ func TestCreateUser(t *testing.T) {
})
}
}

type mockGetUserJoinedChannelCount struct {
mock.Mock
}

func (m *mockGetUserJoinedChannelCount) Call(ctx context.Context, method string, url string, header http.Header, body interface{}, result interface{}) *errors.Error {
args := m.Called(ctx, method, url, header, body, result)
if args.Get(0) != nil {
return args.Get(0).(*errors.Error)
}
result.(*UserJoinedChannelCountResponse).GroupChannelCount = 5
return nil
}

func TestGetUserJoinedChannelCount(t *testing.T) {
ctx := context.Background()

type args struct {
ctx context.Context
params UserJoinedChannelCountParams
}

tests := []struct {
name string
args args
setupMock func(m *mockGetUserJoinedChannelCount)
wantExpected UserJoinedChannelCountResponse
wantError *errors.Error
}{
{
name: "success",
args: args{ctx: ctx, params: UserJoinedChannelCountParams{UserID: "111001100"}},
setupMock: func(m *mockGetUserJoinedChannelCount) {
m.On(
"Call",
ctx,
http.MethodGet,
fmt.Sprintf("/v3/users/%s/group_channel_count", "111001100"),
http.Header(nil),
nil,
&UserJoinedChannelCountResponse{},
).Return(nil)
},
wantExpected: UserJoinedChannelCountResponse{GroupChannelCount: 5},
wantError: nil,
},
{
name: "failed user not found",
args: args{ctx: ctx, params: UserJoinedChannelCountParams{UserID: "000000000"}},
setupMock: func(m *mockGetUserJoinedChannelCount) {
m.On(
"Call",
ctx,
http.MethodGet,
fmt.Sprintf("/v3/users/%s/group_channel_count", "000000000"),
http.Header(nil),
nil,
&UserJoinedChannelCountResponse{},
).Return(errors.FromHTTPErr(400, []byte(`
{
"message": "\"User\" not found.",
"code": 400201,
"error": true
}
`)))
},
wantExpected: UserJoinedChannelCountResponse{},
wantError: errors.FromHTTPErr(400, []byte(`
{
"message": "\"User\" not found.",
"code": 400201,
"error": true
}
`)),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockClient := new(mockGetUserJoinedChannelCount)
tt.setupMock(mockClient)

chatService := New(mockClient)

resp, err := chatService.GetUserJoinedChannelCount(tt.args.ctx, tt.args.params)
assert.Equal(t, tt.wantExpected, resp)
assert.Equal(t, tt.wantError, err)
})
}
}
8 changes: 8 additions & 0 deletions chat/model_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ type UserUnreadMessagesParams struct {
UserID string `json:"user_id"`
}

type UserJoinedChannelCountParams struct {
UserID string `json:"user_id"`
}

type UserJoinedChannelCountResponse struct {
GroupChannelCount int `json:"group_channel_count"`
}

type CreateUserRequest struct {
UserID string `json:"user_id"`
Nickname string `json:"nickname"`
Expand Down

0 comments on commit 1048abc

Please sign in to comment.