From e39a8d442a9b352064bda25bedbb57993e5d8ddf Mon Sep 17 00:00:00 2001 From: hanif salafi Date: Tue, 23 Sep 2025 02:42:53 +0700 Subject: [PATCH] feat: update approval articles --- app/module/articles/articles.module.go | 13 +++---- .../controller/articles.controller.go | 19 +++++----- .../articles/service/articles.service.go | 36 ++++++++++++++----- docs/swagger/docs.go | 7 ++++ docs/swagger/swagger.json | 7 ++++ docs/swagger/swagger.yaml | 5 +++ 6 files changed, 63 insertions(+), 24 deletions(-) diff --git a/app/module/articles/articles.module.go b/app/module/articles/articles.module.go index 09d106b..79e0ae4 100644 --- a/app/module/articles/articles.module.go +++ b/app/module/articles/articles.module.go @@ -51,6 +51,13 @@ func (_i *ArticlesRouter) RegisterArticlesRoutes() { _i.App.Route("/articles", func(router fiber.Router) { // Add user middleware to extract user level from JWT token router.Use(middleware.UserMiddleware(_i.UsersRepo)) + + // Dynamic approval system routes + router.Post("/:id/submit-approval", articlesController.SubmitForApproval) + router.Get("/:id/approval-status", articlesController.GetApprovalStatus) + router.Get("/pending-approval", articlesController.GetPendingApprovals) + router.Get("/waiting-for-approval", articlesController.GetArticlesWaitingForApproval) + router.Get("/", articlesController.All) router.Get("/old-id/:id", articlesController.ShowByOldId) router.Get("/:id", articlesController.Show) @@ -64,11 +71,5 @@ func (_i *ArticlesRouter) RegisterArticlesRoutes() { router.Get("/statistic/summary", articlesController.SummaryStats) router.Get("/statistic/user-levels", articlesController.ArticlePerUserLevelStats) router.Get("/statistic/monthly", articlesController.ArticleMonthlyStats) - - // Dynamic approval system routes - router.Post("/:id/submit-approval", articlesController.SubmitForApproval) - router.Get("/:id/approval-status", articlesController.GetApprovalStatus) - router.Get("/pending-approval", articlesController.GetPendingApprovals) - router.Get("/waiting-for-approval", articlesController.GetArticlesWaitingForApproval) }) } diff --git a/app/module/articles/controller/articles.controller.go b/app/module/articles/controller/articles.controller.go index 0524c33..6ca2948 100644 --- a/app/module/articles/controller/articles.controller.go +++ b/app/module/articles/controller/articles.controller.go @@ -532,10 +532,10 @@ func (_i *articlesController) SubmitForApproval(c *fiber.Ctx) error { // Get ClientId from context clientId := middleware.GetClientID(c) - // Get user ID from token (you'll need to implement this based on your auth system) - userId := uint(1) // TODO: Get from JWT token + // Get Authorization token from header + authToken := c.Get("Authorization") - err = _i.articlesService.SubmitForApproval(clientId, uint(id), userId, req.WorkflowId) + err = _i.articlesService.SubmitForApproval(clientId, uint(id), authToken, req.WorkflowId) if err != nil { return err } @@ -608,10 +608,10 @@ func (_i *articlesController) GetPendingApprovals(c *fiber.Ctx) error { // Get ClientId from context clientId := middleware.GetClientID(c) - // Get user level ID from token (you'll need to implement this based on your auth system) - userLevelId := uint(1) // TODO: Get from JWT token + // Get Authorization token from header + authToken := c.Get("Authorization") - response, paging, err := _i.articlesService.GetPendingApprovals(clientId, userLevelId, page, limit) + response, paging, err := _i.articlesService.GetPendingApprovals(clientId, authToken, page, limit) if err != nil { return err } @@ -630,6 +630,7 @@ func (_i *articlesController) GetPendingApprovals(c *fiber.Ctx) error { // @Tags Articles // @Security Bearer // @Param X-Client-Key header string true "Client Key" +// @Param Authorization header string false "Insert your access token" default(Bearer ) // @Param page query int false "Page number" default(1) // @Param limit query int false "Items per page" default(10) // @Success 200 {object} response.Response @@ -651,10 +652,10 @@ func (_i *articlesController) GetArticlesWaitingForApproval(c *fiber.Ctx) error // Get ClientId from context clientId := middleware.GetClientID(c) - // Get user level from middleware - userLevelId := middleware.GetUserLevelID(c) + // Get Authorization token from header + authToken := c.Get("Authorization") - responses, paging, err := _i.articlesService.GetArticlesWaitingForApproval(clientId, *userLevelId, page, limit) + responses, paging, err := _i.articlesService.GetArticlesWaitingForApproval(clientId, authToken, page, limit) if err != nil { return err } diff --git a/app/module/articles/service/articles.service.go b/app/module/articles/service/articles.service.go index 019b78e..0794084 100644 --- a/app/module/articles/service/articles.service.go +++ b/app/module/articles/service/articles.service.go @@ -73,10 +73,10 @@ type ArticlesService interface { ExecuteScheduling() error // Dynamic approval system methods - SubmitForApproval(clientId *uuid.UUID, articleId uint, submittedById uint, workflowId *uint) error + SubmitForApproval(clientId *uuid.UUID, articleId uint, authToken string, workflowId *uint) error GetApprovalStatus(clientId *uuid.UUID, articleId uint) (*response.ArticleApprovalStatusResponse, error) - GetArticlesWaitingForApproval(clientId *uuid.UUID, userLevelId uint, page, limit int) ([]*response.ArticleApprovalQueueResponse, paginator.Pagination, error) - GetPendingApprovals(clientId *uuid.UUID, userLevelId uint, page, limit int) ([]*response.ArticleApprovalQueueResponse, paginator.Pagination, error) + GetArticlesWaitingForApproval(clientId *uuid.UUID, authToken string, page, limit int) ([]*response.ArticleApprovalQueueResponse, paginator.Pagination, error) + GetPendingApprovals(clientId *uuid.UUID, authToken string, page, limit int) ([]*response.ArticleApprovalQueueResponse, paginator.Pagination, error) // No-approval system methods CheckApprovalRequired(clientId *uuid.UUID, articleId uint, userId uint, userLevelId uint) (bool, error) @@ -732,7 +732,13 @@ func getFileExtension(filename string) string { } // SubmitForApproval submits an article for approval using the dynamic workflow system -func (_i *articlesService) SubmitForApproval(clientId *uuid.UUID, articleId uint, submittedById uint, workflowId *uint) error { +func (_i *articlesService) SubmitForApproval(clientId *uuid.UUID, articleId uint, authToken string, workflowId *uint) error { + // Extract user info from auth token + user := utilSvc.GetUserInfo(_i.Log, _i.UsersRepo, authToken) + if user == nil { + return errors.New("user not found from auth token") + } + // Check if article exists article, err := _i.Repo.FindOne(clientId, articleId) if err != nil { @@ -761,7 +767,7 @@ func (_i *articlesService) SubmitForApproval(clientId *uuid.UUID, articleId uint CurrentStep: 1, StatusId: 1, // 1 = In Progress ClientId: clientId, - SubmittedById: submittedById, + SubmittedById: user.ID, } _, err = _i.ArticleApprovalFlowsRepo.Create(clientId, approvalFlow) @@ -866,9 +872,15 @@ func (_i *articlesService) GetApprovalStatus(clientId *uuid.UUID, articleId uint } // GetPendingApprovals gets articles pending approval for a specific user level -func (_i *articlesService) GetPendingApprovals(clientId *uuid.UUID, userLevelId uint, page, limit int) ([]*response.ArticleApprovalQueueResponse, paginator.Pagination, error) { +func (_i *articlesService) GetPendingApprovals(clientId *uuid.UUID, authToken string, page, limit int) ([]*response.ArticleApprovalQueueResponse, paginator.Pagination, error) { + // Extract user info from auth token + user := utilSvc.GetUserInfo(_i.Log, _i.UsersRepo, authToken) + if user == nil { + return nil, paginator.Pagination{}, errors.New("user not found from auth token") + } + // Get pending approvals for the user level - approvalFlows, paging, err := _i.ArticleApprovalFlowsRepo.GetPendingApprovalsByUserLevel(clientId, userLevelId, page, limit) + approvalFlows, paging, err := _i.ArticleApprovalFlowsRepo.GetPendingApprovalsByUserLevel(clientId, user.UserLevelId, page, limit) if err != nil { return nil, paging, err } @@ -948,7 +960,13 @@ func (_i *articlesService) GetPendingApprovals(clientId *uuid.UUID, userLevelId } // GetArticlesWaitingForApproval gets articles that are waiting for approval by a specific user level -func (_i *articlesService) GetArticlesWaitingForApproval(clientId *uuid.UUID, userLevelId uint, page, limit int) ([]*response.ArticleApprovalQueueResponse, paginator.Pagination, error) { +func (_i *articlesService) GetArticlesWaitingForApproval(clientId *uuid.UUID, authToken string, page, limit int) ([]*response.ArticleApprovalQueueResponse, paginator.Pagination, error) { + // Extract user info from auth token + user := utilSvc.GetUserInfo(_i.Log, _i.UsersRepo, authToken) + if user == nil { + return nil, paginator.Pagination{}, errors.New("user not found from auth token") + } + // Use the existing repository method with proper filtering pagination := paginator.Pagination{ Page: page, @@ -958,7 +976,7 @@ func (_i *articlesService) GetArticlesWaitingForApproval(clientId *uuid.UUID, us Pagination: &pagination, } - articles, paging, err := _i.Repo.GetAll(clientId, &userLevelId, req) + articles, paging, err := _i.Repo.GetAll(clientId, &user.UserLevelId, req) if err != nil { return nil, paging, err } diff --git a/docs/swagger/docs.go b/docs/swagger/docs.go index c4aff50..0a208b2 100644 --- a/docs/swagger/docs.go +++ b/docs/swagger/docs.go @@ -7592,6 +7592,13 @@ const docTemplate = `{ "in": "header", "required": true }, + { + "type": "string", + "default": "Bearer \u003cAdd access token here\u003e", + "description": "Insert your access token", + "name": "Authorization", + "in": "header" + }, { "type": "integer", "default": 1, diff --git a/docs/swagger/swagger.json b/docs/swagger/swagger.json index a1a546f..47896b6 100644 --- a/docs/swagger/swagger.json +++ b/docs/swagger/swagger.json @@ -7581,6 +7581,13 @@ "in": "header", "required": true }, + { + "type": "string", + "default": "Bearer \u003cAdd access token here\u003e", + "description": "Insert your access token", + "name": "Authorization", + "in": "header" + }, { "type": "integer", "default": 1, diff --git a/docs/swagger/swagger.yaml b/docs/swagger/swagger.yaml index 9937537..75cafc5 100644 --- a/docs/swagger/swagger.yaml +++ b/docs/swagger/swagger.yaml @@ -6507,6 +6507,11 @@ paths: name: X-Client-Key required: true type: string + - default: Bearer + description: Insert your access token + in: header + name: Authorization + type: string - default: 1 description: Page number in: query