feat[BE-298]: add api get all list closing

This commit is contained in:
giovanni-ce
2025-12-09 09:19:50 +07:00
parent 29aa737422
commit 536e76d481
2 changed files with 20 additions and 8 deletions
@@ -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,
})
}
@@ -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) {