mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
add response logic cut over recording for view
This commit is contained in:
@@ -167,6 +167,7 @@ func (s recordingService) GetAll(c *fiber.Ctx, params *validation.Query) ([]enti
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
cutOverChickinAvailability := make(map[uint]bool)
|
||||
for i := range recordings {
|
||||
if recordings[i].ProjectFlockKandangId != 0 && !recordings[i].RecordDatetime.IsZero() {
|
||||
total := cumulativeMap[recordings[i].Id]
|
||||
@@ -189,6 +190,10 @@ func (s recordingService) GetAll(c *fiber.Ctx, params *validation.Query) ([]enti
|
||||
if stateErr != nil {
|
||||
return nil, 0, stateErr
|
||||
}
|
||||
isTransition, isLaying, stateErr = s.applyCutOverLayingRecordingLookupOverride(c.Context(), &recordings[i], isTransition, isLaying, cutOverChickinAvailability)
|
||||
if stateErr != nil {
|
||||
return nil, 0, stateErr
|
||||
}
|
||||
recordings[i].PopulationCanChange = boolPtr(populationCanChange)
|
||||
recordings[i].TransferExecuted = boolPtr(transferExecuted)
|
||||
recordings[i].IsTransition = boolPtr(isTransition)
|
||||
@@ -257,6 +262,10 @@ func (s recordingService) GetOne(c *fiber.Ctx, id uint) (*entity.Recording, erro
|
||||
if stateErr != nil {
|
||||
return nil, stateErr
|
||||
}
|
||||
isTransition, isLaying, stateErr = s.applyCutOverLayingRecordingLookupOverride(c.Context(), recording, isTransition, isLaying, nil)
|
||||
if stateErr != nil {
|
||||
return nil, stateErr
|
||||
}
|
||||
recording.PopulationCanChange = boolPtr(populationCanChange)
|
||||
recording.TransferExecuted = boolPtr(transferExecuted)
|
||||
recording.IsTransition = boolPtr(isTransition)
|
||||
@@ -1003,6 +1012,69 @@ func (s *recordingService) resolveRecordingCategory(ctx context.Context, recordi
|
||||
return strings.ToUpper(strings.TrimSpace(pfk.ProjectFlock.Category)), nil
|
||||
}
|
||||
|
||||
func (s *recordingService) applyCutOverLayingRecordingLookupOverride(
|
||||
ctx context.Context,
|
||||
recording *entity.Recording,
|
||||
isTransition bool,
|
||||
isLaying bool,
|
||||
chickinAvailabilityCache map[uint]bool,
|
||||
) (bool, bool, error) {
|
||||
if recording == nil || recording.ProjectFlockKandangId == 0 || isLaying {
|
||||
return isTransition, isLaying, nil
|
||||
}
|
||||
|
||||
category, err := s.resolveRecordingCategory(ctx, recording)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to resolve recording category for cut-over override (recording=%d): %+v", recording.Id, err)
|
||||
return isTransition, isLaying, fiber.NewError(fiber.StatusInternalServerError, "Gagal memvalidasi status laying recording")
|
||||
}
|
||||
if category != strings.ToUpper(string(utils.ProjectFlockCategoryLaying)) {
|
||||
return isTransition, isLaying, nil
|
||||
}
|
||||
|
||||
hasChickinDate, err := s.hasProjectFlockKandangChickinDate(ctx, recording.ProjectFlockKandangId, chickinAvailabilityCache)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to resolve chickin date for cut-over override (project_flock_kandang=%d): %+v", recording.ProjectFlockKandangId, err)
|
||||
return isTransition, isLaying, fiber.NewError(fiber.StatusInternalServerError, "Gagal memvalidasi status laying recording")
|
||||
}
|
||||
if !hasChickinDate {
|
||||
return isTransition, isLaying, nil
|
||||
}
|
||||
|
||||
return false, true, nil
|
||||
}
|
||||
|
||||
func (s *recordingService) hasProjectFlockKandangChickinDate(ctx context.Context, projectFlockKandangID uint, cache map[uint]bool) (bool, error) {
|
||||
if projectFlockKandangID == 0 || s.ProjectFlockPopulationRepo == nil {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
if cache != nil {
|
||||
if value, ok := cache[projectFlockKandangID]; ok {
|
||||
return value, nil
|
||||
}
|
||||
}
|
||||
|
||||
populations, err := s.ProjectFlockPopulationRepo.GetByProjectFlockKandangID(ctx, projectFlockKandangID)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
hasChickinDate := false
|
||||
for _, pop := range populations {
|
||||
if pop.ProjectChickin != nil && !pop.ProjectChickin.ChickInDate.IsZero() {
|
||||
hasChickinDate = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if cache != nil {
|
||||
cache[projectFlockKandangID] = hasChickinDate
|
||||
}
|
||||
|
||||
return hasChickinDate, nil
|
||||
}
|
||||
|
||||
func (s *recordingService) evaluatePopulationMutationState(ctx context.Context, recording *entity.Recording) (bool, bool, bool, bool, *entity.LayingTransfer, time.Time, error) {
|
||||
if recording == nil || recording.ProjectFlockKandangId == 0 || s.TransferLayingRepo == nil {
|
||||
return true, false, false, false, nil, time.Time{}, nil
|
||||
|
||||
Reference in New Issue
Block a user