update unique letter number

This commit is contained in:
efrilm
2025-10-20 22:50:08 +07:00
parent 0c3e1db502
commit 88ad35c5d0
5 changed files with 76 additions and 25 deletions
+18 -12
View File
@@ -94,6 +94,12 @@ func (s *LetterOutgoingServiceImpl) CreateOutgoingLetter(ctx context.Context, re
departmentID := getDepartmentIDFromContext(ctx)
userID := getUserIDFromContext(ctx)
existingOutgoing, err := s.processor.GetOutgoingLetterByReferenceNumber(ctx, req.ReferenceNumber)
if err == nil && existingOutgoing != nil {
return nil, fmt.Errorf("surat dengan nomor %s sudah ada", *req.ReferenceNumber)
}
// Create letter entity
letter := &entities.LetterOutgoing{
Subject: req.Subject,
@@ -125,7 +131,7 @@ func (s *LetterOutgoingServiceImpl) CreateOutgoingLetter(ctx context.Context, re
}
// Execute creation with transaction in service layer
err := s.txManager.WithTransaction(ctx, func(txCtx context.Context) error {
err = s.txManager.WithTransaction(ctx, func(txCtx context.Context) error {
// Step 1: Validate letter
if err := s.validationProcessor.ValidateCreateOutgoingLetter(txCtx, letter); err != nil {
return err
@@ -660,7 +666,7 @@ func (s *LetterOutgoingServiceImpl) ApproveOutgoingLetter(ctx context.Context, l
if s.notificationProcessor != nil {
// Get next parallel group to determine notification message
nextParallelGroup := s.getNextParallelGroup(approvals, currentApproval.ParallelGroup)
if nextParallelGroup > 0 {
// Notify creator about group completion AND next group approvers
creatorMessage := fmt.Sprintf("Surat keluar '%s' telah disetujui pada grup %d, menunggu persetujuan grup berikutnya", letter.Subject, currentApproval.ParallelGroup)
@@ -1896,14 +1902,14 @@ func (s *LetterOutgoingServiceImpl) getNextParallelGroup(approvals []entities.Le
for _, approval := range approvals {
groupsMap[approval.ParallelGroup] = true
}
// Convert to sorted slice
var groups []int
for group := range groupsMap {
groups = append(groups, group)
}
sort.Ints(groups)
// Find the next group after current
for _, group := range groups {
if group > currentGroup {
@@ -1915,45 +1921,45 @@ func (s *LetterOutgoingServiceImpl) getNextParallelGroup(approvals []entities.Le
}
}
}
return 0 // No next group
}
// Send notifications to approvers in a specific parallel group
func (s *LetterOutgoingServiceImpl) sendParallelGroupApprovalNotifications(ctx context.Context, letterID uuid.UUID, subject string, parallelGroup int) {
log.Printf("[DEBUG] sendParallelGroupApprovalNotifications START - LetterID: %s, ParallelGroup: %d", letterID.String(), parallelGroup)
// Get the letter to know the current revision
letter, err := s.processor.GetOutgoingLetterByID(ctx, letterID)
if err != nil {
log.Printf("[ERROR] Failed to get letter: %v", err)
return
}
// Get approvals for the current revision only
approvals, err := s.processor.GetApprovalsByLetterAndRevision(ctx, letterID, letter.RevisionNumber)
if err != nil {
log.Printf("[ERROR] Failed to get approvals: %v", err)
return
}
log.Printf("[DEBUG] Found %d approvals", len(approvals))
// Find approvers for the specified parallel group
for _, approval := range approvals {
log.Printf("[DEBUG] Checking approval: ParallelGroup=%d, Status=%s, ApproverID=%v",
approval.ParallelGroup, approval.Status, approval.ApproverID)
if approval.ParallelGroup == parallelGroup && approval.ApproverID != nil {
log.Printf("[DEBUG] Sending notification to approver %s for parallel group %d", approval.ApproverID.String(), parallelGroup)
err := s.notificationProcessor.SendOutgoingLetterNotification(
ctx,
letterID,
*approval.ApproverID,
"Surat Keluar Perlu Persetujuan",
fmt.Sprintf("Surat keluar '%s' memerlukan persetujuan Anda pada grup %d", subject, parallelGroup))
if err != nil {
log.Printf("[ERROR] Failed to send notification to approver %s: %v", approval.ApproverID.String(), err)
} else {