From 2ad0c17fbe095c7954ff43a357309883d6f74e64 Mon Sep 17 00:00:00 2001 From: giovanni Date: Thu, 22 Jan 2026 10:00:24 +0700 Subject: [PATCH] add filter project status and location id --- .../controllers/closing.controller.go | 27 ++++++++++++++++--- .../closings/services/closing.service.go | 22 +++++++++++++++ .../validations/closing.validation.go | 8 +++--- 3 files changed, 51 insertions(+), 6 deletions(-) diff --git a/internal/modules/closings/controllers/closing.controller.go b/internal/modules/closings/controllers/closing.controller.go index 29c89f33..b1b02886 100644 --- a/internal/modules/closings/controllers/closing.controller.go +++ b/internal/modules/closings/controllers/closing.controller.go @@ -28,10 +28,31 @@ func NewClosingController(closingService service.ClosingService, sapronakService } func (u *ClosingController) GetAll(c *fiber.Ctx) error { + var projectStatus *int + if raw := c.Query("project_status"); raw != "" { + statusValue, err := strconv.Atoi(raw) + if err != nil { + return fiber.NewError(fiber.StatusBadRequest, "Invalid project_status") + } + projectStatus = &statusValue + } + + var locationID *uint + if raw := c.Query("location_id"); raw != "" { + locationValue, err := strconv.Atoi(raw) + if err != nil || locationValue <= 0 { + return fiber.NewError(fiber.StatusBadRequest, "Invalid location_id") + } + locationUint := uint(locationValue) + locationID = &locationUint + } + query := &validation.Query{ - Page: c.QueryInt("page", 1), - Limit: c.QueryInt("limit", 10), - Search: c.Query("search", ""), + Page: c.QueryInt("page", 1), + Limit: c.QueryInt("limit", 10), + Search: c.Query("search", ""), + ProjectStatus: projectStatus, + LocationID: locationID, } if query.Page < 1 || query.Limit < 1 { diff --git a/internal/modules/closings/services/closing.service.go b/internal/modules/closings/services/closing.service.go index daca980f..372d38fd 100644 --- a/internal/modules/closings/services/closing.service.go +++ b/internal/modules/closings/services/closing.service.go @@ -99,9 +99,31 @@ func (s closingService) GetAll(c *fiber.Ctx, params *validation.Query) ([]dto.Cl } offset := (params.Page - 1) * params.Limit + statusFilter := "" + if params.ProjectStatus != nil { + switch *params.ProjectStatus { + case 1: + statusFilter = "Pengajuan" + case 2: + statusFilter = "Aktif" + } + } closings, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB { db = s.withClosingRelations(db) + if params.LocationID != nil { + db = db.Where("location_id = ?", *params.LocationID) + } + if statusFilter != "" { + latestApprovalSubQuery := s.Repository.DB(). + WithContext(c.Context()). + Table("approvals"). + Select("DISTINCT ON (approvable_id) approvable_id, step_name, id"). + Where("approvable_type = ?", utils.ApprovalWorkflowProjectFlock.String()). + Order("approvable_id, id DESC") + db = db.Joins("JOIN (?) AS latest_approval ON latest_approval.approvable_id = project_flocks.id", latestApprovalSubQuery). + Where("LOWER(latest_approval.step_name) = LOWER(?)", statusFilter) + } if params.Search != "" { return db.Where("flock_name ILIKE ?", "%"+params.Search+"%") } diff --git a/internal/modules/closings/validations/closing.validation.go b/internal/modules/closings/validations/closing.validation.go index 454bbdfc..9d3ad573 100644 --- a/internal/modules/closings/validations/closing.validation.go +++ b/internal/modules/closings/validations/closing.validation.go @@ -9,9 +9,11 @@ type Update struct { } type Query struct { - Page int `query:"page" validate:"omitempty,number,min=1,gt=0"` - Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"` - Search string `query:"search" validate:"omitempty,max=50"` + Page int `query:"page" validate:"omitempty,number,min=1,gt=0"` + Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"` + Search string `query:"search" validate:"omitempty,max=50"` + ProjectStatus *int `query:"project_status" validate:"omitempty,oneof=1 2"` + LocationID *uint `query:"location_id" validate:"omitempty,gt=0"` } const (