fix double revise letter

This commit is contained in:
efrilm
2025-10-18 18:54:46 +07:00
parent 99cb544a0f
commit c07d3dfefb
3 changed files with 46 additions and 22 deletions
+21 -12
View File
@@ -2,6 +2,7 @@ package processor
import (
"context"
"fmt"
"time"
"eslogad-be/internal/contract"
@@ -778,43 +779,48 @@ func (p *LetterOutgoingProcessorImpl) ProcessRejection(ctx context.Context, lett
}
func (p *LetterOutgoingProcessorImpl) ProcessRevision(ctx context.Context, letterID uuid.UUID, attachment entities.LetterOutgoingAttachment, userID uuid.UUID) error {
fmt.Printf("[ProcessRevision] start letterID=%s userID=%s attachment=%s\n", letterID, userID, attachment.FileName)
return p.txManager.WithTransaction(ctx, func(txCtx context.Context) error {
// Get the current letter
letter, err := p.letterRepo.Get(txCtx, letterID)
if err != nil {
fmt.Printf("[ProcessRevision] error get letter: %v\n", err)
return err
}
// Increment revision number
lastRevisionNumber := letter.RevisionNumber
fmt.Printf("[ProcessRevision] current revision=%d\n", letter.RevisionNumber)
letter.RevisionNumber++
fmt.Printf("[ProcessRevision] incremented revision=%d\n", letter.RevisionNumber)
// Set revision number on the new attachment
attachment.RevisionNumber = letter.RevisionNumber
// Add the new attachment
if err := p.attachmentRepo.Create(txCtx, &attachment); err != nil {
fmt.Printf("[ProcessRevision] error create attachment: %v\n", err)
return err
}
fmt.Println("[ProcessRevision] attachment created")
// Update letter with new revision number
if err := p.letterRepo.Update(txCtx, letter); err != nil {
fmt.Printf("[ProcessRevision] error update letter: %v\n", err)
return err
}
fmt.Println("[ProcessRevision] letter updated")
// Update status to pending approval (ready for re-submission)
if err := p.letterRepo.UpdateStatus(txCtx, letterID, entities.LetterOutgoingStatusPendingApproval); err != nil {
fmt.Printf("[ProcessRevision] error update status: %v\n", err)
return err
}
fmt.Println("[ProcessRevision] letter status set to PENDING_APPROVAL")
// Get existing approvals for the current revision
approvals, err := p.approvalRepo.ListByLetter(txCtx, letterID)
approvals, err := p.approvalRepo.ListByLetterAndLasRevisionNumber(txCtx, letterID, lastRevisionNumber)
if err != nil {
fmt.Printf("[ProcessRevision] error list approvals: %v\n", err)
return err
}
fmt.Printf("[ProcessRevision] found %d approvals\n", len(approvals))
// Create new approval records for the new revision
for _, approval := range approvals {
// Create a new approval for the new revision
newApproval := entities.LetterOutgoingApproval{
LetterID: approval.LetterID,
StepID: approval.StepID,
@@ -826,11 +832,12 @@ func (p *LetterOutgoingProcessorImpl) ProcessRevision(ctx context.Context, lette
ApproverID: approval.ApproverID,
}
if err := p.approvalRepo.Create(txCtx, &newApproval); err != nil {
fmt.Printf("[ProcessRevision] error create approval: %v\n", err)
return err
}
}
fmt.Println("[ProcessRevision] approvals cloned for new revision")
// Log activity
fromStatus := string(entities.LetterOutgoingStatusRejected)
toStatus := string(entities.LetterOutgoingStatusPendingApproval)
activityLog := &entities.LetterOutgoingActivityLog{
@@ -843,9 +850,11 @@ func (p *LetterOutgoingProcessorImpl) ProcessRevision(ctx context.Context, lette
Context: entities.JSONB{"attachment": attachment.FileName, "revision_number": letter.RevisionNumber},
}
if err := p.activityLogRepo.Create(txCtx, activityLog); err != nil {
fmt.Printf("[ProcessRevision] error create activity log: %v\n", err)
return err
}
fmt.Printf("[ProcessRevision] success letterID=%s new_revision=%d\n", letterID, letter.RevisionNumber)
return nil
})
}