From 2713210bccda821d282371a6849987fe1e7c0ba8 Mon Sep 17 00:00:00 2001 From: Adnan Zahir Date: Tue, 17 Mar 2026 18:41:51 +0700 Subject: [PATCH] fix: override is_transfer lookup for direct cut-over laying flocks --- .../controllers/projectflock.controller.go | 15 +++++ .../projectflock.controller_test.go | 60 +++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 internal/modules/production/project_flocks/controllers/projectflock.controller_test.go diff --git a/internal/modules/production/project_flocks/controllers/projectflock.controller.go b/internal/modules/production/project_flocks/controllers/projectflock.controller.go index 6ad70ae1..6a7b4cb0 100644 --- a/internal/modules/production/project_flocks/controllers/projectflock.controller.go +++ b/internal/modules/production/project_flocks/controllers/projectflock.controller.go @@ -318,6 +318,7 @@ func (u *ProjectflockController) LookupProjectFlockKandang(c *fiber.Ctx) error { dtoResult.IsTransition = isTransition dtoResult.IsLaying = isLaying } + applyCutOverLayingLookupOverride(&dtoResult) if withPopulation { population := dtoResult.AvailableQuantity dtoResult.Population = &population @@ -344,6 +345,20 @@ func (u *ProjectflockController) LookupProjectFlockKandang(c *fiber.Ctx) error { Data: dtoResult}) } +func applyCutOverLayingLookupOverride(result *dto.ProjectFlockKandangDTO) { + if result == nil || result.ProjectFlock == nil || result.IsLaying || result.ChickInDate == nil { + return + } + + category := strings.ToUpper(strings.TrimSpace(result.ProjectFlock.Category)) + if category != strings.ToUpper(string(utils.ProjectFlockCategoryLaying)) { + return + } + + result.IsTransition = false + result.IsLaying = true +} + func (u *ProjectflockController) Resubmit(c *fiber.Ctx) error { param := c.Params("id") req := new(validation.Resubmit) diff --git a/internal/modules/production/project_flocks/controllers/projectflock.controller_test.go b/internal/modules/production/project_flocks/controllers/projectflock.controller_test.go new file mode 100644 index 00000000..3f37cd35 --- /dev/null +++ b/internal/modules/production/project_flocks/controllers/projectflock.controller_test.go @@ -0,0 +1,60 @@ +package controller + +import ( + "testing" + "time" + + "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/dto" +) + +func TestApplyCutOverLayingLookupOverride(t *testing.T) { + t.Run("marks direct cut-over laying flock as laying", func(t *testing.T) { + chickinDate := time.Date(2025, time.August, 15, 0, 0, 0, 0, time.UTC) + result := &dto.ProjectFlockKandangDTO{ + ChickInDate: &chickinDate, + ProjectFlock: &dto.ProjectFlockWithPivotDTO{ + Category: "LAYING", + }, + } + + applyCutOverLayingLookupOverride(result) + + if !result.IsLaying { + t.Fatalf("expected cut-over laying flock to be exposed as laying") + } + if result.IsTransition { + t.Fatalf("expected cut-over laying flock to stay out of transition") + } + }) + + t.Run("keeps transfer-based state when chickin is absent", func(t *testing.T) { + result := &dto.ProjectFlockKandangDTO{ + ProjectFlock: &dto.ProjectFlockWithPivotDTO{ + Category: "LAYING", + }, + } + + applyCutOverLayingLookupOverride(result) + + if result.IsLaying { + t.Fatalf("expected lookup override to skip non cut-over laying flocks") + } + }) + + t.Run("does not change already laying lookup state", func(t *testing.T) { + chickinDate := time.Date(2025, time.August, 15, 0, 0, 0, 0, time.UTC) + result := &dto.ProjectFlockKandangDTO{ + IsLaying: true, + ChickInDate: &chickinDate, + ProjectFlock: &dto.ProjectFlockWithPivotDTO{ + Category: "LAYING", + }, + } + + applyCutOverLayingLookupOverride(result) + + if !result.IsLaying { + t.Fatalf("expected laying state to remain true") + } + }) +}