From ca2acfd850f6e283fd9768b3f7b858bd6676a09c Mon Sep 17 00:00:00 2001 From: efrilm Date: Sun, 21 Sep 2025 16:51:26 +0700 Subject: [PATCH] notification --- internal/service/letter_outgoing_service.go | 88 +++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/internal/service/letter_outgoing_service.go b/internal/service/letter_outgoing_service.go index d33b075..777c749 100644 --- a/internal/service/letter_outgoing_service.go +++ b/internal/service/letter_outgoing_service.go @@ -730,6 +730,11 @@ func (s *LetterOutgoingServiceImpl) CreateDiscussion(ctx context.Context, letter return nil, err } + if s.notificationProcessor != nil && req.Mentions != nil { + go s.sendOutgoingDiscussionMentionNotifications(context.Background(), letterID, userID, req.Mentions, req.Message) + } + + return transformDiscussionToResponse(result), nil } @@ -1633,3 +1638,86 @@ func (s *LetterOutgoingServiceImpl) sendApprovalNotificationToCreator(ctx contex log.Printf("[DEBUG] Successfully sent notification to creator %s", creatorID.String()) } } + +func (s *LetterOutgoingServiceImpl) sendOutgoingDiscussionMentionNotifications(ctx context.Context, letterID uuid.UUID, senderUserID uuid.UUID, mentions map[string]interface{}, message string) { + log.Printf("[DEBUG] sendOutgoingDiscussionMentionNotifications START - LetterID: %s", letterID.String()) + + // Extract user_ids dari mentions + userIDs := s.extractUserIDsFromMentions(mentions) + if len(userIDs) == 0 { + log.Printf("[DEBUG] No user IDs found in mentions") + return + } + + log.Printf("[DEBUG] Found %d mentioned users", len(userIDs)) + + // Get letter details untuk notification + letter, err := s.processor.GetOutgoingLetterByID(ctx, letterID) + if err != nil { + log.Printf("[ERROR] Failed to get letter details: %v", err) + return + } + + // Get sender user name dari context (bisa juga dari user service) + appContext := appcontext.FromGinContext(ctx) + senderName := appContext.UserName + if senderName == "" { + senderName = "Seseorang" // fallback jika nama tidak tersedia + } + + // Kirim notification ke setiap mentioned user + for _, mentionedUserID := range userIDs { + // Jangan kirim notification ke sender sendiri + if mentionedUserID == senderUserID { + continue + } + + subject := "Anda Disebutkan dalam Diskusi Surat Keluar" + notificationMessage := fmt.Sprintf("%s menyebutkan Anda dalam diskusi surat keluar: %s", senderName, letter.Subject) + + err := s.notificationProcessor.SendOutgoingLetterNotification( + ctx, + letterID, + mentionedUserID, + subject, + notificationMessage) + + if err != nil { + log.Printf("[ERROR] Failed to send mention notification to user %s: %v", mentionedUserID.String(), err) + } else { + log.Printf("[DEBUG] Successfully sent mention notification to user %s", mentionedUserID.String()) + } + } +} + +// Helper function untuk extract user IDs dari mentions map +func (s *LetterOutgoingServiceImpl) extractUserIDsFromMentions(mentions map[string]interface{}) []uuid.UUID { + userIDs := make([]uuid.UUID, 0) + + if mentions == nil { + return userIDs + } + + 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) + } + } + case []uuid.UUID: + userIDs = userIDsValue + } + } + + return userIDs +} \ No newline at end of file