From 1048abc040e850434d4832f3c759949e89a5da85 Mon Sep 17 00:00:00 2001 From: nasrul faizin Date: Wed, 26 Oct 2022 16:05:23 +0700 Subject: [PATCH] feat: get user joined channel count --- chat/chat.go | 1 + chat/chat_user.go | 9 +++++ chat/chat_user_test.go | 90 ++++++++++++++++++++++++++++++++++++++++++ chat/model_user.go | 8 ++++ 4 files changed, 108 insertions(+) diff --git a/chat/chat.go b/chat/chat.go index abbdb97..00a2cda 100644 --- a/chat/chat.go +++ b/chat/chat.go @@ -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 diff --git a/chat/chat_user.go b/chat/chat_user.go index 0d00d9a..defc80d 100644 --- a/chat/chat_user.go +++ b/chat/chat_user.go @@ -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 +} diff --git a/chat/chat_user_test.go b/chat/chat_user_test.go index e78ff47..52a9712 100644 --- a/chat/chat_user_test.go +++ b/chat/chat_user_test.go @@ -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) + }) + } +} diff --git a/chat/model_user.go b/chat/model_user.go index 09f8cda..d0e9a47 100644 --- a/chat/model_user.go +++ b/chat/model_user.go @@ -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"`