Skip to content

Commit

Permalink
Merge pull request #485 from AOEpeople/update/#263226-mealstate-refac…
Browse files Browse the repository at this point in the history
…toring

Refactored MealState computation
  • Loading branch information
MalibusParty authored Jul 9, 2024
2 parents 2c1e8fe + 51cb729 commit 59a479a
Show file tree
Hide file tree
Showing 20 changed files with 194 additions and 125 deletions.
42 changes: 0 additions & 42 deletions src/Mealz/MealBundle/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use App\Mealz\MealBundle\Entity\Day;
use App\Mealz\MealBundle\Entity\DishVariation;
use App\Mealz\MealBundle\Entity\Meal;
use App\Mealz\MealBundle\Entity\Participant;
use App\Mealz\MealBundle\Entity\Slot;
use App\Mealz\MealBundle\Service\ApiService;
use App\Mealz\MealBundle\Service\DishService;
Expand Down Expand Up @@ -262,24 +261,12 @@ public function getTransactionData(): JsonResponse
]);
}

/**
* @Security("is_granted('ROLE_USER')")
*/
public function getMealStateOnDay(Meal $meal): JsonResponse
{
$profile = $this->getProfile();
$participant = $this->participationSrv->getParticipationByMealAndUser($meal, $profile);

return new JsonResponse($this->getMealState($meal, $profile, $participant), Response::HTTP_OK);
}

private function convertMealForDashboard(Meal $meal, float $participationCount, ?Profile $profile): array
{
$description = null;
$parentId = null;
$participationId = null;
$isOffering = false;
$mealState = 'open';

if (false === ($meal->getDish() instanceof DishVariation)) {
$description = [
Expand All @@ -296,7 +283,6 @@ private function convertMealForDashboard(Meal $meal, float $participationCount,
$participationId = $participation->getId();
}
$isOffering = $this->offerSrv->isOfferingMeal($profile, $meal);
$mealState = $this->getMealState($meal, $profile, $participation);
}

$reachedLimit = $meal->getParticipationLimit() > 0.0 ? $participationCount >= $meal->getParticipationLimit() : false;
Expand All @@ -323,7 +309,6 @@ private function convertMealForDashboard(Meal $meal, float $participationCount,
'isParticipating' => $participationId,
'hasOffers' => $this->offerSrv->getOfferCountByMeal($meal) > 0,
'isOffering' => $isOffering,
'mealState' => $mealState,
];
}

Expand All @@ -348,33 +333,6 @@ private function addMealWithVariations(Meal $meal, float $participationCount, ?P
$meals[$parent->getId()]['variations'][$meal->getId()] = $this->convertMealForDashboard($meal, $participationCount, $profile);
}

private function getMealState(Meal $meal, Profile $profile, ?Participant $participant): string
{
if (true === $meal->isLocked() && true === $meal->isOpen()) {
$isOffering = $this->offerSrv->isOfferingMeal($profile, $meal);
if ($isOffering) {
return 'offering';
} elseif (null !== $participant) {
return 'offerable';
} elseif ($this->offerSrv->getOfferCountByMeal($meal) > 0) {
return 'tradeable';
}
}

return $this->getMealStateForOpenMeal($meal, $participant);
}

private function getMealStateForOpenMeal(Meal $meal, ?Participant $participant): string
{
if ($this->apiSrv->isMealOpen($meal)) {
return 'open';
} elseif (false === $meal->isLocked() && true === $meal->isOpen() && null !== $participant) {
return 'offerable';
}

return 'disabled';
}

public function getGuestData(string $guestInvitationId, ParticipationCountService $partCountSrv): JsonResponse
{
$guestInvitation = $this->guestPartiSrv->getGuestInvitationById($guestInvitationId);
Expand Down
22 changes: 20 additions & 2 deletions src/Mealz/MealBundle/Controller/ParticipantController.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ public function joinMeal(Request $request): JsonResponse
[
'slotId' => $slotID,
'participantId' => $result['participant']->getId(),
'mealState' => $this->participationHelper->getMealState($meal),
],
Response::HTTP_OK
);
Expand Down Expand Up @@ -137,7 +136,6 @@ public function leaveMeal(Request $request, EntityManagerInterface $entityManage

return new JsonResponse([
'slotId' => $slotID,
'mealState' => $this->participationHelper->getMealState($meal),
], Response::HTTP_OK);
}

Expand Down Expand Up @@ -356,6 +354,26 @@ public function getCombiForMeal(Meal $meal): JsonResponse
return new JsonResponse($participant->getCombinedDishes()->toArray(), Response::HTTP_OK);
}

/**
* Determines wether the user is participating in a specific meal.
* Returns null on error, -1 for not participating,
* the id of the participation if they are participating.
*/
public function isParticipating(Meal $meal): JsonResponse
{
$profile = $this->getProfile();
if (null === $profile) {
return new JsonResponse(null, Response::HTTP_FORBIDDEN);
}

$participant = $meal->getParticipant($profile);
if (null === $participant) {
return new JsonResponse(-1, Response::HTTP_OK);
}

return new JsonResponse($participant->getId(), Response::HTTP_OK);
}

private function generateResponse(string $route, string $action, Participant $participant): JsonResponse
{
return new JsonResponse([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function onUpdate(ParticipationUpdateEvent $event): void
'weekId' => $eventMeal->getDay()->getWeek()->getId(),
'dayId' => $eventMeal->getDay()->getId(),
'meals' => [],
'participant' => $event->getParticipant()->getProfile()->getFullName(),
];

foreach ($meals as $meal) {
Expand Down
12 changes: 0 additions & 12 deletions src/Mealz/MealBundle/Helper/ParticipationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,6 @@ public function getParticipationMealData(Participant $participant): array
return $participationData;
}

public function getMealState(Meal $meal): string
{
$mealState = 'open';
if (true === $meal->isLocked() && true === $meal->isOpen()) {
$mealState = 'offerable';
} elseif ($this->getReachedLimit($meal)) {
$mealState = 'disabled';
}

return $mealState;
}

protected function compareNameOfParticipants(Participant $participant1, Participant $participant2): int
{
$result = strcasecmp($participant1->getProfile()->getName(), $participant2->getProfile()->getName());
Expand Down
6 changes: 3 additions & 3 deletions src/Mealz/MealBundle/Resources/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ MealzMealBundle_api_participant_remove:
defaults: { _controller: App\Mealz\MealBundle\Controller\ParticipantController::remove }
methods: [DELETE]

MealzMealBundle_Meal_mealstate:
path: /api/mealstate/{meal}
defaults: { _controller: App\Mealz\MealBundle\Controller\ApiController::getMealStateOnDay }
MealzMealBundle_api_is_participanting:
path: /api/participation/{meal}
defaults: { _controller: App\Mealz\MealBundle\Controller\ParticipantController::isParticipating}
methods: [GET]

MealzMealBundle_Meal_count:
Expand Down
9 changes: 0 additions & 9 deletions src/Mealz/MealBundle/Service/ApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,6 @@ public function hasCombiReachedLimit(Day $day): bool
return $hasReachedLimit;
}

public function isMealOpen(Meal $meal): bool
{
return false === $meal->isLocked()
&& true === $meal->isOpen()
&& false === $meal->hasReachedParticipationLimit()
&& (false === $meal->isCombinedMeal()
|| false === $this->hasCombiReachedLimit($meal->getDay()));
}

private function getEventParticipants(Day $day): array
{
return $this->eventPartSrv->getParticipants($day);
Expand Down
1 change: 1 addition & 0 deletions src/Mealz/UserBundle/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function getUserData(): JsonResponse
[
'roles' => [Role::ROLE_GUEST],
'name' => null,
'fullname' => null,
'balance' => 0,
]
);
Expand Down
5 changes: 3 additions & 2 deletions src/Mealz/UserBundle/Entity/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,15 @@ public function __toString()
}

/**
* @return (string|string[])[]
* @return (string|string|string[])[]
*
* @psalm-return array{user: string, roles: array<string>}
* @psalm-return array{user: string, fullname: string, roles: array<string>}
*/
public function jsonSerialize(): array
{
return [
'user' => $this->username,
'fullname' => $this->getFullName(),
'roles' => $this->getRoles(),
];
}
Expand Down
1 change: 0 additions & 1 deletion src/Resources/src/api/deleteLeaveMeal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { ref } from 'vue';

export type LeaveMeal = {
slotId: number;
mealState: string;
};

export async function useLeaveMeal(data: string) {
Expand Down
3 changes: 2 additions & 1 deletion src/Resources/src/api/getDashboardData.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import useApi from '@/api/api';
import { Dictionary } from '../../types/types';
import { ref } from 'vue';
import { MealState } from '@/enums/MealState';

export type Meal = {
variations: Dictionary<Meal> | null;
Expand All @@ -18,7 +19,7 @@ export type Meal = {
dishSlug: string | null;
hasOffers: boolean;
isOffering: boolean;
mealState: string;
mealState: MealState;
};

export type DateTime = {
Expand Down
12 changes: 12 additions & 0 deletions src/Resources/src/api/getIsParticipating.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ref } from 'vue';
import useApi from './api';

export async function getIsParticipating(mealId: number) {
const { response, error, request } = useApi<number>('GET', `/api/participation/${mealId}`);

await request();

const participationId = response.value !== -1 ? response : ref(null);

return { response: participationId, error };
}
12 changes: 0 additions & 12 deletions src/Resources/src/api/getMealState.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/Resources/src/api/getUserData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ref } from 'vue';
export type UserData = {
roles: Array<string>;
user: string | null;
fullname: string | null;
balance: number;
};

Expand Down
1 change: 0 additions & 1 deletion src/Resources/src/api/postJoinMeal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { ref } from 'vue';
export type JoinMeal = {
slotId: number;
participantId: number;
mealState: string;
};

export async function useJoinMeal(data: string) {
Expand Down
35 changes: 19 additions & 16 deletions src/Resources/src/components/dashboard/MealCheckbox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ import useFlashMessage from '@/services/useFlashMessage';
import { IMessage, isMessage } from '@/interfaces/IMessage';
import { FlashMessageType } from '@/enums/FlashMessage';
import { useLockRequests } from '@/services/useLockRequests';
import { MealState } from '@/enums/MealState';
import useMealState from '@/services/useMealState';
const props = defineProps<{
weekID: number | string | undefined;
Expand Down Expand Up @@ -78,6 +80,7 @@ const isCombiBox = props.day.meals[props.mealID].dishSlug === 'combined-dish';
const openPopover = ref(false);
const { receive } = useEventsBus();
const { generateMealState } = useMealState();
receive('openOfferPanel_' + props.mealID, () => {
openPopover.value = true;
Expand All @@ -89,31 +92,31 @@ const checkboxCSS = computed(() => {
if (isParticipating.value === true) {
switch (mealOrVariation.value.mealState) {
case 'disabled':
case MealState.DISABLED:
cssResult += 'border-[0.5px] border-[#ABABAB]';
if (mealOrVariation.value.isLocked === false) {
cssResult += ' bg-[#80909F] cursor-pointer';
} else {
cssResult += ' bg-[#B4C1CE]';
}
return cssResult;
case 'open':
case 'tradeable':
case MealState.OPEN:
case MealState.TRADEABLE:
cssResult += 'bg-primary-4 hover:bg-primary-3 cursor-pointer border-0';
return cssResult;
case 'offering':
case MealState.OFFERING:
cssResult += 'bg-highlight cursor-pointer border-0';
case 'offerable':
case MealState.OFFERABLE:
cssResult += 'bg-primary-4 cursor-pointer border-0';
return cssResult;
}
} else if (isParticipating.value === false) {
switch (mealOrVariation.value.mealState) {
case 'disabled':
case MealState.DISABLED:
cssResult += 'bg-[#EDEDED] border-[0.5px] border-[#ABABAB]';
return cssResult;
case 'tradeable':
case 'open':
case MealState.TRADEABLE:
case MealState.OPEN:
cssResult += 'cursor-pointer bg-[#FAFAFA] border-[0.5px] border-[#ABABAB]';
return cssResult;
}
Expand All @@ -123,7 +126,7 @@ const checkboxCSS = computed(() => {
async function handle() {
// Meal is being offered by someone to be taken over
if (mealOrVariation.value.hasOffers === true && mealOrVariation.value.mealState === 'tradeable') {
if (mealOrVariation.value.hasOffers === true && mealOrVariation.value.mealState === MealState.TRADEABLE) {
let slugs = [mealOrVariation.value.dishSlug];
if (isCombiBox === true) {
slugs = getDishSlugs();
Expand All @@ -134,7 +137,7 @@ async function handle() {
}
// Meal is not locked
if (
(mealOrVariation.value.isLocked === false || mealOrVariation.value.mealState === 'tradeable') &&
(mealOrVariation.value.isLocked === false || mealOrVariation.value.mealState === MealState.TRADEABLE) &&
isLocked(String(props.dayID)) === false
) {
addLock(String(props.dayID));
Expand All @@ -152,10 +155,10 @@ async function handle() {
removeLock(String(props.dayID));
} else if (isLocked(String(props.dayID)) === false) {
addLock(String(props.dayID));
if (mealOrVariation.value.mealState === 'offerable') {
if (mealOrVariation.value.mealState === MealState.OFFERABLE) {
addLock(String(props.dayID));
await sendOffer();
} else if (mealOrVariation.value.mealState === 'offering') {
} else if (mealOrVariation.value.mealState === MealState.OFFERING) {
addLock(String(props.dayID));
await cancelOffer();
}
Expand Down Expand Up @@ -197,7 +200,7 @@ async function joinMeal(dishSlugs) {
if (error.value === false) {
day.activeSlot = (response.value as JoinMeal).slotId;
mealOrVariation.value.isParticipating = (response.value as JoinMeal).participantId;
mealOrVariation.value.mealState = (response.value as JoinMeal).mealState;
mealOrVariation.value.mealState = generateMealState(props.meal);
} else if (isMessage(response.value) === true) {
sendFlashMessage({
type: FlashMessageType.ERROR,
Expand All @@ -223,7 +226,7 @@ async function leaveMeal() {
const { response, error } = await useLeaveMeal(JSON.stringify(data));
if (error.value === false) {
day.activeSlot = response.value.slotId;
mealOrVariation.value.mealState = response.value.mealState;
mealOrVariation.value.mealState = generateMealState(props.meal);
mealOrVariation.value.isParticipating = null;
}
}
Expand All @@ -236,7 +239,7 @@ async function sendOffer() {
const { error } = await useOfferMeal(JSON.stringify(data));
if (error.value === false) {
mealOrVariation.value.mealState = 'offering';
mealOrVariation.value.mealState = MealState.OFFERING;
const { emit } = useEventsBus();
emit('openOfferPanel_' + mealId);
}
Expand All @@ -249,7 +252,7 @@ async function cancelOffer() {
const { error } = await useCancelOffer(JSON.stringify(data));
if (error.value === false) {
mealOrVariation.value.mealState = 'offerable';
mealOrVariation.value.mealState = MealState.OFFERABLE;
}
}
Expand Down
Loading

0 comments on commit 59a479a

Please sign in to comment.