feat: fixing article approval flows

This commit is contained in:
hanif salafi 2025-09-17 00:29:22 +07:00
parent 8e9fe2559b
commit a4c83f8f0c
2 changed files with 50 additions and 22 deletions

View File

@ -2,13 +2,14 @@ package repository
import ( import (
"fmt" "fmt"
"github.com/google/uuid"
"github.com/rs/zerolog"
"time" "time"
"web-medols-be/app/database" "web-medols-be/app/database"
"web-medols-be/app/database/entity" "web-medols-be/app/database/entity"
"web-medols-be/app/module/article_approval_flows/request" "web-medols-be/app/module/article_approval_flows/request"
"web-medols-be/utils/paginator" "web-medols-be/utils/paginator"
"github.com/google/uuid"
"github.com/rs/zerolog"
) )
type articleApprovalFlowsRepository struct { 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.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("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.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 // Apply filters
if categoryId, ok := filters["category_id"]; ok { if categoryId, ok := filters["category_id"]; ok {
@ -204,12 +212,22 @@ func (_i *articleApprovalFlowsRepository) GetPendingApprovals(clientId *uuid.UUI
return nil, paginator.Pagination{}, err return nil, paginator.Pagination{}, err
} }
// Debug logging
_i.Log.Info().
Int64("count", count).
Msg("Found pending approvals count")
offset := (page - 1) * limit offset := (page - 1) * limit
err = query.Offset(offset).Limit(limit).Find(&flows).Error err = query.Offset(offset).Limit(limit).Find(&flows).Error
if err != nil { if err != nil {
return nil, paginator.Pagination{}, err return nil, paginator.Pagination{}, err
} }
// Debug logging
_i.Log.Info().
Int("flowsCount", len(flows)).
Msg("Retrieved flows from database")
paging = paginator.Pagination{ paging = paginator.Pagination{
Page: page, Page: page,
Limit: limit, 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_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.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("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 query = query.Where("article_approval_flows.submitted_at < ?", time.Now().Add(-24*time.Hour)) // overdue after 24 hours
err = query.Count(&count).Error err = query.Count(&count).Error
@ -364,6 +382,7 @@ func (_i *articleApprovalFlowsRepository) GetPendingApprovalsByUserLevel(clientI
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.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("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.current_step > 0") // exclude completed flows (current_step = 0)
// Preload related data // Preload related data
query = query.Preload("Article").Preload("Workflow") query = query.Preload("Article").Preload("Workflow")

View File

@ -257,7 +257,8 @@ func (_i *articleApprovalFlowsService) processAutoSkipSteps(clientId *uuid.UUID,
if flow.CurrentStep > maxStepOrder { if flow.CurrentStep > maxStepOrder {
// All steps completed, mark as approved // 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] flow.CompletedAt = &[]time.Time{time.Now()}[0]
err = _i.ArticleApprovalFlowsRepository.Update(flow.ID, flow) err = _i.ArticleApprovalFlowsRepository.Update(flow.ID, flow)
@ -272,8 +273,8 @@ func (_i *articleApprovalFlowsService) processAutoSkipSteps(clientId *uuid.UUID,
} }
// Update only the necessary fields // Update only the necessary fields
currentArticle.StatusId = &[]int{2}[0] // approved currentArticle.StatusId = &[]int{2}[0] // approved
currentArticle.CurrentApprovalStep = nil currentArticle.CurrentApprovalStep = &[]int{0}[0] // Set to 0 to indicate completion
err = _i.ArticlesRepository.UpdateSkipNull(clientId, flow.ArticleId, currentArticle) err = _i.ArticlesRepository.UpdateSkipNull(clientId, flow.ArticleId, currentArticle)
if err != nil { if err != nil {
@ -380,13 +381,19 @@ func (_i *articleApprovalFlowsService) ApproveStep(clientId *uuid.UUID, flowId u
return err return err
} }
if nextStep == nil { if nextStep == nil || nextStep.ID == 0 {
// No next step - approval complete // No next step - approval complete
flowUpdate := &entity.ArticleApprovalFlows{ flowUpdate := &entity.ArticleApprovalFlows{
StatusId: 2, // approved StatusId: 2, // approved
CurrentStep: 0, // Set to 0 to indicate completion
CompletedAt: &[]time.Time{time.Now()}[0], 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) err = _i.ArticleApprovalFlowsRepository.Update(flowId, flowUpdate)
if err != nil { if err != nil {
return err return err
@ -399,8 +406,10 @@ func (_i *articleApprovalFlowsService) ApproveStep(clientId *uuid.UUID, flowId u
} }
// Update only the necessary fields // Update only the necessary fields
currentArticle.StatusId = &[]int{2}[0] // approved currentArticle.StatusId = &[]int{2}[0] // approved
currentArticle.CurrentApprovalStep = nil 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) err = _i.ArticlesRepository.UpdateSkipNull(clientId, flow.ArticleId, currentArticle)
if err != nil { if err != nil {