From 536e76d4811bbf5b201c9f4aa2fecfe2ee33f9df Mon Sep 17 00:00:00 2001 From: giovanni-ce Date: Tue, 9 Dec 2025 09:19:50 +0700 Subject: [PATCH] feat[BE-298]: add api get all list closing --- .../controllers/closing.controller.go | 6 ++--- .../closings/services/closing.service.go | 22 ++++++++++++++----- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/internal/modules/closings/controllers/closing.controller.go b/internal/modules/closings/controllers/closing.controller.go index 60af9e2a..ecbded41 100644 --- a/internal/modules/closings/controllers/closing.controller.go +++ b/internal/modules/closings/controllers/closing.controller.go @@ -40,17 +40,17 @@ func (u *ClosingController) GetAll(c *fiber.Ctx) error { } return c.Status(fiber.StatusOK). - JSON(response.SuccessWithPaginate[dto.ClosingListDTO]{ + JSON(response.SuccessWithPaginate[dto.ClosingSummaryDTO]{ Code: fiber.StatusOK, Status: "success", - Message: "Get all closings successfully", + Message: "Retrieved closing projects list successfully", Meta: response.Meta{ Page: query.Page, Limit: query.Limit, TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))), TotalResults: totalResults, }, - Data: dto.ToClosingListDTOs(result), + Data: result, }) } diff --git a/internal/modules/closings/services/closing.service.go b/internal/modules/closings/services/closing.service.go index d3ab26e6..d59d2339 100644 --- a/internal/modules/closings/services/closing.service.go +++ b/internal/modules/closings/services/closing.service.go @@ -23,7 +23,7 @@ import ( ) type ClosingService interface { - GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.ProjectFlock, int64, error) + GetAll(ctx *fiber.Ctx, params *validation.Query) ([]dto.ClosingSummaryDTO, int64, error) GetProjectFlockByID(ctx *fiber.Ctx, id uint) (*entity.ProjectFlock, error) GetPenjualan(ctx *fiber.Ctx, projectFlockID uint) ([]entity.MarketingDeliveryProduct, error) GetClosingSummary(ctx *fiber.Ctx, projectFlockID uint) (*dto.ClosingSummaryDTO, error) @@ -62,7 +62,7 @@ func (s closingService) withClosingRelations(db *gorm.DB) *gorm.DB { Preload("KandangHistory.Chickins") } -func (s closingService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.ProjectFlock, int64, error) { +func (s closingService) GetAll(c *fiber.Ctx, params *validation.Query) ([]dto.ClosingSummaryDTO, int64, error) { if err := s.Validate.Struct(params); err != nil { return nil, 0, err } @@ -70,9 +70,9 @@ func (s closingService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity offset := (params.Page - 1) * params.Limit closings, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB { - db = s.withRelations(db) + db = s.withClosingRelations(db) if params.Search != "" { - return db.Where("name LIKE ?", "%"+params.Search+"%") + return db.Where("flock_name LIKE ?", "%"+params.Search+"%") } return db.Order("created_at DESC").Order("updated_at DESC") }) @@ -81,7 +81,19 @@ func (s closingService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity s.Log.Errorf("Failed to get closings: %+v", err) return nil, 0, err } - return closings, total, nil + + result := make([]dto.ClosingSummaryDTO, 0, len(closings)) + for _, closing := range closings { + statusProject, statusClosing, err := s.getApprovalStatuses(c.Context(), closing.Id) + if err != nil { + s.Log.Errorf("Failed to retrieve approval statuses for project flock %d: %+v", closing.Id, err) + return nil, 0, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch approval status") + } + + result = append(result, dto.ToClosingSummaryDTO(closing, statusProject, statusClosing)) + } + + return result, total, nil } func (s closingService) GetProjectFlockByID(c *fiber.Ctx, id uint) (*entity.ProjectFlock, error) {