notification
This commit is contained in:
parent
709ffda4e1
commit
ca2acfd850
@ -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
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user