send notification to mention user

This commit is contained in:
efrilm 2025-09-21 11:52:39 +07:00
parent e4946d6e05
commit cf6708899d

View File

@ -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
}