Skip to content

Commit b2d0f57

Browse files
committed
refactor: bug fix on chat_service
1 parent 7ad643c commit b2d0f57

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

internal/service/chat/chat_errors.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var (
77

88
ErrCreateChat = errors.New("unable to create chat")
99
ErrNotFound = errors.New("not found")
10+
ErrUserNotFound = errors.New("user not found")
1011
ErrGetUserChats = errors.New("failed to get user chats")
1112
ErrChatAlreadyExists = errors.New("chat already exists")
1213
ErrUnableToAddChatToUsersList = errors.New("unable to add add chat to users list")

internal/service/chat/chat_service.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,12 +326,12 @@ func (s *ChatService) JoinChat(ctx context.Context, chatID model.ChatID, userID
326326
}
327327

328328
if !isMember {
329-
err := s.chatRepo.JoinChat(ctx, chat.ChatType, userID, chatID)
329+
_, err := s.userRepo.FindByUserID(ctx, userID)
330330
if err != nil {
331-
return nil, &vali.Varror{Error: err}
331+
return nil, &vali.Varror{Error: ErrUserNotFound}
332332
}
333333

334-
user, err := s.userRepo.FindByUserID(ctx, userID)
334+
err = s.chatRepo.JoinChat(ctx, chat.ChatType, userID, chatID)
335335
if err != nil {
336336
return nil, &vali.Varror{Error: err}
337337
}
@@ -340,7 +340,7 @@ func (s *ChatService) JoinChat(ctx context.Context, chatID model.ChatID, userID
340340
chatGetter.LastMessage = lastMessage
341341

342342
return &JoinChatResult{
343-
Joined: user.IncludesChatID(chatID),
343+
Joined: true,
344344
UpdatedChat: chatGetter,
345345
}, nil
346346
}

tests/integration/service/chat_service_test.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,6 @@ func (s *ChatTestSuite) TestG_JoinChat_Channel() {
417417
chatID primitive.ObjectID
418418
userID string
419419
Valid bool
420-
Error error
421420
}{
422421
{
423422
chatID: model.NewChatID(),
@@ -439,11 +438,6 @@ func (s *ChatTestSuite) TestG_JoinChat_Channel() {
439438
for _, tc := range testCases {
440439
joinResult, varror := s.service.JoinChat(ctx, tc.chatID, tc.userID)
441440
if !tc.Valid {
442-
if tc.Error != nil {
443-
require.Equal(s.T(), tc.Error, varror.Error)
444-
continue
445-
}
446-
447441
require.NotNil(s.T(), varror)
448442
} else if tc.Valid {
449443
if varror != nil {
@@ -453,6 +447,14 @@ func (s *ChatTestSuite) TestG_JoinChat_Channel() {
453447
require.Nil(s.T(), varror)
454448
require.True(s.T(), joinResult.Joined)
455449
require.NotEmpty(s.T(), joinResult.UpdatedChat)
450+
451+
chat, err := s.chatRepo.GetChat(ctx, tc.chatID)
452+
require.Nil(s.T(), err)
453+
454+
chatDetail, err := utils.TypeConverter[model.GroupChatDetail](chat.ChatDetail)
455+
require.Nil(s.T(), err)
456+
457+
require.True(s.T(), chatDetail.IsMember(tc.userID))
456458
} else {
457459
require.Fail(s.T(), "not specific")
458460
}
@@ -484,7 +486,6 @@ func (s *ChatTestSuite) TestH_JoinChat_Group() {
484486
chatID primitive.ObjectID
485487
userID string
486488
Valid bool
487-
Error error
488489
}{
489490
{
490491
chatID: model.NewChatID(),
@@ -506,11 +507,6 @@ func (s *ChatTestSuite) TestH_JoinChat_Group() {
506507
for _, tc := range testCases {
507508
joinResult, varror := s.service.JoinChat(ctx, tc.chatID, tc.userID)
508509
if !tc.Valid {
509-
if tc.Error != nil {
510-
require.Equal(s.T(), tc.Error, varror.Error)
511-
continue
512-
}
513-
514510
require.NotNil(s.T(), varror)
515511
} else if tc.Valid {
516512
if varror != nil {
@@ -520,6 +516,15 @@ func (s *ChatTestSuite) TestH_JoinChat_Group() {
520516
require.Nil(s.T(), varror)
521517
require.True(s.T(), joinResult.Joined)
522518
require.NotEmpty(s.T(), joinResult.UpdatedChat)
519+
520+
chat, err := s.chatRepo.GetChat(ctx, tc.chatID)
521+
require.Nil(s.T(), err)
522+
523+
chatDetail, err := utils.TypeConverter[model.GroupChatDetail](chat.ChatDetail)
524+
require.Nil(s.T(), err)
525+
526+
require.True(s.T(), chatDetail.IsMember(tc.userID))
527+
523528
} else {
524529
require.Fail(s.T(), "not specific")
525530
}

0 commit comments

Comments
 (0)