diff --git a/internal/processor/letter_outgoing_processor.go b/internal/processor/letter_outgoing_processor.go index f0320f2..1104869 100644 --- a/internal/processor/letter_outgoing_processor.go +++ b/internal/processor/letter_outgoing_processor.go @@ -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 }) } diff --git a/internal/repository/approval_flow_repository.go b/internal/repository/approval_flow_repository.go index 73b6cab..b4dd568 100644 --- a/internal/repository/approval_flow_repository.go +++ b/internal/repository/approval_flow_repository.go @@ -76,15 +76,15 @@ func (r *ApprovalFlowRepository) List(ctx context.Context, filter ListApprovalFl // Build base query for counting countQuery := r.db.WithContext(ctx).Model(&entities.ApprovalFlow{}) - + if filter.DepartmentID != nil { countQuery = countQuery.Where("department_id = ?", *filter.DepartmentID) } - + if filter.IsActive != nil { countQuery = countQuery.Where("is_active = ?", *filter.IsActive) } - + if filter.Search != nil && *filter.Search != "" { like := "%" + *filter.Search + "%" countQuery = countQuery.Where("name ILIKE ? OR description ILIKE ?", like, like) @@ -97,15 +97,15 @@ func (r *ApprovalFlowRepository) List(ctx context.Context, filter ListApprovalFl // Build query for fetching data - BUAT QUERY BARU DARI AWAL dataQuery := r.db.WithContext(ctx).Model(&entities.ApprovalFlow{}) - + if filter.DepartmentID != nil { dataQuery = dataQuery.Where("department_id = ?", *filter.DepartmentID) } - + if filter.IsActive != nil { dataQuery = dataQuery.Where("is_active = ?", *filter.IsActive) } - + if filter.Search != nil && *filter.Search != "" { like := "%" + *filter.Search + "%" dataQuery = dataQuery.Where("name ILIKE ? OR description ILIKE ?", like, like) @@ -239,18 +239,33 @@ func (r *LetterOutgoingApprovalRepository) ListByLetter(ctx context.Context, let return list, nil } +func (r *LetterOutgoingApprovalRepository) ListByLetterAndLasRevisionNumber(ctx context.Context, letterID uuid.UUID, revisionNumber int) ([]entities.LetterOutgoingApproval, error) { + db := DBFromContext(ctx, r.db) + var list []entities.LetterOutgoingApproval + if err := db.WithContext(ctx). + Preload("Step.ApproverRole"). + Preload("Step.ApproverUser"). + Preload("Approver"). + Where("letter_id = ? AND revision_number = ?", letterID, revisionNumber). + Order("created_at ASC"). + Find(&list).Error; err != nil { + return nil, err + } + return list, nil +} + func (r *LetterOutgoingApprovalRepository) GetPendingApprovals(ctx context.Context, userID uuid.UUID) ([]entities.LetterOutgoingApproval, error) { db := DBFromContext(ctx, r.db) var list []entities.LetterOutgoingApproval - + if err := db.WithContext(ctx). Preload("Letter"). Preload("Step"). Joins("JOIN approval_flow_steps afs ON afs.id = letter_outgoing_approvals.step_id"). - Where("letter_outgoing_approvals.status = ? AND (afs.approver_user_id = ? OR afs.approver_role_id IN (SELECT role_id FROM user_roles WHERE user_id = ?))", + Where("letter_outgoing_approvals.status = ? AND (afs.approver_user_id = ? OR afs.approver_role_id IN (SELECT role_id FROM user_roles WHERE user_id = ?))", entities.ApprovalStatusPending, userID, userID). Find(&list).Error; err != nil { return nil, err } return list, nil -} \ No newline at end of file +} diff --git a/migrations/000008_letters_incoming_suite.up.sql b/migrations/000008_letters_incoming_suite.up.sql index 6d46bfb..13eed90 100644 --- a/migrations/000008_letters_incoming_suite.up.sql +++ b/migrations/000008_letters_incoming_suite.up.sql @@ -44,7 +44,7 @@ CREATE TABLE IF NOT EXISTS letter_incoming_recipients ( read_at TIMESTAMP WITHOUT TIME ZONE, completed_at TIMESTAMP WITHOUT TIME ZONE, created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT CURRENT_TIMESTAMP -); +);I CREATE INDEX IF NOT EXISTS idx_letter_incoming_recipients_letter ON letter_incoming_recipients(letter_id);