fix double revise letter
This commit is contained in:
parent
99cb544a0f
commit
c07d3dfefb
@ -2,6 +2,7 @@ package processor
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"eslogad-be/internal/contract"
|
"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 {
|
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 {
|
return p.txManager.WithTransaction(ctx, func(txCtx context.Context) error {
|
||||||
// Get the current letter
|
|
||||||
letter, err := p.letterRepo.Get(txCtx, letterID)
|
letter, err := p.letterRepo.Get(txCtx, letterID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
fmt.Printf("[ProcessRevision] error get letter: %v\n", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Increment revision number
|
lastRevisionNumber := letter.RevisionNumber
|
||||||
|
|
||||||
|
fmt.Printf("[ProcessRevision] current revision=%d\n", letter.RevisionNumber)
|
||||||
letter.RevisionNumber++
|
letter.RevisionNumber++
|
||||||
|
fmt.Printf("[ProcessRevision] incremented revision=%d\n", letter.RevisionNumber)
|
||||||
|
|
||||||
// Set revision number on the new attachment
|
|
||||||
attachment.RevisionNumber = letter.RevisionNumber
|
attachment.RevisionNumber = letter.RevisionNumber
|
||||||
|
|
||||||
// Add the new attachment
|
|
||||||
if err := p.attachmentRepo.Create(txCtx, &attachment); err != nil {
|
if err := p.attachmentRepo.Create(txCtx, &attachment); err != nil {
|
||||||
|
fmt.Printf("[ProcessRevision] error create attachment: %v\n", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
fmt.Println("[ProcessRevision] attachment created")
|
||||||
|
|
||||||
// Update letter with new revision number
|
|
||||||
if err := p.letterRepo.Update(txCtx, letter); err != nil {
|
if err := p.letterRepo.Update(txCtx, letter); err != nil {
|
||||||
|
fmt.Printf("[ProcessRevision] error update letter: %v\n", err)
|
||||||
return 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 {
|
if err := p.letterRepo.UpdateStatus(txCtx, letterID, entities.LetterOutgoingStatusPendingApproval); err != nil {
|
||||||
|
fmt.Printf("[ProcessRevision] error update status: %v\n", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
fmt.Println("[ProcessRevision] letter status set to PENDING_APPROVAL")
|
||||||
|
|
||||||
// Get existing approvals for the current revision
|
approvals, err := p.approvalRepo.ListByLetterAndLasRevisionNumber(txCtx, letterID, lastRevisionNumber)
|
||||||
approvals, err := p.approvalRepo.ListByLetter(txCtx, letterID)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
fmt.Printf("[ProcessRevision] error list approvals: %v\n", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
fmt.Printf("[ProcessRevision] found %d approvals\n", len(approvals))
|
||||||
|
|
||||||
// Create new approval records for the new revision
|
|
||||||
for _, approval := range approvals {
|
for _, approval := range approvals {
|
||||||
// Create a new approval for the new revision
|
|
||||||
newApproval := entities.LetterOutgoingApproval{
|
newApproval := entities.LetterOutgoingApproval{
|
||||||
LetterID: approval.LetterID,
|
LetterID: approval.LetterID,
|
||||||
StepID: approval.StepID,
|
StepID: approval.StepID,
|
||||||
@ -826,11 +832,12 @@ func (p *LetterOutgoingProcessorImpl) ProcessRevision(ctx context.Context, lette
|
|||||||
ApproverID: approval.ApproverID,
|
ApproverID: approval.ApproverID,
|
||||||
}
|
}
|
||||||
if err := p.approvalRepo.Create(txCtx, &newApproval); err != nil {
|
if err := p.approvalRepo.Create(txCtx, &newApproval); err != nil {
|
||||||
|
fmt.Printf("[ProcessRevision] error create approval: %v\n", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
fmt.Println("[ProcessRevision] approvals cloned for new revision")
|
||||||
|
|
||||||
// Log activity
|
|
||||||
fromStatus := string(entities.LetterOutgoingStatusRejected)
|
fromStatus := string(entities.LetterOutgoingStatusRejected)
|
||||||
toStatus := string(entities.LetterOutgoingStatusPendingApproval)
|
toStatus := string(entities.LetterOutgoingStatusPendingApproval)
|
||||||
activityLog := &entities.LetterOutgoingActivityLog{
|
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},
|
Context: entities.JSONB{"attachment": attachment.FileName, "revision_number": letter.RevisionNumber},
|
||||||
}
|
}
|
||||||
if err := p.activityLogRepo.Create(txCtx, activityLog); err != nil {
|
if err := p.activityLogRepo.Create(txCtx, activityLog); err != nil {
|
||||||
|
fmt.Printf("[ProcessRevision] error create activity log: %v\n", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Printf("[ProcessRevision] success letterID=%s new_revision=%d\n", letterID, letter.RevisionNumber)
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -76,15 +76,15 @@ func (r *ApprovalFlowRepository) List(ctx context.Context, filter ListApprovalFl
|
|||||||
|
|
||||||
// Build base query for counting
|
// Build base query for counting
|
||||||
countQuery := r.db.WithContext(ctx).Model(&entities.ApprovalFlow{})
|
countQuery := r.db.WithContext(ctx).Model(&entities.ApprovalFlow{})
|
||||||
|
|
||||||
if filter.DepartmentID != nil {
|
if filter.DepartmentID != nil {
|
||||||
countQuery = countQuery.Where("department_id = ?", *filter.DepartmentID)
|
countQuery = countQuery.Where("department_id = ?", *filter.DepartmentID)
|
||||||
}
|
}
|
||||||
|
|
||||||
if filter.IsActive != nil {
|
if filter.IsActive != nil {
|
||||||
countQuery = countQuery.Where("is_active = ?", *filter.IsActive)
|
countQuery = countQuery.Where("is_active = ?", *filter.IsActive)
|
||||||
}
|
}
|
||||||
|
|
||||||
if filter.Search != nil && *filter.Search != "" {
|
if filter.Search != nil && *filter.Search != "" {
|
||||||
like := "%" + *filter.Search + "%"
|
like := "%" + *filter.Search + "%"
|
||||||
countQuery = countQuery.Where("name ILIKE ? OR description ILIKE ?", like, like)
|
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
|
// Build query for fetching data - BUAT QUERY BARU DARI AWAL
|
||||||
dataQuery := r.db.WithContext(ctx).Model(&entities.ApprovalFlow{})
|
dataQuery := r.db.WithContext(ctx).Model(&entities.ApprovalFlow{})
|
||||||
|
|
||||||
if filter.DepartmentID != nil {
|
if filter.DepartmentID != nil {
|
||||||
dataQuery = dataQuery.Where("department_id = ?", *filter.DepartmentID)
|
dataQuery = dataQuery.Where("department_id = ?", *filter.DepartmentID)
|
||||||
}
|
}
|
||||||
|
|
||||||
if filter.IsActive != nil {
|
if filter.IsActive != nil {
|
||||||
dataQuery = dataQuery.Where("is_active = ?", *filter.IsActive)
|
dataQuery = dataQuery.Where("is_active = ?", *filter.IsActive)
|
||||||
}
|
}
|
||||||
|
|
||||||
if filter.Search != nil && *filter.Search != "" {
|
if filter.Search != nil && *filter.Search != "" {
|
||||||
like := "%" + *filter.Search + "%"
|
like := "%" + *filter.Search + "%"
|
||||||
dataQuery = dataQuery.Where("name ILIKE ? OR description ILIKE ?", like, like)
|
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
|
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) {
|
func (r *LetterOutgoingApprovalRepository) GetPendingApprovals(ctx context.Context, userID uuid.UUID) ([]entities.LetterOutgoingApproval, error) {
|
||||||
db := DBFromContext(ctx, r.db)
|
db := DBFromContext(ctx, r.db)
|
||||||
var list []entities.LetterOutgoingApproval
|
var list []entities.LetterOutgoingApproval
|
||||||
|
|
||||||
if err := db.WithContext(ctx).
|
if err := db.WithContext(ctx).
|
||||||
Preload("Letter").
|
Preload("Letter").
|
||||||
Preload("Step").
|
Preload("Step").
|
||||||
Joins("JOIN approval_flow_steps afs ON afs.id = letter_outgoing_approvals.step_id").
|
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).
|
entities.ApprovalStatusPending, userID, userID).
|
||||||
Find(&list).Error; err != nil {
|
Find(&list).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return list, nil
|
return list, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -44,7 +44,7 @@ CREATE TABLE IF NOT EXISTS letter_incoming_recipients (
|
|||||||
read_at TIMESTAMP WITHOUT TIME ZONE,
|
read_at TIMESTAMP WITHOUT TIME ZONE,
|
||||||
completed_at TIMESTAMP WITHOUT TIME ZONE,
|
completed_at TIMESTAMP WITHOUT TIME ZONE,
|
||||||
created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT CURRENT_TIMESTAMP
|
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);
|
CREATE INDEX IF NOT EXISTS idx_letter_incoming_recipients_letter ON letter_incoming_recipients(letter_id);
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user