From 8e9fe2559b4655dd20025eb847b6bafc0670c868 Mon Sep 17 00:00:00 2001 From: hanif salafi Date: Tue, 16 Sep 2025 23:45:21 +0700 Subject: [PATCH] feat: update article all --- .../repository/articles.repository.go | 51 +++++++++---------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/app/module/articles/repository/articles.repository.go b/app/module/articles/repository/articles.repository.go index 28c5e0a..526a27e 100644 --- a/app/module/articles/repository/articles.repository.go +++ b/app/module/articles/repository/articles.repository.go @@ -54,53 +54,50 @@ func (_i *articlesRepository) GetAll(clientId *uuid.UUID, userLevelId *uint, req query = query.Where("client_id = ?", clientId) } + _i.Log.Info().Interface("userLevelId", userLevelId).Msg("") // Add approval workflow filtering based on user level if userLevelId != nil { - // Complete filtering logic for article visibility + // Strict filtering logic for article visibility based on approval workflow query = query.Where(` ( - -- Articles that don't require approval + -- Articles that don't require approval (bypass or exempt) (bypass_approval = true OR approval_exempt = true) OR - -- Articles that are published - (is_publish = true) + -- Articles that are published AND approved through workflow + (is_publish = true AND status_id = 2) OR - -- Articles where this user level is an approver in current step - ( - workflow_id IS NOT NULL - AND current_approval_step > 0 - AND EXISTS ( - SELECT 1 FROM article_approval_flows aaf - JOIN approval_workflow_steps aws ON aaf.workflow_id = aws.workflow_id - WHERE aaf.article_id = articles.id - AND aaf.status_id = 1 - AND aws.required_user_level_id = ? - AND aws.step_order = aaf.current_step - ) - ) - OR - -- Articles created by users at same or lower hierarchy + -- Articles created by users at HIGHER hierarchy only (not same or lower) EXISTS ( SELECT 1 FROM users u JOIN user_levels ul ON u.user_level_id = ul.id WHERE u.id = articles.created_by_id - AND ul.level_number >= ( + AND ul.level_number < ( SELECT ul2.level_number FROM user_levels ul2 WHERE ul2.id = ? ) ) OR - -- Articles where this user level is ANY approver in the workflow + -- Articles where this user level is the CURRENT approver in the workflow ( workflow_id IS NOT NULL AND EXISTS ( SELECT 1 FROM article_approval_flows aaf + JOIN approval_workflow_steps aws ON aaf.workflow_id = aws.workflow_id WHERE aaf.article_id = articles.id - AND aaf.status_id IN (1, 4) - AND EXISTS ( - SELECT 1 FROM approval_workflow_steps aws - WHERE aws.workflow_id = aaf.workflow_id - AND aws.required_user_level_id = ? - ) + AND aaf.status_id = 1 -- Only in progress + AND aws.required_user_level_id = ? + AND aws.step_order = aaf.current_step -- Must be current step + ) + ) + OR + -- Articles that have been approved by this user level + ( + workflow_id IS NOT NULL + AND EXISTS ( + SELECT 1 FROM article_approval_flows aaf + JOIN article_approval_step_logs aasl ON aaf.id = aasl.approval_flow_id + WHERE aaf.article_id = articles.id + AND aasl.user_level_id = ? + AND aasl.action = 'approve' ) ) )