Skip to content

Commit

Permalink
Merge pull request #509 from AOEpeople/bugfix/#265278-servingsize-dis…
Browse files Browse the repository at this point in the history
…able

fixed a bug that prevented setting the servingsize correctly and adde…
  • Loading branch information
MalibusParty authored Aug 15, 2024
2 parents 4188598 + 73d6147 commit fdab970
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 43 deletions.
4 changes: 2 additions & 2 deletions src/Mealz/MealBundle/Controller/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function update(Request $request, Category $category): JsonResponse

return new JsonResponse($category, Response::HTTP_OK);
} catch (Exception $e) {
$this->logger->error('category update error', $this->getTrace($e));
$this->logger->info('category update error', $this->getTrace($e));

return new JsonResponse(['message' => $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR);
}
Expand All @@ -69,7 +69,7 @@ public function delete(Category $category): JsonResponse
$this->em->remove($category);
$this->em->flush();
} catch (Exception $e) {
$this->logger->error('category delete error', $this->getTrace($e));
$this->logger->info('category delete error', $this->getTrace($e));

return new JsonResponse(['message' => $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR);
}
Expand Down
13 changes: 9 additions & 4 deletions src/Mealz/MealBundle/Controller/DishController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(
private readonly DishRepositoryInterface $dishRepository,
private readonly DishService $dishService,
private readonly EntityManagerInterface $em,
private readonly LoggerInterface $logger
private readonly LoggerInterface $logger,
) {
}

Expand Down Expand Up @@ -98,7 +98,7 @@ public function new(Request $request): JsonResponse

return new JsonResponse(null, Response::HTTP_OK);
} catch (Exception $e) {
$this->logger->error('dish create error', $this->getTrace($e));
$this->logger->info('dish create error', $this->getTrace($e));

return new JsonResponse(['message' => $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR);
}
Expand All @@ -121,7 +121,7 @@ public function delete(Dish $dish): JsonResponse

return new JsonResponse(null, Response::HTTP_OK);
} catch (Exception $e) {
$this->logger->error('dish delete error', $this->getTrace($e));
$this->logger->info('dish delete error', $this->getTrace($e));

return new JsonResponse(['message' => $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR);
}
Expand All @@ -134,15 +134,20 @@ public function update(Dish $dish, Request $request): JsonResponse
{
try {
$parameters = json_decode($request->getContent(), true);
$oneServingSize = $dish->hasOneServingSize();

$this->dishService->updateHelper($dish, $parameters);

$this->em->persist($dish);
$this->em->flush();

if (true === isset($parameters['oneServingSize']) && $oneServingSize !== $parameters['oneServingSize']) {
$this->dishService->updateCombisForDish($dish);
}

return new JsonResponse($dish, Response::HTTP_OK);
} catch (Exception $e) {
$this->logger->error('dish update error', $this->getTrace($e));
$this->logger->info('dish update error', $this->getTrace($e));

return new JsonResponse(['message' => $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Mealz/MealBundle/Controller/DishVariationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function new(Request $request, Dish $dish): JsonResponse

return new JsonResponse(null, Response::HTTP_OK);
} catch (Exception $e) {
$this->logger->error('dish variation create error', $this->getTrace($e));
$this->logger->info('dish variation create error', $this->getTrace($e));

return new JsonResponse(['message' => $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR);
}
Expand Down Expand Up @@ -85,7 +85,7 @@ public function update(Request $request, DishVariation $dishVariation): JsonResp

return new JsonResponse($dishVariation, Response::HTTP_OK);
} catch (Exception $e) {
$this->logger->error('dish variation update error', $this->getTrace($e));
$this->logger->info('dish variation update error', $this->getTrace($e));

return new JsonResponse(['message' => $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR);
}
Expand All @@ -108,7 +108,7 @@ public function delete(DishVariation $dishVariation): JsonResponse

return new JsonResponse(null, Response::HTTP_OK);
} catch (Exception $e) {
$this->logger->error('dish variation delete error', $this->getTrace($e));
$this->logger->info('dish variation delete error', $this->getTrace($e));

return new JsonResponse(['message' => $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Mealz/MealBundle/Controller/EventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function update(Request $request, Event $event): JsonResponse

return new JsonResponse($event, Response::HTTP_OK);
} catch (Exception $e) {
$this->logger->error('event update error', $this->getTrace($e));
$this->logger->info('event update error', $this->getTrace($e));

return new JsonResponse(['message' => $e->getMessage(), 500]);
}
Expand All @@ -90,7 +90,7 @@ public function delete(Event $event): JsonResponse

return new JsonResponse(null, Response::HTTP_OK);
} catch (Exception $e) {
$this->logger->error('event delete error', $this->getTrace($e));
$this->logger->info('event delete error', $this->getTrace($e));

return new JsonResponse(['message' => $e->getMessage(), 500]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Mealz/MealBundle/Controller/MealGuestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function joinAsGuest(Request $request): JsonResponse
$participants = $this->guestPartSrv->join($profile, $meals, $slot, $dishSlugs);
$this->triggerJoinEvents($participants);
} catch (Exception $e) {
$this->logger->error('guest join error', $this->getTrace($e));
$this->logger->info('guest join error', $this->getTrace($e));

return new JsonResponse($e->getMessage(), Response::HTTP_BAD_REQUEST);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Mealz/MealBundle/Controller/ParticipantController.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function joinMeal(Request $request): JsonResponse
try {
$result = $this->participationSrv->join($profile, $meal, $slot, $parameters['dishSlugs']);
} catch (Exception $e) {
$this->logger->error('join meal error', $this->getTrace($e));
$this->logger->info('join meal error', $this->getTrace($e));

return new JsonResponse(['message' => '402: ' . $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR);
}
Expand Down Expand Up @@ -151,7 +151,7 @@ public function updateCombinedMeal(
} catch (ParticipationException $pex) {
return new JsonResponse(['message' => $pex->getMessage()], Response::HTTP_UNPROCESSABLE_ENTITY);
} catch (Exception $e) {
$this->logger->error('update combined meal error', $this->getTrace($e));
$this->logger->info('update combined meal error', $this->getTrace($e));

return new JsonResponse(['message' => 'unexpected error'], Response::HTTP_INTERNAL_SERVER_ERROR);
}
Expand Down Expand Up @@ -289,7 +289,7 @@ public function add(Profile $profile, Meal $meal, Request $request): JsonRespons
'booked' => $participationData,
], Response::HTTP_OK);
} catch (Exception $e) {
$this->logger->error('error adding participant to meal', $this->getTrace($e));
$this->logger->info('error adding participant to meal', $this->getTrace($e));

return new JsonResponse(['message' => $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR);
}
Expand Down Expand Up @@ -321,7 +321,7 @@ public function remove(EntityManagerInterface $entityManager, Profile $profile,
'booked' => $participationData,
], Response::HTTP_OK);
} catch (Exception $e) {
$this->logger->error('error removing participant from meal', $this->getTrace($e));
$this->logger->info('error removing participant from meal', $this->getTrace($e));

return new JsonResponse(['message' => $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Mealz/MealBundle/Controller/SlotController.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function update(Request $request, Slot $slot): JsonResponse

return new JsonResponse($slot, Response::HTTP_OK);
} catch (Exception $e) {
$this->logger->error('slot update error', $this->getTrace($e));
$this->logger->info('slot update error', $this->getTrace($e));

return new JsonResponse(['message' => $e->getMessage()], Response::HTTP_METHOD_NOT_ALLOWED);
}
Expand All @@ -79,7 +79,7 @@ public function delete(Slot $slot): JsonResponse
$this->em->persist($slot);
$this->em->flush();
} catch (Exception $e) {
$this->logger->error('slot delete error', $this->getTrace($e));
$this->logger->info('slot delete error', $this->getTrace($e));

return new JsonResponse(['message' => $e->getMessage()], Response::HTTP_METHOD_NOT_ALLOWED);
}
Expand Down Expand Up @@ -111,7 +111,7 @@ public function new(Request $request): JsonResponse
$this->em->persist($slot);
$this->em->flush();
} catch (Exception $e) {
$this->logger->error('slot create error', $this->getTrace($e));
$this->logger->info('slot create error', $this->getTrace($e));

return new JsonResponse(['message' => $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR);
}
Expand Down
5 changes: 5 additions & 0 deletions src/Mealz/MealBundle/Entity/Week.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,9 @@ public function jsonSerialize(): array
'enabled' => $this->isEnabled(),
];
}

public function __toString()
{
return (string) $this->getCalendarWeek();
}
}
38 changes: 17 additions & 21 deletions src/Mealz/MealBundle/Repository/DishRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use App\Mealz\MealBundle\Entity\Dish;
use App\Mealz\MealBundle\Entity\Meal;
use App\Mealz\MealBundle\Entity\Participant;
use App\Mealz\MealBundle\EventListener\LocalisationListener;
use DateTime;
use Doctrine\DBAL\Types\Types;
Expand Down Expand Up @@ -89,30 +88,27 @@ public function hasDishAssociatedCombiMealsInFuture(Dish $dish): bool
{
$today = new DateTime('today');
$formattedDate = $today->format('Y-m-d') . ' 12:00:00.000000';
if (true === $dish->hasVariations()) {
$variations = $dish->getVariations();
foreach ($variations as $variation) {
if (true === $this->hasDishAssociatedCombiMealsInFuture($variation)) {
return true;
}
}
}

$mealRepo = $this->getEntityManager()->getRepository(Meal::class);
$participantRepo = $this->getEntityManager()->getRepository(Participant::class);
$dayQuery = $mealRepo->createQueryBuilder('m')
->select('DISTINCT d.id')
->join('m.day', 'd')
->join('m.dish', 'g')
$query = $mealRepo->createQueryBuilder('m2')
->select('COUNT(m2)')
->join('m2.day', 'd')
->join('m2.dish', 'g')
->join(Meal::class, 'm', 'WITH', 'm.day = d.id')
->join('m2.participants', 'p')
->where('g.slug = :slug')
->setParameter('slug', 'combined-dish');

$mealQuery = $mealRepo->createQueryBuilder('m2')
->select('m2.id')
->where('m2.day IN (' . $dayQuery->getDQL() . ')')
->andWhere('m2.dish = :dishId')
->andWhere('m2.dateTime >= :now')
->setParameter('dishId', $dish->getId())
->setParameter('now', $formattedDate);

$query = $participantRepo->createQueryBuilder('p')
->select('COUNT(p.id)')
->join('p.meal', 'm3')
->where('m3.id IN (' . $mealQuery->getDQL() . ')')
->andWhere('m.dish = :dish_id')
->andWhere('m.dateTime >= :now')
->setParameter('slug', 'combined-dish')
->setParameter('dishId', $dish->getId())
->setParameter('dish_id', $dish->getId(), Types::INTEGER)
->setParameter('now', $formattedDate);

return 0 < $query->getQuery()->getSingleScalarResult();
Expand Down
19 changes: 19 additions & 0 deletions src/Mealz/MealBundle/Repository/MealRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Mealz\MealBundle\Repository;

use App\Mealz\MealBundle\Entity\Dish;
use App\Mealz\MealBundle\Entity\Meal;
use DateTime;
use Doctrine\DBAL\Types\Types;
Expand Down Expand Up @@ -155,4 +156,22 @@ public function getLockedMeals(): array

return $queryBuilder->getQuery()->getResult();
}

/**
* Returns all CombiMeals that are in the future and contain a specific dish.
*
* @return Meal[]
*/
public function getFutureMealsForDish(Dish $dish): array
{
$queryBuilder = $this->createQueryBuilder('m')
->select('m')
->join('m.day', 'd')
->andWhere('m.dish = :dish_id')
->andWhere('m.dateTime >= :now')
->setParameter('dish_id', $dish->getId(), Types::INTEGER)
->setParameter('now', new DateTime('now'), Types::DATETIME_MUTABLE);

return $queryBuilder->getQuery()->getResult();
}
}
8 changes: 8 additions & 0 deletions src/Mealz/MealBundle/Repository/MealRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Mealz\MealBundle\Repository;

use App\Mealz\MealBundle\Entity\Dish;
use App\Mealz\MealBundle\Entity\Meal;
use DateTime;
use Doctrine\Common\Collections\Selectable;
Expand Down Expand Up @@ -60,4 +61,11 @@ public function getOutdatedMeals(): array;
* @return Meal[]
*/
public function getLockedMeals(): array;

/**
* Returns all Meals that are in the future and contain a specific dish.
*
* @return Meal[]
*/
public function getFutureMealsForDish(Dish $dish): array;
}
37 changes: 34 additions & 3 deletions src/Mealz/MealBundle/Service/DishService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
use App\Mealz\MealBundle\Entity\DishVariation;
use App\Mealz\MealBundle\Entity\Meal;
use App\Mealz\MealBundle\Enum\Diet;
use App\Mealz\MealBundle\Event\WeekUpdateEvent;
use App\Mealz\MealBundle\Repository\CategoryRepository;
use App\Mealz\MealBundle\Repository\DishRepository;
use App\Mealz\MealBundle\Repository\MealRepositoryInterface;
use Exception;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class DishService
{
Expand All @@ -26,19 +29,25 @@ class DishService
private ApiService $apiService;
private CategoryRepository $categoryRepository;
private DishRepository $dishRepository;
private EventDispatcherInterface $eventDispatcher;
private MealRepositoryInterface $mealRepo;

public function __construct(
int $newFlagThreshold,
string $dishConsCountPeriod,
ApiService $apiService,
CategoryRepository $categoryRepository,
DishRepository $dishRepository
DishRepository $dishRepository,
EventDispatcherInterface $eventDispatcher,
MealRepositoryInterface $mealRepo,
) {
$this->newFlagThreshold = $newFlagThreshold;
$this->dishConsCountPeriod = $dishConsCountPeriod;
$this->apiService = $apiService;
$this->categoryRepository = $categoryRepository;
$this->dishRepository = $dishRepository;
$this->eventDispatcher = $eventDispatcher;
$this->mealRepo = $mealRepo;
}

/**
Expand Down Expand Up @@ -92,6 +101,28 @@ public function getUniqueDishesFromMeals(array $meals): array
return $uniqueMeals;
}

public function updateCombisForDish(Dish $dish): void
{
$meals = $this->mealRepo->getFutureMealsForDish($dish);

if (true === $dish->hasVariations()) {
foreach ($dish->getVariations() as $variation) {
$res = $this->mealRepo->getFutureMealsForDish($variation);
$meals = array_merge($meals, $res);
}
}

$weeksToUpdate = array_map(
fn ($meal) => $meal->getDay()->getWeek(),
array_unique($meals)
);
$uniqueWeeks = array_unique($weeksToUpdate);

foreach ($uniqueWeeks as $week) {
$this->eventDispatcher->dispatch(new WeekUpdateEvent($week, false));
}
}

private function setDietIfValid(Dish $dish, array $parameters): void
{
if (true === $this->apiService->isParamValid($parameters, 'diet', 'string')) {
Expand Down Expand Up @@ -119,7 +150,7 @@ private function setDescriptionIfValid(Dish $dish, array $parameters): void
private function setServingSizeIfValid(Dish $dish, array $parameters): void
{
if (true === $this->apiService->isParamValid($parameters, 'oneServingSize', 'boolean')) {
if (true === $this->hasCombiMealsInFuture($dish)) {
if (true === $parameters['oneServingSize'] && true === $this->hasBookedCombiMealsInFuture($dish)) {
throw new Exception('204: The servingSize cannot be adjusted, because there are booked combi-meals');
}

Expand All @@ -143,7 +174,7 @@ private function setTitleIfValid(Dish $dish, array $parameters): void
}
}

private function hasCombiMealsInFuture(Dish $dish): bool
private function hasBookedCombiMealsInFuture(Dish $dish): bool
{
return $this->dishRepository->hasDishAssociatedCombiMealsInFuture($dish);
}
Expand Down

0 comments on commit fdab970

Please sign in to comment.