recreate calendar range when an event is deleted

This commit is contained in:
Julien Fastré 2022-07-01 13:43:30 +02:00
parent 0276ec1bc7
commit 87403e509f
9 changed files with 160 additions and 19 deletions

View File

@ -80,12 +80,13 @@ class CalendarController extends AbstractController
*
* @Route("/{_locale}/calendar/{id}/delete", name="chill_calendar_calendar_delete")
*/
public function deleteAction(Request $request, int $id)
public function deleteAction(Request $request, Calendar $entity)
{
$view = null;
$em = $this->getDoctrine()->getManager();
[$user, $accompanyingPeriod] = $this->getEntity($request);
$accompanyingPeriod = $entity->getAccompanyingPeriod();
$user = null; // TODO legacy code ? remove it ?
if ($accompanyingPeriod instanceof AccompanyingPeriod) {
$view = '@ChillCalendar/Calendar/confirm_deleteByAccompanyingCourse.html.twig';
@ -93,14 +94,7 @@ class CalendarController extends AbstractController
$view = '@ChillCalendar/Calendar/confirm_deleteByUser.html.twig';
}
/** @var Calendar $entity */
$entity = $em->getRepository(\Chill\CalendarBundle\Entity\Calendar::class)->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Calendar entity.');
}
$form = $this->createDeleteForm($id, $user, $accompanyingPeriod);
$form = $this->createDeleteForm($entity->getId(), $user, $accompanyingPeriod);
if ($request->getMethod() === Request::METHOD_DELETE) {
$form->handleRequest($request);
@ -119,7 +113,7 @@ class CalendarController extends AbstractController
$params = $this->buildParamsToUrl($user, $accompanyingPeriod);
return $this->redirectToRoute('chill_calendar_calendar_list', $params);
return $this->redirectToRoute('chill_calendar_calendar_list_by_period', $params);
}
}

View File

@ -149,9 +149,7 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface
private ?User $mainUser = null;
/**
* @ORM\ManyToMany(
* targetEntity="Chill\PersonBundle\Entity\Person",
* cascade={"persist", "remove", "merge", "detach"})
* @ORM\ManyToMany(targetEntity="Chill\PersonBundle\Entity\Person")
* @ORM\JoinTable(name="chill_calendar.calendar_to_persons")
* @Serializer\Groups({"calendar:read", "read"})
*/
@ -164,9 +162,7 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface
private PrivateCommentEmbeddable $privateComment;
/**
* @ORM\ManyToMany(
* targetEntity="Chill\ThirdPartyBundle\Entity\ThirdParty",
* cascade={"persist", "remove", "merge", "detach"})
* @ORM\ManyToMany(targetEntity="Chill\ThirdPartyBundle\Entity\ThirdParty")
* @ORM\JoinTable(name="chill_calendar.calendar_to_thirdparties")
* @Serializer\Groups({"calendar:read", "read"})
*/

View File

@ -13,6 +13,7 @@ namespace Chill\CalendarBundle\Messenger\Doctrine;
use Chill\CalendarBundle\Entity\Calendar;
use Chill\CalendarBundle\Messenger\Message\CalendarMessage;
use Chill\CalendarBundle\Messenger\Message\CalendarRemovedMessage;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Security\Core\Security;
@ -44,7 +45,14 @@ class CalendarEntityListener
public function postRemove(Calendar $calendar, LifecycleEventArgs $args): void
{
// TODO
if (!$calendar->preventEnqueueChanges) {
$this->messageBus->dispatch(
new CalendarRemovedMessage(
$calendar,
$this->security->getUser()
)
);
}
}
public function postUpdate(Calendar $calendar, LifecycleEventArgs $args): void

View File

@ -39,7 +39,7 @@ class CalendarRangeRemoveToRemoteHandler implements MessageHandlerInterface
$this->remoteCalendarConnector->removeCalendarRange(
$calendarRangeRemovedMessage->getRemoteId(),
$calendarRangeRemovedMessage->getRemoteAttributes(),
$this->userRepository->find($calendarRangeRemovedMessage->getCalendarRangeUserId())
$this->userRepository->find($calendarRangeRemovedMessage->getCalendarUserId())
);
}
}

View File

@ -0,0 +1,49 @@
<?php
namespace Chill\CalendarBundle\Messenger\Handler;
use Chill\CalendarBundle\Messenger\Message\CalendarRemovedMessage;
use Chill\CalendarBundle\RemoteCalendar\Connector\RemoteCalendarConnectorInterface;
use Chill\CalendarBundle\Repository\CalendarRangeRepository;
use Chill\MainBundle\Repository\UserRepositoryInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
/**
* Handle the deletion of calendar
*
* @AsMessageHandler
*/
class CalendarRemoveHandler implements MessageHandlerInterface
{
private RemoteCalendarConnectorInterface $remoteCalendarConnector;
private CalendarRangeRepository $calendarRangeRepository;
private UserRepositoryInterface $userRepository;
/**
* @param RemoteCalendarConnectorInterface $remoteCalendarConnector
* @param CalendarRangeRepository $calendarRangeRepository
* @param UserRepositoryInterface $userRepository
*/
public function __construct(RemoteCalendarConnectorInterface $remoteCalendarConnector, CalendarRangeRepository $calendarRangeRepository, UserRepositoryInterface $userRepository)
{
$this->remoteCalendarConnector = $remoteCalendarConnector;
$this->calendarRangeRepository = $calendarRangeRepository;
$this->userRepository = $userRepository;
}
public function __invoke(CalendarRemovedMessage $message)
{
if (null !== $message->getAssociatedCalendarRangeId()) {
$associatedRange = $this->calendarRangeRepository->find($message->getAssociatedCalendarRangeId());
} else {
$associatedRange = null;
}
$this->remoteCalendarConnector->removeCalendar(
$message->getRemoteId(),
$message->getRemoteAttributes(),
$this->userRepository->find($message->getCalendarUserId()),
$associatedRange
);
}
}

View File

@ -0,0 +1,73 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\CalendarBundle\Messenger\Message;
use Chill\CalendarBundle\Entity\Calendar;
use Chill\MainBundle\Entity\User;
class CalendarRemovedMessage
{
private ?int $byUserId = null;
private int $calendarUserId;
private array $remoteAttributes;
private string $remoteId;
private ?int $associatedCalendarRangeId = null;
public function __construct(Calendar $calendar, ?User $byUser)
{
$this->remoteId = $calendar->getRemoteId();
$this->remoteAttributes = $calendar->getRemoteAttributes();
$this->calendarUserId = $calendar->getMainUser()->getId();
if ($calendar->hasCalendarRange()) {
$this->associatedCalendarRangeId = $calendar->getCalendarRange()->getId();
}
if (null !== $byUser) {
$this->byUserId = $byUser->getId();
}
}
public function getByUserId(): ?int
{
return $this->byUserId;
}
public function getCalendarUserId(): ?int
{
return $this->calendarUserId;
}
public function getRemoteAttributes(): array
{
return $this->remoteAttributes;
}
public function getRemoteId(): string
{
return $this->remoteId;
}
/**
* @return int|null
*/
public function getAssociatedCalendarRangeId(): ?int
{
return $this->associatedCalendarRangeId;
}
}

View File

@ -136,6 +136,20 @@ class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface
}
}
public function removeCalendar(string $remoteId, array $remoteAttributes, User $user, ?CalendarRange $associatedCalendarRange = null): void
{
if ('' === $remoteId) {
return;
}
$this->removeEvent($remoteId, $user);
if (null !== $associatedCalendarRange) {
$this->syncCalendarRange($associatedCalendarRange);
}
}
public function removeCalendarRange(string $remoteId, array $remoteAttributes, User $user): void
{
if ('' === $remoteId) {

View File

@ -36,6 +36,11 @@ class NullRemoteCalendarConnector implements RemoteCalendarConnectorInterface
return [];
}
public function removeCalendar(string $remoteId, array $remoteAttributes, User $user, ?CalendarRange $associatedCalendarRange = null): void
{
}
public function removeCalendarRange(string $remoteId, array $remoteAttributes, User $user): void
{
}

View File

@ -41,6 +41,8 @@ interface RemoteCalendarConnectorInterface
public function removeCalendarRange(string $remoteId, array $remoteAttributes, User $user): void;
public function removeCalendar(string $remoteId, array $remoteAttributes, User $user, ?CalendarRange $associatedCalendarRange = null): void;
/**
* @param array<array{inviteId: int, userId: int, userEmail: int, userLabel: string}> $oldInvites
*/