This commit is contained in:
giovanni
2026-04-22 01:44:37 +07:00
parent 091f706276
commit fec7bb5825
3 changed files with 79 additions and 6 deletions
@@ -312,10 +312,10 @@ func (u *ProjectflockController) LookupProjectFlockKandang(c *fiber.Ctx) error {
mapped := warehouseDTO.ToWarehouseRelationDTO(*warehouse)
dtoResult.Warehouse = &mapped
}
if isTransition, isLaying, serr := u.ProjectflockService.GetProjectFlockKandangTransferStateAtDate(c, result.Id, recordDate); serr != nil {
if _, isLaying, serr := u.ProjectflockService.GetProjectFlockKandangTransferStateAtDate(c, result.Id, recordDate); serr != nil {
return serr
} else {
dtoResult.IsTransition = isTransition
dtoResult.IsTransition = false
dtoResult.IsLaying = isLaying
}
applyCutOverLayingLookupOverride(&dtoResult)
@@ -1737,9 +1737,6 @@ func (s *recordingService) resolveTransferSourceProjectFlockKandangID(ctx contex
if transfer == nil || transfer.Id == 0 {
return 0, nil
}
if transfer.SourceProjectFlockKandangId != nil && *transfer.SourceProjectFlockKandangId != 0 {
return *transfer.SourceProjectFlockKandangId, nil
}
var row struct {
SourceProjectFlockKandangID uint `gorm:"column:source_project_flock_kandang_id"`
@@ -1754,12 +1751,21 @@ func (s *recordingService) resolveTransferSourceProjectFlockKandangID(ctx contex
Limit(1).
Take(&row).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
if transfer.SourceProjectFlockKandangId != nil && *transfer.SourceProjectFlockKandangId != 0 {
return *transfer.SourceProjectFlockKandangId, nil
}
return 0, nil
}
if err != nil {
return 0, err
}
return row.SourceProjectFlockKandangID, nil
if row.SourceProjectFlockKandangID != 0 {
return row.SourceProjectFlockKandangID, nil
}
if transfer.SourceProjectFlockKandangId != nil && *transfer.SourceProjectFlockKandangId != 0 {
return *transfer.SourceProjectFlockKandangId, nil
}
return 0, nil
}
func (s *recordingService) getEarliestChickInDateByProjectFlockKandangID(ctx context.Context, projectFlockKandangID uint) (*time.Time, error) {
@@ -148,6 +148,53 @@ func TestShouldNormalizeEggRequestsOnUpdateNormalizesFarmLevelEggs(t *testing.T)
}
}
func TestResolveTransferSourceProjectFlockKandangIDPrefersTransferResourceSource(t *testing.T) {
db := setupTransferSourceResolutionTestDB(t)
repo := repository.NewRecordingRepository(db)
svc := &recordingService{Repository: repo}
transfer := &entity.LayingTransfer{
Id: 77,
SourceProjectFlockKandangId: uintPtrForTest(999),
}
if err := db.Exec(`INSERT INTO laying_transfer_sources (id, laying_transfer_id, source_project_flock_kandang_id, deleted_at) VALUES (1, 77, 123, NULL)`).Error; err != nil {
t.Fatalf("failed seeding laying_transfer_sources: %v", err)
}
got, err := svc.resolveTransferSourceProjectFlockKandangID(context.Background(), transfer)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if got != 123 {
t.Fatalf("expected resource source project_flock_kandang_id=123, got %d", got)
}
}
func TestResolveTransferSourceProjectFlockKandangIDFallsBackToTransferHeader(t *testing.T) {
db := setupTransferSourceResolutionTestDB(t)
repo := repository.NewRecordingRepository(db)
svc := &recordingService{Repository: repo}
transfer := &entity.LayingTransfer{
Id: 78,
SourceProjectFlockKandangId: uintPtrForTest(456),
}
got, err := svc.resolveTransferSourceProjectFlockKandangID(context.Background(), transfer)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if got != 456 {
t.Fatalf("expected fallback source project_flock_kandang_id=456, got %d", got)
}
}
func uintPtrForTest(value uint) *uint {
v := value
return &v
}
func setupRecordingServiceTestDB(t *testing.T) *gorm.DB {
t.Helper()
@@ -193,3 +240,23 @@ func setupRecordingServiceTestDB(t *testing.T) *gorm.DB {
return db
}
func setupTransferSourceResolutionTestDB(t *testing.T) *gorm.DB {
t.Helper()
db, err := gorm.Open(sqlite.Open("file:"+t.Name()+"?mode=memory&cache=private"), &gorm.Config{})
if err != nil {
t.Fatalf("failed opening sqlite db: %v", err)
}
if err := db.Exec(`CREATE TABLE laying_transfer_sources (
id INTEGER PRIMARY KEY,
laying_transfer_id INTEGER NOT NULL,
source_project_flock_kandang_id INTEGER NOT NULL,
deleted_at TIMESTAMP NULL
)`).Error; err != nil {
t.Fatalf("failed creating laying_transfer_sources schema: %v", err)
}
return db
}