fix recording standar prod laying

This commit is contained in:
giovanni
2026-06-07 18:55:24 +07:00
parent edfd6ac95c
commit 2216f572c2
6 changed files with 445 additions and 11 deletions
@@ -387,35 +387,87 @@ func (s productionStandardService) EnsureWeekAvailable(ctx context.Context, stan
return nil
}
week := ((day - 1) / 7) + 1
if week <= 0 {
requestedWeek := ((day - 1) / 7) + 1
if requestedWeek <= 0 {
return nil
}
upperCategory := strings.ToUpper(category)
if upperCategory == string(utils.ProjectFlockCategoryLaying) {
detail, err := s.ProductionStandardDetailRepo.GetByStandardIDAndWeek(ctx, standardID, week)
effectiveWeek := requestedWeek
firstCommonWeek, ok, err := s.layingFirstCommonStandardWeek(ctx, standardID)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Standart production tidak tersedia untuk week %d", week))
}
return err
}
if detail == nil {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Standart production tidak tersedia untuk week %d", week))
if ok && requestedWeek < firstCommonWeek {
effectiveWeek = firstCommonWeek
}
detail, err := s.ProductionStandardDetailRepo.GetByStandardIDAndWeek(ctx, standardID, effectiveWeek)
if err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
return err
}
}
growthDetail, err := s.StandardGrowthDetailRepo.GetByStandardIDAndWeek(ctx, standardID, effectiveWeek)
if err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
return err
}
}
if detail != nil && growthDetail != nil {
return nil
}
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Standart production tidak tersedia untuk week %d", requestedWeek))
}
growthDetail, err := s.StandardGrowthDetailRepo.GetByStandardIDAndWeek(ctx, standardID, week)
growthDetail, err := s.StandardGrowthDetailRepo.GetByStandardIDAndWeek(ctx, standardID, requestedWeek)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Standart production tidak tersedia untuk week %d", week))
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Standart production tidak tersedia untuk week %d", requestedWeek))
}
return err
}
if growthDetail == nil {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Standart production tidak tersedia untuk week %d", week))
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Standart production tidak tersedia untuk week %d", requestedWeek))
}
return nil
}
func (s productionStandardService) layingFirstCommonStandardWeek(ctx context.Context, standardID uint) (int, bool, error) {
details, err := s.ProductionStandardDetailRepo.GetByProductionStandardID(ctx, standardID)
if err != nil {
return 0, false, err
}
detailWeeks := make(map[int]struct{}, len(details))
for _, detail := range details {
if detail.Week <= 0 {
continue
}
detailWeeks[detail.Week] = struct{}{}
}
growthDetails, err := s.StandardGrowthDetailRepo.GetByProductionStandardID(ctx, standardID)
if err != nil {
return 0, false, err
}
firstCommonWeek := 0
for _, detail := range growthDetails {
if detail.Week <= 0 {
continue
}
if _, ok := detailWeeks[detail.Week]; !ok {
continue
}
if firstCommonWeek == 0 || detail.Week < firstCommonWeek {
firstCommonWeek = detail.Week
}
}
return firstCommonWeek, firstCommonWeek > 0, nil
}
@@ -0,0 +1,95 @@
package service
import (
"context"
"strings"
"testing"
"github.com/glebarez/sqlite"
repositories "gitlab.com/mbugroup/lti-api.git/internal/modules/master/production-standards/repositories"
"gitlab.com/mbugroup/lti-api.git/internal/utils"
"gorm.io/gorm"
)
func TestEnsureWeekAvailableAllowsLayingBeforeFirstCommonStandardWeek(t *testing.T) {
svc := setupProductionStandardServiceTest(t)
if err := svc.EnsureWeekAvailable(context.Background(), 1, string(utils.ProjectFlockCategoryLaying), 85); err != nil {
t.Fatalf("expected pre-standard laying week to be allowed, got %v", err)
}
}
func TestEnsureWeekAvailableRejectsLayingMissingWeekAfterStandardStarts(t *testing.T) {
svc := setupProductionStandardServiceTest(t)
err := svc.EnsureWeekAvailable(context.Background(), 1, string(utils.ProjectFlockCategoryLaying), 127)
if err == nil {
t.Fatal("expected missing laying standard week to be rejected")
}
if !strings.Contains(err.Error(), "week 19") {
t.Fatalf("expected error to mention requested week 19, got %v", err)
}
}
func TestEnsureWeekAvailableKeepsGrowingWeekStrict(t *testing.T) {
svc := setupProductionStandardServiceTest(t)
err := svc.EnsureWeekAvailable(context.Background(), 2, string(utils.ProjectFlockCategoryGrowing), 8)
if err == nil {
t.Fatal("expected missing growing standard week to be rejected")
}
if !strings.Contains(err.Error(), "week 2") {
t.Fatalf("expected error to mention requested week 2, got %v", err)
}
}
func setupProductionStandardServiceTest(t *testing.T) productionStandardService {
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)
}
statements := []string{
`CREATE TABLE production_standard_details (
id INTEGER PRIMARY KEY,
production_standard_id INTEGER NOT NULL,
week INTEGER NOT NULL,
target_hen_day_production NUMERIC NULL,
target_hen_house_production NUMERIC NULL,
target_egg_weight NUMERIC NULL,
target_egg_mass NUMERIC NULL,
standard_fcr NUMERIC NULL,
created_at TIMESTAMP NULL,
updated_at TIMESTAMP NULL
)`,
`CREATE TABLE standard_growth_details (
id INTEGER PRIMARY KEY,
production_standard_id INTEGER NOT NULL,
target_mean_bw NUMERIC NULL,
max_depletion NUMERIC NULL,
min_uniformity NUMERIC NOT NULL,
week INTEGER NOT NULL,
feed_intake NUMERIC NULL,
created_at TIMESTAMP NULL,
created_by INTEGER NOT NULL
)`,
`INSERT INTO production_standard_details (id, production_standard_id, week, standard_fcr) VALUES
(1, 1, 18, 2.1)`,
`INSERT INTO standard_growth_details (id, production_standard_id, week, min_uniformity, created_by) VALUES
(1, 1, 18, 80, 1),
(2, 2, 1, 80, 1)`,
}
for _, stmt := range statements {
if err := db.Exec(stmt).Error; err != nil {
t.Fatalf("failed preparing schema: %v", err)
}
}
return productionStandardService{
ProductionStandardDetailRepo: repositories.NewProductionStandardDetailRepository(db),
StandardGrowthDetailRepo: repositories.NewStandardGrowthDetailRepository(db),
}
}