diff --git a/app/module/articles/repository/articles.repository.go b/app/module/articles/repository/articles.repository.go index 53a33b8..f77daa4 100644 --- a/app/module/articles/repository/articles.repository.go +++ b/app/module/articles/repository/articles.repository.go @@ -22,7 +22,7 @@ type articlesRepository struct { // ArticlesRepository define interface of IArticlesRepository type ArticlesRepository interface { - GetAll(clientId *uuid.UUID, userLevelId *uint, req request.ArticlesQueryRequest) (articless []*entity.Articles, paging paginator.Pagination, err error) + GetAll(clientId *uuid.UUID, userLevelId *uint, userId *uint, req request.ArticlesQueryRequest) (articless []*entity.Articles, paging paginator.Pagination, err error) GetAllPublishSchedule(clientId *uuid.UUID) (articless []*entity.Articles, err error) FindOne(clientId *uuid.UUID, id uint) (articles *entity.Articles, err error) FindByFilename(clientId *uuid.UUID, thumbnailName string) (articleReturn *entity.Articles, err error) @@ -44,7 +44,7 @@ func NewArticlesRepository(db *database.Database, log zerolog.Logger) ArticlesRe } // implement interface of IArticlesRepository -func (_i *articlesRepository) GetAll(clientId *uuid.UUID, userLevelId *uint, req request.ArticlesQueryRequest) (articless []*entity.Articles, paging paginator.Pagination, err error) { +func (_i *articlesRepository) GetAll(clientId *uuid.UUID, userLevelId *uint, userId *uint, req request.ArticlesQueryRequest) (articless []*entity.Articles, paging paginator.Pagination, err error) { var count int64 query := _i.DB.DB.Model(&entity.Articles{}) @@ -55,9 +55,10 @@ func (_i *articlesRepository) GetAll(clientId *uuid.UUID, userLevelId *uint, req } _i.Log.Info().Interface("userLevelId", userLevelId).Msg("") + _i.Log.Info().Interface("userId", userId).Msg("") // Add approval workflow filtering based on user level if userLevelId != nil { - // Strict filtering logic for article visibility based on approval workflow + // Enhanced filtering logic for article visibility based on approval workflow query = query.Where(` ( -- Articles that don't require approval (bypass or exempt) @@ -66,7 +67,7 @@ func (_i *articlesRepository) GetAll(clientId *uuid.UUID, userLevelId *uint, req -- Articles that are published AND approved through workflow (is_publish = true AND status_id = 2) OR - -- Articles created by users at HIGHER hierarchy only (not same or lower) + -- Articles created by users at HIGHER hierarchy (not same or lower) EXISTS ( SELECT 1 FROM users u JOIN user_levels ul ON u.user_level_id = ul.id @@ -76,6 +77,13 @@ func (_i *articlesRepository) GetAll(clientId *uuid.UUID, userLevelId *uint, req ) ) OR + -- Articles created by the CURRENT user (allow users to see their own articles) + EXISTS ( + SELECT 1 FROM users u + WHERE u.id = articles.created_by_id + AND u.id = ? + ) + OR -- Articles where this user level is the CURRENT approver in the workflow ( workflow_id IS NOT NULL @@ -101,7 +109,7 @@ func (_i *articlesRepository) GetAll(clientId *uuid.UUID, userLevelId *uint, req ) ) ) - `, *userLevelId, *userLevelId, *userLevelId) + `, *userLevelId, *userId, *userLevelId, *userLevelId) } if req.CategoryId != nil { diff --git a/app/module/articles/service/articles.service.go b/app/module/articles/service/articles.service.go index 2f1ca33..3af222c 100644 --- a/app/module/articles/service/articles.service.go +++ b/app/module/articles/service/articles.service.go @@ -122,9 +122,10 @@ func NewArticlesService( // All implement interface of ArticlesService func (_i *articlesService) All(authToken string, req request.ArticlesQueryRequest) (articless []*response.ArticlesResponse, paging paginator.Pagination, err error) { - // Extract clientId and userLevelId from authToken + // Extract clientId, userLevelId, and userId from authToken var clientId *uuid.UUID var userLevelId *uint + var userId *uint if authToken != "" { user := utilSvc.GetUserInfo(_i.Log, _i.UsersRepo, authToken) if user != nil { @@ -133,7 +134,9 @@ func (_i *articlesService) All(authToken string, req request.ArticlesQueryReques _i.Log.Info().Interface("clientId", clientId).Msg("Extracted clientId from auth token") } userLevelId = &user.UserLevelId + userId = &user.ID _i.Log.Info().Interface("userLevelId", userLevelId).Msg("Extracted userLevelId from auth token") + _i.Log.Info().Interface("userId", userId).Msg("Extracted userId from auth token") } } @@ -145,7 +148,7 @@ func (_i *articlesService) All(authToken string, req request.ArticlesQueryReques req.CategoryId = &findCategory.ID } - results, paging, err := _i.Repo.GetAll(clientId, userLevelId, req) + results, paging, err := _i.Repo.GetAll(clientId, userLevelId, userId, req) if err != nil { return } @@ -1234,7 +1237,7 @@ func (_i *articlesService) GetArticlesWaitingForApproval(authToken string, page, Pagination: &pagination, } - articles, paging, err := _i.Repo.GetAll(clientId, &user.UserLevelId, req) + articles, paging, err := _i.Repo.GetAll(clientId, &user.UserLevelId, &user.ID, req) if err != nil { return nil, paging, err }