update api incoming letter
This commit is contained in:
@@ -37,6 +37,7 @@ type LetterProcessor interface {
|
||||
|
||||
// Batch loading methods
|
||||
GetBatchAttachments(ctx context.Context, letterIDs []uuid.UUID) (map[uuid.UUID][]entities.LetterIncomingAttachment, error)
|
||||
GetBatchDispositions(ctx context.Context, letterIDs []uuid.UUID) (map[uuid.UUID][]entities.LetterIncomingDisposition, error)
|
||||
GetBatchPriorities(ctx context.Context, priorityIDs []uuid.UUID) (map[uuid.UUID]*entities.Priority, error)
|
||||
GetBatchInstitutions(ctx context.Context, institutionIDs []uuid.UUID) (map[uuid.UUID]*entities.Institution, error)
|
||||
GetBatchRecipientsByUser(ctx context.Context, letterIDs []uuid.UUID, userID uuid.UUID) (map[uuid.UUID]*entities.LetterIncomingRecipient, error)
|
||||
@@ -347,6 +348,7 @@ func (s *LetterServiceImpl) ListIncomingLetters(ctx context.Context, req *contra
|
||||
priorities map[uuid.UUID]*entities.Priority
|
||||
institutions map[uuid.UUID]*entities.Institution
|
||||
recipients map[uuid.UUID]*entities.LetterIncomingRecipient
|
||||
dispostions map[uuid.UUID][]entities.LetterIncomingDisposition
|
||||
err error
|
||||
}
|
||||
|
||||
@@ -358,9 +360,10 @@ func (s *LetterServiceImpl) ListIncomingLetters(ctx context.Context, req *contra
|
||||
priorities: make(map[uuid.UUID]*entities.Priority),
|
||||
institutions: make(map[uuid.UUID]*entities.Institution),
|
||||
recipients: make(map[uuid.UUID]*entities.LetterIncomingRecipient),
|
||||
dispostions: make(map[uuid.UUID][]entities.LetterIncomingDisposition),
|
||||
}
|
||||
|
||||
errChan := make(chan error, 4)
|
||||
errChan := make(chan error, 5)
|
||||
|
||||
go func() {
|
||||
var err error
|
||||
@@ -386,7 +389,13 @@ func (s *LetterServiceImpl) ListIncomingLetters(ctx context.Context, req *contra
|
||||
errChan <- err
|
||||
}()
|
||||
|
||||
for i := 0; i < 4; i++ {
|
||||
go func() {
|
||||
var err error
|
||||
result.dispostions, err = s.processor.GetBatchDispositions(ctx, letterIDs)
|
||||
errChan <- err
|
||||
}()
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
if err := <-errChan; err != nil {
|
||||
// Batch load error, continue anyway
|
||||
}
|
||||
@@ -414,12 +423,17 @@ func (s *LetterServiceImpl) ListIncomingLetters(ctx context.Context, req *contra
|
||||
institution = batchData.institutions[*letter.SenderInstitutionID]
|
||||
}
|
||||
|
||||
dispositions := batchData.dispostions[letter.ID]
|
||||
if dispositions == nil {
|
||||
dispositions = []entities.LetterIncomingDisposition{}
|
||||
}
|
||||
|
||||
isRead := false
|
||||
if recipient, exists := batchData.recipients[letter.ID]; exists && recipient != nil {
|
||||
isRead = recipient.ReadAt != nil
|
||||
}
|
||||
|
||||
resp := transformer.LetterEntityToContract(&letter, attachments, priority, institution)
|
||||
resp := transformer.LetterEntityToContract(&letter, attachments, dispositions, priority, institution)
|
||||
resp.IsRead = isRead
|
||||
respList = append(respList, *resp)
|
||||
}
|
||||
@@ -697,7 +711,7 @@ func (s *LetterServiceImpl) CreateDispositions(ctx context.Context, req *contrac
|
||||
if len(recipients) > 0 {
|
||||
go s.sendDispositionNotifications(context.Background(), req.LetterID, recipients)
|
||||
}
|
||||
|
||||
|
||||
// Send notification to letter creator about new disposition
|
||||
go s.sendDispositionCreatorNotification(context.Background(), req.LetterID, userID)
|
||||
}
|
||||
@@ -711,7 +725,7 @@ func (s *LetterServiceImpl) GetEnhancedDispositionsByLetter(ctx context.Context,
|
||||
|
||||
func (s *LetterServiceImpl) CreateDiscussion(ctx context.Context, letterID uuid.UUID, req *contract.CreateLetterDiscussionRequest) (*contract.LetterDiscussionResponse, error) {
|
||||
userID := appcontext.FromGinContext(ctx).UserID
|
||||
|
||||
|
||||
var result *contract.LetterDiscussionResponse
|
||||
err := s.txManager.WithTransaction(ctx, func(txCtx context.Context) error {
|
||||
var err error
|
||||
@@ -719,34 +733,34 @@ func (s *LetterServiceImpl) CreateDiscussion(ctx context.Context, letterID uuid.
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
// Log activity for discussion creation
|
||||
if s.activityLogger != nil && result != nil {
|
||||
if err := s.activityLogger.LogLetterDispositionStatusUpdate(txCtx, letterID, userID, "discussion_created"); err != nil {
|
||||
// Don't fail the transaction for logging errors
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
|
||||
if err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
func (s *LetterServiceImpl) UpdateDiscussion(ctx context.Context, letterID uuid.UUID, discussionID uuid.UUID, req *contract.UpdateLetterDiscussionRequest) (*contract.LetterDiscussionResponse, error) {
|
||||
userID := appcontext.FromGinContext(ctx).UserID
|
||||
|
||||
|
||||
var result *contract.LetterDiscussionResponse
|
||||
|
||||
|
||||
err := s.txManager.WithTransaction(ctx, func(txCtx context.Context) error {
|
||||
var err error
|
||||
var oldMessage string
|
||||
@@ -754,7 +768,7 @@ func (s *LetterServiceImpl) UpdateDiscussion(ctx context.Context, letterID uuid.
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
// Log activity for discussion update (could use oldMessage for more detailed logging)
|
||||
if s.activityLogger != nil && result != nil {
|
||||
// Create a simple activity log - oldMessage could be included in a more detailed log
|
||||
@@ -763,14 +777,14 @@ func (s *LetterServiceImpl) UpdateDiscussion(ctx context.Context, letterID uuid.
|
||||
// Don't fail the transaction for logging errors
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -780,14 +794,14 @@ func (s *LetterServiceImpl) GetDepartmentDispositionStatus(ctx context.Context,
|
||||
|
||||
func (s *LetterServiceImpl) UpdateDispositionStatus(ctx context.Context, req *contract.UpdateDispositionStatusRequest) (*contract.DepartmentDispositionStatusResponse, error) {
|
||||
var result *contract.DepartmentDispositionStatusResponse
|
||||
|
||||
|
||||
err := s.txManager.WithTransaction(ctx, func(txCtx context.Context) error {
|
||||
var err error
|
||||
result, err = s.processor.UpdateDispositionStatus(txCtx, req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
// Log activity for disposition status update
|
||||
if s.activityLogger != nil && result != nil {
|
||||
userID := appcontext.FromGinContext(txCtx).UserID
|
||||
@@ -795,19 +809,19 @@ func (s *LetterServiceImpl) UpdateDispositionStatus(ctx context.Context, req *co
|
||||
// Don't fail the transaction for logging errors
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
// Send notification to letter creator asynchronously
|
||||
if s.notificationProcessor != nil && result != nil {
|
||||
go s.sendDispositionStatusUpdateNotification(context.Background(), req.LetterIncomingID, req.Status)
|
||||
}
|
||||
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -840,34 +854,34 @@ func (s *LetterServiceImpl) sendDiscussionMentionNotifications(ctx context.Conte
|
||||
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
|
||||
}
|
||||
@@ -876,7 +890,7 @@ func (s *LetterServiceImpl) sendDiscussionMentionNotifications(ctx context.Conte
|
||||
|
||||
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{}:
|
||||
@@ -895,7 +909,7 @@ func (s *LetterServiceImpl) extractUserIDsFromMentions(mentions map[string]inter
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return userIDs
|
||||
}
|
||||
|
||||
@@ -909,8 +923,6 @@ func (s *LetterServiceImpl) sendDispositionCreatorNotification(ctx context.Conte
|
||||
fmt.Printf("[DEBUG] Starting sendDispositionCreatorNotification for letterID: %s\n", letterID.String())
|
||||
fmt.Printf("[DEBUG] Successfully retrieved letter: %s\n", letter.Subject)
|
||||
fmt.Printf("[DEBUG] Successfully retrieved letter: %s\n", letter.CreatedBy)
|
||||
|
||||
|
||||
|
||||
letterCreatorID := letter.CreatedBy
|
||||
|
||||
@@ -924,7 +936,7 @@ func (s *LetterServiceImpl) sendDispositionCreatorNotification(ctx context.Conte
|
||||
dispositionCreatorName := appContext.UserName
|
||||
|
||||
subject := "Disposisi Baru pada Surat Anda"
|
||||
message := fmt.Sprintf("Surat yang Anda buat telah didisposisikan %s: %s",
|
||||
message := fmt.Sprintf("Surat yang Anda buat telah didisposisikan %s: %s",
|
||||
dispositionCreatorName, letter.Subject)
|
||||
|
||||
err = s.notificationProcessor.SendIncomingLetterNotification(
|
||||
@@ -947,19 +959,19 @@ func (s *LetterServiceImpl) sendDispositionStatusUpdateNotification(ctx context.
|
||||
// Log error but don't fail
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
// Get current user context (the one updating the status)
|
||||
appContext := appcontext.FromGinContext(ctx)
|
||||
updaterUserID := appContext.UserID
|
||||
updaterName := appContext.UserName
|
||||
|
||||
|
||||
letterCreatorID := letter.CreatedBy
|
||||
|
||||
|
||||
// Don't send notification if the updater is the same as letter creator
|
||||
if letterCreatorID == updaterUserID {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
// Create status-specific notification message
|
||||
var statusMessage string
|
||||
switch newStatus {
|
||||
@@ -974,20 +986,20 @@ func (s *LetterServiceImpl) sendDispositionStatusUpdateNotification(ctx context.
|
||||
default:
|
||||
statusMessage = fmt.Sprintf("diubah statusnya menjadi %s", newStatus)
|
||||
}
|
||||
|
||||
|
||||
subject := "Status Disposisi Surat Diperbarui"
|
||||
message := fmt.Sprintf("Disposisi surat '%s' %s %s",
|
||||
message := fmt.Sprintf("Disposisi surat '%s' %s %s",
|
||||
letter.Subject, statusMessage, updaterName)
|
||||
|
||||
|
||||
err = s.notificationProcessor.SendIncomingLetterNotification(
|
||||
ctx,
|
||||
letterID,
|
||||
letterCreatorID,
|
||||
subject,
|
||||
message)
|
||||
|
||||
|
||||
if err != nil {
|
||||
// Log error but don't fail the operation
|
||||
// You might want to add proper logging here
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user