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 (
"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,
@ -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.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")

View File

@ -258,6 +258,7 @@ func (_i *articleApprovalFlowsService) processAutoSkipSteps(clientId *uuid.UUID,
if flow.CurrentStep > maxStepOrder {
// All steps completed, mark as 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)
@ -273,7 +274,7 @@ func (_i *articleApprovalFlowsService) processAutoSkipSteps(clientId *uuid.UUID,
// Update only the necessary fields
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)
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
@ -400,7 +407,9 @@ func (_i *articleApprovalFlowsService) ApproveStep(clientId *uuid.UUID, flowId u
// Update only the necessary fields
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)
if err != nil {