diff --git a/app/module/article_approval_flows/repository/article_approval_flows.repository.go b/app/module/article_approval_flows/repository/article_approval_flows.repository.go index 943a084..fa3e24d 100644 --- a/app/module/article_approval_flows/repository/article_approval_flows.repository.go +++ b/app/module/article_approval_flows/repository/article_approval_flows.repository.go @@ -2,13 +2,14 @@ package repository import ( "fmt" - "github.com/google/uuid" - "github.com/rs/zerolog" "time" "web-medols-be/app/database" "web-medols-be/app/database/entity" "web-medols-be/app/module/article_approval_flows/request" "web-medols-be/utils/paginator" + + "github.com/google/uuid" + "github.com/rs/zerolog" ) type articleApprovalFlowsRepository struct { @@ -184,6 +185,13 @@ func (_i *articleApprovalFlowsRepository) GetPendingApprovals(clientId *uuid.UUI query = query.Joins("JOIN approval_workflow_steps ON approval_workflows.id = approval_workflow_steps.workflow_id AND article_approval_flows.current_step = approval_workflow_steps.step_order") query = query.Where("approval_workflow_steps.required_user_level_id = ?", userLevelId) query = query.Where("article_approval_flows.status_id IN (1, 4)") // pending or revision_requested + query = query.Where("article_approval_flows.current_step > 0") // exclude completed flows (current_step = 0) + + // Debug logging + _i.Log.Info(). + Interface("clientId", clientId). + Uint("userLevelId", userLevelId). + Msg("Getting pending approvals for user level") // Apply filters if categoryId, ok := filters["category_id"]; ok { @@ -204,12 +212,22 @@ func (_i *articleApprovalFlowsRepository) GetPendingApprovals(clientId *uuid.UUI return nil, paginator.Pagination{}, err } + // Debug logging + _i.Log.Info(). + Int64("count", count). + Msg("Found pending approvals count") + offset := (page - 1) * limit err = query.Offset(offset).Limit(limit).Find(&flows).Error if err != nil { return nil, paginator.Pagination{}, err } + // Debug logging + _i.Log.Info(). + Int("flowsCount", len(flows)). + Msg("Retrieved flows from database") + paging = paginator.Pagination{ Page: page, Limit: limit, @@ -257,7 +275,7 @@ func (_i *articleApprovalFlowsRepository) GetOverdueCountByLevel(clientId *uuid. query = query.Joins("JOIN approval_workflows ON article_approval_flows.workflow_id = approval_workflows.id") query = query.Joins("JOIN approval_workflow_steps ON approval_workflows.id = approval_workflow_steps.workflow_id AND article_approval_flows.current_step = approval_workflow_steps.step_order") query = query.Where("approval_workflow_steps.required_user_level_id = ?", userLevelId) - query = query.Where("article_approval_flows.status_id IN (1, 4)") // pending or revision_requested + query = query.Where("article_approval_flows.status_id IN (1, 4)") // pending or revision_requested query = query.Where("article_approval_flows.submitted_at < ?", time.Now().Add(-24*time.Hour)) // overdue after 24 hours err = query.Count(&count).Error @@ -336,15 +354,15 @@ func (_i *articleApprovalFlowsRepository) GetWorkflowBottlenecks(clientId *uuid. // FindByArticleId finds an approval flow by article ID for a specific client func (_i *articleApprovalFlowsRepository) FindByArticleId(clientId *uuid.UUID, articleId uint) (flow *entity.ArticleApprovalFlows, err error) { query := _i.DB.DB.Model(&entity.ArticleApprovalFlows{}) - + if clientId != nil { query = query.Where("client_id = ?", clientId) } - + query = query.Where("article_id = ?", articleId) query = query.Where("status_id IN (1, 2, 3, 4)") // active flows (pending, approved, rejected, revision_requested) query = query.Order("created_at DESC") - + err = query.First(&flow).Error return flow, err } @@ -352,38 +370,39 @@ func (_i *articleApprovalFlowsRepository) FindByArticleId(clientId *uuid.UUID, a // GetPendingApprovalsByUserLevel gets pending approvals for a specific user level with pagination func (_i *articleApprovalFlowsRepository) GetPendingApprovalsByUserLevel(clientId *uuid.UUID, userLevelId uint, page, limit int) (flows []*entity.ArticleApprovalFlows, paging paginator.Pagination, err error) { var count int64 - + query := _i.DB.DB.Model(&entity.ArticleApprovalFlows{}) - + if clientId != nil { query = query.Where("article_approval_flows.client_id = ?", clientId) } - + // Join with workflow steps to get approvals for this user level query = query.Joins("JOIN approval_workflows ON article_approval_flows.workflow_id = approval_workflows.id") query = query.Joins("JOIN approval_workflow_steps ON approval_workflows.id = approval_workflow_steps.workflow_id AND article_approval_flows.current_step = approval_workflow_steps.step_order") query = query.Where("approval_workflow_steps.required_user_level_id = ?", userLevelId) query = query.Where("article_approval_flows.status_id IN (1, 4)") // pending or revision_requested - + query = query.Where("article_approval_flows.current_step > 0") // exclude completed flows (current_step = 0) + // Preload related data query = query.Preload("Article").Preload("Workflow") - + // Count total err = query.Count(&count).Error if err != nil { return nil, paginator.Pagination{}, err } - + // Apply pagination offset := (page - 1) * limit query = query.Offset(offset).Limit(limit) query = query.Order("article_approval_flows.created_at ASC") // oldest first - + err = query.Find(&flows).Error if err != nil { return nil, paginator.Pagination{}, err } - + // Create pagination response paging = paginator.Pagination{ Page: page, @@ -391,6 +410,6 @@ func (_i *articleApprovalFlowsRepository) GetPendingApprovalsByUserLevel(clientI Count: count, Offset: offset, } - + return flows, paging, nil -} \ No newline at end of file +} diff --git a/app/module/article_approval_flows/service/article_approval_flows.service.go b/app/module/article_approval_flows/service/article_approval_flows.service.go index 04ef22d..02fde27 100644 --- a/app/module/article_approval_flows/service/article_approval_flows.service.go +++ b/app/module/article_approval_flows/service/article_approval_flows.service.go @@ -257,7 +257,8 @@ func (_i *articleApprovalFlowsService) processAutoSkipSteps(clientId *uuid.UUID, if flow.CurrentStep > maxStepOrder { // All steps completed, mark as approved - flow.StatusId = 2 // approved + flow.StatusId = 2 // approved + flow.CurrentStep = 0 // Set to 0 to indicate completion flow.CompletedAt = &[]time.Time{time.Now()}[0] err = _i.ArticleApprovalFlowsRepository.Update(flow.ID, flow) @@ -272,8 +273,8 @@ func (_i *articleApprovalFlowsService) processAutoSkipSteps(clientId *uuid.UUID, } // Update only the necessary fields - currentArticle.StatusId = &[]int{2}[0] // approved - currentArticle.CurrentApprovalStep = nil + currentArticle.StatusId = &[]int{2}[0] // approved + currentArticle.CurrentApprovalStep = &[]int{0}[0] // Set to 0 to indicate completion err = _i.ArticlesRepository.UpdateSkipNull(clientId, flow.ArticleId, currentArticle) if err != nil { @@ -380,13 +381,19 @@ func (_i *articleApprovalFlowsService) ApproveStep(clientId *uuid.UUID, flowId u return err } - if nextStep == nil { + if nextStep == nil || nextStep.ID == 0 { // No next step - approval complete flowUpdate := &entity.ArticleApprovalFlows{ StatusId: 2, // approved + CurrentStep: 0, // Set to 0 to indicate completion CompletedAt: &[]time.Time{time.Now()}[0], } + // Debug logging + _i.Log.Info(). + Interface("flowUpdate :: ", flowUpdate). + Msg("Retrieved next step from database") + err = _i.ArticleApprovalFlowsRepository.Update(flowId, flowUpdate) if err != nil { return err @@ -399,8 +406,10 @@ func (_i *articleApprovalFlowsService) ApproveStep(clientId *uuid.UUID, flowId u } // Update only the necessary fields - currentArticle.StatusId = &[]int{2}[0] // approved - currentArticle.CurrentApprovalStep = nil + currentArticle.StatusId = &[]int{2}[0] // approved + currentArticle.CurrentApprovalStep = &[]int{0}[0] // Set to 0 to indicate completion + currentArticle.IsPublish = &[]bool{true}[0] // Set to true to indicate publication + currentArticle.IsDraft = &[]bool{false}[0] // Set to false to indicate publication err = _i.ArticlesRepository.UpdateSkipNull(clientId, flow.ArticleId, currentArticle) if err != nil {