Skip to content

Commit

Permalink
refactor: bug fix on chat_service
Browse files Browse the repository at this point in the history
  • Loading branch information
tahadostifam committed Dec 1, 2024
1 parent 7ad643c commit b2d0f57
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
1 change: 1 addition & 0 deletions internal/service/chat/chat_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
8 changes: 4 additions & 4 deletions internal/service/chat/chat_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
Expand All @@ -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
}
Expand Down
29 changes: 17 additions & 12 deletions tests/integration/service/chat_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,6 @@ func (s *ChatTestSuite) TestG_JoinChat_Channel() {
chatID primitive.ObjectID
userID string
Valid bool
Error error
}{
{
chatID: model.NewChatID(),
Expand All @@ -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 {
Expand All @@ -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")
}
Expand Down Expand Up @@ -484,7 +486,6 @@ func (s *ChatTestSuite) TestH_JoinChat_Group() {
chatID primitive.ObjectID
userID string
Valid bool
Error error
}{
{
chatID: model.NewChatID(),
Expand All @@ -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 {
Expand All @@ -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 {

Check failure on line 528 in tests/integration/service/chat_service_test.go

View workflow job for this annotation

GitHub Actions / linting

unnecessary trailing newline (whitespace)
require.Fail(s.T(), "not specific")
}
Expand Down

0 comments on commit b2d0f57

Please sign in to comment.