Skip to content

Commit

Permalink
fixed cypress tests (meals weren't deleted properly)
Browse files Browse the repository at this point in the history
  • Loading branch information
IrisOlfermann committed Feb 6, 2025
1 parent 9136182 commit e69cff5
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/Mealz/MealBundle/Controller/MealAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,17 +192,7 @@ private function handleDay(array $day): void
$dayEntity = $this->getDayEntity($day['id']);
$this->updateDaySettings($dayEntity, $day);
$this->updateEvents($dayEntity, $day['events'] ?? []);
$this->updateMeals($dayEntity, $day['meals']);
}

private function getDayEntity(int $dayId): Day
{
$dayEntity = $this->dayRepository->find($dayId);
if (null === $dayEntity) {
throw new Exception('105: day not found');
}

return $dayEntity;
$this->updateMeals($dayEntity, $day['meals'], 3);
}

private function handleNewDay($dayData, Day $day): void
Expand All @@ -213,7 +203,7 @@ private function handleNewDay($dayData, Day $day): void

$this->updateDaySettings($day, $dayData);
$this->updateEvents($day, $dayData['events']);
$this->updateMeals($day, $dayData['meals']);
$this->updateMeals($day, $dayData['meals'], 2);
}

private function updateDaySettings(Day $day, array $dayData): void
Expand Down Expand Up @@ -251,6 +241,16 @@ private function addNewEvents(Day $day, array $newEvents, array $existingEventId
}
}

private function getDayEntity(int $dayId): Day
{
$dayEntity = $this->dayRepository->find($dayId);
if (null === $dayEntity) {
throw new Exception('105: day not found');
}

return $dayEntity;
}

private function removeOldEvents(Day $dayEntity, array $newEventIds): void
{
foreach ($dayEntity->getEvents() as $existingEvent) {
Expand All @@ -261,11 +261,15 @@ private function removeOldEvents(Day $dayEntity, array $newEventIds): void
}
}

private function updateMeals(Day $day, array $mealCollection): void
private function updateMeals(Day $day, array $mealCollection, int $count): void
{
if (3 < count($mealCollection)) {
if ($count < count($mealCollection)) {
throw new Exception('106: too many meals requested');
}
// when updating a day remove old meals
if (3 === $count) {
$this->dayService->removeUnusedMeals($day, $mealCollection);
}

foreach ($mealCollection as $mealArr) {
$this->mealAdminHelper->handleMealArray($mealArr, $day);
Expand Down

0 comments on commit e69cff5

Please sign in to comment.