diff --git a/internal/service/letter_service.go b/internal/service/letter_service.go index df936cf..702c06a 100644 --- a/internal/service/letter_service.go +++ b/internal/service/letter_service.go @@ -510,7 +510,6 @@ func (s *LetterServiceImpl) CreateDiscussion(ctx context.Context, letterID uuid. // Log activity for discussion creation if s.activityLogger != nil && result != nil { - // Create a simple activity log if err := s.activityLogger.LogLetterDispositionStatusUpdate(txCtx, letterID, userID, "discussion_created"); err != nil { // Don't fail the transaction for logging errors } @@ -523,6 +522,11 @@ func (s *LetterServiceImpl) CreateDiscussion(ctx context.Context, letterID uuid. return nil, err } + // Send notifications to mentioned users asynchronously + if s.notificationProcessor != nil && req.Mentions != nil { + go s.sendDiscussionMentionNotifications(context.Background(), letterID, userID, req.Mentions, req.Message) + } + return result, nil } @@ -590,3 +594,68 @@ func (s *LetterServiceImpl) BulkArchiveIncomingLetters(ctx context.Context, lett ArchivedCount: int(archivedCount), }, nil } + +func (s *LetterServiceImpl) sendDiscussionMentionNotifications(ctx context.Context, letterID uuid.UUID, senderUserID uuid.UUID, mentions map[string]interface{}, message string) { + // Extract user_ids from mentions + userIDs := s.extractUserIDsFromMentions(mentions) + if len(userIDs) == 0 { + return + } + + // Get letter details for notification + letter, err := s.processor.GetIncomingLetterByID(ctx, letterID) + if err != nil { + return + } + + // Get sender user name (you might need to implement this) + appContext := appcontext.FromGinContext(ctx) + senderName := appContext.UserName // or get from user service + + // Send notification to each mentioned user + for _, mentionedUserID := range userIDs { + // Don't send notification to the sender themselves + if mentionedUserID == senderUserID { + continue + } + + subject := "Anda Disebutkan dalam Diskusi" + notificationMessage := fmt.Sprintf("%s menyebutkan Anda dalam diskusi surat: %s", senderName, letter.Subject) + + err := s.notificationProcessor.SendIncomingLetterNotification( + ctx, + letterID, + mentionedUserID, + subject, + notificationMessage) + + if err != nil { + // Log error but continue with other notifications + } + } +} + +func (s *LetterServiceImpl) extractUserIDsFromMentions(mentions map[string]interface{}) []uuid.UUID { + userIDs := make([]uuid.UUID, 0) + + if userIDsInterface, exists := mentions["user_ids"]; exists { + switch userIDsValue := userIDsInterface.(type) { + case []interface{}: + for _, userIDInterface := range userIDsValue { + if userIDStr, ok := userIDInterface.(string); ok { + if userID, err := uuid.Parse(userIDStr); err == nil { + userIDs = append(userIDs, userID) + } + } + } + case []string: + for _, userIDStr := range userIDsValue { + if userID, err := uuid.Parse(userIDStr); err == nil { + userIDs = append(userIDs, userID) + } + } + } + } + + return userIDs +} \ No newline at end of file