From b2d0f57989945c7377382fc659a6524fe71e815a Mon Sep 17 00:00:00 2001 From: "[ Taha. Dostifam ]" Date: Sun, 1 Dec 2024 14:39:45 +0330 Subject: [PATCH] refactor: bug fix on chat_service --- internal/service/chat/chat_errors.go | 1 + internal/service/chat/chat_service.go | 8 ++--- .../integration/service/chat_service_test.go | 29 +++++++++++-------- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/internal/service/chat/chat_errors.go b/internal/service/chat/chat_errors.go index 6ebbed3..c39ed05 100644 --- a/internal/service/chat/chat_errors.go +++ b/internal/service/chat/chat_errors.go @@ -7,6 +7,7 @@ var ( ErrCreateChat = errors.New("unable to create chat") ErrNotFound = errors.New("not found") + ErrUserNotFound = errors.New("user not found") ErrGetUserChats = errors.New("failed to get user chats") ErrChatAlreadyExists = errors.New("chat already exists") ErrUnableToAddChatToUsersList = errors.New("unable to add add chat to users list") diff --git a/internal/service/chat/chat_service.go b/internal/service/chat/chat_service.go index 177d208..0ab0b59 100644 --- a/internal/service/chat/chat_service.go +++ b/internal/service/chat/chat_service.go @@ -326,12 +326,12 @@ func (s *ChatService) JoinChat(ctx context.Context, chatID model.ChatID, userID } if !isMember { - err := s.chatRepo.JoinChat(ctx, chat.ChatType, userID, chatID) + _, err := s.userRepo.FindByUserID(ctx, userID) if err != nil { - return nil, &vali.Varror{Error: err} + return nil, &vali.Varror{Error: ErrUserNotFound} } - user, err := s.userRepo.FindByUserID(ctx, userID) + err = s.chatRepo.JoinChat(ctx, chat.ChatType, userID, chatID) if err != nil { return nil, &vali.Varror{Error: err} } @@ -340,7 +340,7 @@ func (s *ChatService) JoinChat(ctx context.Context, chatID model.ChatID, userID chatGetter.LastMessage = lastMessage return &JoinChatResult{ - Joined: user.IncludesChatID(chatID), + Joined: true, UpdatedChat: chatGetter, }, nil } diff --git a/tests/integration/service/chat_service_test.go b/tests/integration/service/chat_service_test.go index d795d62..362ddf7 100644 --- a/tests/integration/service/chat_service_test.go +++ b/tests/integration/service/chat_service_test.go @@ -417,7 +417,6 @@ func (s *ChatTestSuite) TestG_JoinChat_Channel() { chatID primitive.ObjectID userID string Valid bool - Error error }{ { chatID: model.NewChatID(), @@ -439,11 +438,6 @@ func (s *ChatTestSuite) TestG_JoinChat_Channel() { for _, tc := range testCases { joinResult, varror := s.service.JoinChat(ctx, tc.chatID, tc.userID) if !tc.Valid { - if tc.Error != nil { - require.Equal(s.T(), tc.Error, varror.Error) - continue - } - require.NotNil(s.T(), varror) } else if tc.Valid { if varror != nil { @@ -453,6 +447,14 @@ func (s *ChatTestSuite) TestG_JoinChat_Channel() { require.Nil(s.T(), varror) require.True(s.T(), joinResult.Joined) require.NotEmpty(s.T(), joinResult.UpdatedChat) + + chat, err := s.chatRepo.GetChat(ctx, tc.chatID) + require.Nil(s.T(), err) + + chatDetail, err := utils.TypeConverter[model.GroupChatDetail](chat.ChatDetail) + require.Nil(s.T(), err) + + require.True(s.T(), chatDetail.IsMember(tc.userID)) } else { require.Fail(s.T(), "not specific") } @@ -484,7 +486,6 @@ func (s *ChatTestSuite) TestH_JoinChat_Group() { chatID primitive.ObjectID userID string Valid bool - Error error }{ { chatID: model.NewChatID(), @@ -506,11 +507,6 @@ func (s *ChatTestSuite) TestH_JoinChat_Group() { for _, tc := range testCases { joinResult, varror := s.service.JoinChat(ctx, tc.chatID, tc.userID) if !tc.Valid { - if tc.Error != nil { - require.Equal(s.T(), tc.Error, varror.Error) - continue - } - require.NotNil(s.T(), varror) } else if tc.Valid { if varror != nil { @@ -520,6 +516,15 @@ func (s *ChatTestSuite) TestH_JoinChat_Group() { require.Nil(s.T(), varror) require.True(s.T(), joinResult.Joined) require.NotEmpty(s.T(), joinResult.UpdatedChat) + + chat, err := s.chatRepo.GetChat(ctx, tc.chatID) + require.Nil(s.T(), err) + + chatDetail, err := utils.TypeConverter[model.GroupChatDetail](chat.ChatDetail) + require.Nil(s.T(), err) + + require.True(s.T(), chatDetail.IsMember(tc.userID)) + } else { require.Fail(s.T(), "not specific") }