update get approval by letter
This commit is contained in:
parent
58372ca195
commit
da2246d45a
@ -294,6 +294,7 @@ type EnhancedOutgoingLetterApprovalResponse struct {
|
||||
ParallelGroup int `json:"parallel_group"`
|
||||
IsRequired bool `json:"is_required"`
|
||||
ApproverID *uuid.UUID `json:"approver_id,omitempty"`
|
||||
RevisionNumber int `json:"revision_number"`
|
||||
Status string `json:"status"`
|
||||
Remarks *string `json:"remarks,omitempty"`
|
||||
ActedAt *time.Time `json:"acted_at,omitempty"`
|
||||
@ -302,6 +303,11 @@ type EnhancedOutgoingLetterApprovalResponse struct {
|
||||
Approver *UserResponse `json:"approver,omitempty"`
|
||||
}
|
||||
|
||||
type OutgoingLetterApprovalRevisionNumberResponse struct {
|
||||
RevisionNumber int `json:"revision_number"`
|
||||
Approvals []EnhancedOutgoingLetterApprovalResponse `json:"approvals"`
|
||||
}
|
||||
|
||||
// GetLetterApprovalsResponse represents the list of approvals for a letter
|
||||
type GetLetterApprovalsResponse struct {
|
||||
LetterID uuid.UUID `json:"letter_id"`
|
||||
@ -309,7 +315,8 @@ type GetLetterApprovalsResponse struct {
|
||||
LetterStatus string `json:"letter_status"`
|
||||
TotalSteps int `json:"total_steps"`
|
||||
CurrentStep int `json:"current_step"`
|
||||
Approvals []EnhancedOutgoingLetterApprovalResponse `json:"approvals"`
|
||||
CurrentRevisionNumber int `json:"current_revision_number"`
|
||||
Approvals []OutgoingLetterApprovalRevisionNumberResponse `json:"approvals"`
|
||||
}
|
||||
|
||||
// OutgoingLetterDiscussionResponse represents a discussion on an outgoing letter
|
||||
|
||||
@ -41,6 +41,7 @@ type LetterOutgoingProcessor interface {
|
||||
DeleteDiscussion(ctx context.Context, id uuid.UUID) error
|
||||
|
||||
GetApprovalsByLetter(ctx context.Context, letterID uuid.UUID) ([]entities.LetterOutgoingApproval, error)
|
||||
GetAllApprovalsByLetter(ctx context.Context, letterID uuid.UUID) ([]entities.LetterOutgoingApproval, error)
|
||||
GetApprovalsByLetterAndRevision(ctx context.Context, letterID uuid.UUID, revisionNumber int) ([]entities.LetterOutgoingApproval, error)
|
||||
GetApprovalFlow(ctx context.Context, flowID uuid.UUID) (*entities.ApprovalFlow, error)
|
||||
|
||||
@ -989,6 +990,15 @@ func (p *LetterOutgoingProcessorImpl) GetApprovalsByLetter(ctx context.Context,
|
||||
return p.GetApprovalsByLetterAndRevision(ctx, letterID, letter.RevisionNumber)
|
||||
}
|
||||
|
||||
func (p *LetterOutgoingProcessorImpl) GetAllApprovalsByLetter(ctx context.Context, letterID uuid.UUID) ([]entities.LetterOutgoingApproval, error) {
|
||||
approvals, err := p.approvalRepo.ListByLetter(ctx, letterID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return approvals, nil
|
||||
}
|
||||
|
||||
func (p *LetterOutgoingProcessorImpl) GetApprovalsByLetterAndRevision(ctx context.Context, letterID uuid.UUID, revisionNumber int) ([]entities.LetterOutgoingApproval, error) {
|
||||
// Get all approvals for this letter
|
||||
approvals, err := p.approvalRepo.ListByLetter(ctx, letterID)
|
||||
|
||||
@ -1112,38 +1112,78 @@ func (s *LetterOutgoingServiceImpl) GetLetterApprovals(ctx context.Context, lett
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Get all approvals for this letter's current revision
|
||||
approvals, err := s.processor.GetApprovalsByLetterAndRevision(ctx, letterID, letter.RevisionNumber)
|
||||
// Get all approvals for this letter (all revisions)
|
||||
approvals, err := s.processor.GetAllApprovalsByLetter(ctx, letterID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Sort approvals by step order and parallel group
|
||||
sort.Slice(approvals, func(i, j int) bool {
|
||||
if approvals[i].StepOrder != approvals[j].StepOrder {
|
||||
return approvals[i].StepOrder < approvals[j].StepOrder
|
||||
// Group approvals by revision number from approval itself
|
||||
revisionMap := make(map[int][]entities.LetterOutgoingApproval)
|
||||
for _, approval := range approvals {
|
||||
revisionMap[approval.RevisionNumber] = append(revisionMap[approval.RevisionNumber], approval)
|
||||
}
|
||||
return approvals[i].ParallelGroup < approvals[j].ParallelGroup
|
||||
|
||||
// Get sorted revision numbers
|
||||
revisionNumbers := make([]int, 0, len(revisionMap))
|
||||
for revNum := range revisionMap {
|
||||
revisionNumbers = append(revisionNumbers, revNum)
|
||||
}
|
||||
sort.Sort(sort.Reverse(sort.IntSlice(revisionNumbers)))
|
||||
|
||||
// Process each revision
|
||||
revisionResponses := make([]contract.OutgoingLetterApprovalRevisionNumberResponse, 0, len(revisionNumbers))
|
||||
|
||||
totalSteps := 0
|
||||
currentStep := 0
|
||||
|
||||
for _, revNum := range revisionNumbers {
|
||||
revisionApprovals := revisionMap[revNum]
|
||||
|
||||
// Sort approvals within this revision by step order and parallel group
|
||||
sort.Slice(revisionApprovals, func(i, j int) bool {
|
||||
if revisionApprovals[i].StepOrder != revisionApprovals[j].StepOrder {
|
||||
return revisionApprovals[i].StepOrder < revisionApprovals[j].StepOrder
|
||||
}
|
||||
return revisionApprovals[i].ParallelGroup < revisionApprovals[j].ParallelGroup
|
||||
})
|
||||
|
||||
// Transform to response format
|
||||
approvalResponses := make([]contract.EnhancedOutgoingLetterApprovalResponse, 0, len(approvals))
|
||||
totalSteps := 0
|
||||
currentStep := 0
|
||||
approvalResponses := make([]contract.EnhancedOutgoingLetterApprovalResponse, 0, len(revisionApprovals))
|
||||
|
||||
// Only calculate totalSteps and currentStep for the current letter's revision
|
||||
if revNum == letter.RevisionNumber {
|
||||
stepOrdersSeen := make(map[int]bool)
|
||||
|
||||
for _, approval := range approvals {
|
||||
for _, approval := range revisionApprovals {
|
||||
// Count unique step orders for total steps
|
||||
if !stepOrdersSeen[approval.StepOrder] {
|
||||
stepOrdersSeen[approval.StepOrder] = true
|
||||
totalSteps++
|
||||
}
|
||||
|
||||
// Determine current step (lowest step with pending/not_started status)
|
||||
// Determine current step (lowest step with pending status)
|
||||
if approval.Status == entities.ApprovalStatusPending && (currentStep == 0 || approval.StepOrder < currentStep) {
|
||||
currentStep = approval.StepOrder
|
||||
}
|
||||
}
|
||||
|
||||
// If no current step found but there are approvals, check if all are completed
|
||||
if currentStep == 0 && len(revisionApprovals) > 0 {
|
||||
allCompleted := true
|
||||
for _, approval := range revisionApprovals {
|
||||
if approval.IsRequired && approval.Status != entities.ApprovalStatusApproved {
|
||||
allCompleted = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if allCompleted {
|
||||
currentStep = totalSteps // All steps completed
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, approval := range revisionApprovals {
|
||||
approvalResp := contract.EnhancedOutgoingLetterApprovalResponse{
|
||||
ID: approval.ID,
|
||||
LetterID: approval.LetterID,
|
||||
@ -1152,6 +1192,7 @@ func (s *LetterOutgoingServiceImpl) GetLetterApprovals(ctx context.Context, lett
|
||||
ParallelGroup: approval.ParallelGroup,
|
||||
IsRequired: approval.IsRequired,
|
||||
ApproverID: approval.ApproverID,
|
||||
RevisionNumber: approval.RevisionNumber,
|
||||
Status: string(approval.Status),
|
||||
Remarks: approval.Remarks,
|
||||
ActedAt: approval.ActedAt,
|
||||
@ -1200,18 +1241,11 @@ func (s *LetterOutgoingServiceImpl) GetLetterApprovals(ctx context.Context, lett
|
||||
approvalResponses = append(approvalResponses, approvalResp)
|
||||
}
|
||||
|
||||
// If no current step found but there are approvals, check if all are completed
|
||||
if currentStep == 0 && len(approvals) > 0 {
|
||||
allCompleted := true
|
||||
for _, approval := range approvals {
|
||||
if approval.IsRequired && approval.Status != entities.ApprovalStatusApproved {
|
||||
allCompleted = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if allCompleted {
|
||||
currentStep = totalSteps // All steps completed
|
||||
}
|
||||
// Add revision response
|
||||
revisionResponses = append(revisionResponses, contract.OutgoingLetterApprovalRevisionNumberResponse{
|
||||
RevisionNumber: revNum,
|
||||
Approvals: approvalResponses,
|
||||
})
|
||||
}
|
||||
|
||||
response := &contract.GetLetterApprovalsResponse{
|
||||
@ -1220,12 +1254,12 @@ func (s *LetterOutgoingServiceImpl) GetLetterApprovals(ctx context.Context, lett
|
||||
LetterStatus: string(letter.Status),
|
||||
TotalSteps: totalSteps,
|
||||
CurrentStep: currentStep,
|
||||
Approvals: approvalResponses,
|
||||
CurrentRevisionNumber: letter.RevisionNumber,
|
||||
Approvals: revisionResponses,
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func getUserIDFromContext(ctx context.Context) uuid.UUID {
|
||||
appCtx := appcontext.FromGinContext(ctx)
|
||||
if appCtx != nil {
|
||||
@ -1264,6 +1298,7 @@ func (s *LetterOutgoingServiceImpl) GetApprovalDiscussions(ctx context.Context,
|
||||
ParallelGroup: approval.ParallelGroup,
|
||||
IsRequired: approval.IsRequired,
|
||||
ApproverID: approval.ApproverID,
|
||||
RevisionNumber: approval.RevisionNumber,
|
||||
Status: string(approval.Status),
|
||||
Remarks: approval.Remarks,
|
||||
ActedAt: approval.ActedAt,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user