fix relation inside calendar, bootstrap messenger for handling create and update calendar entity

This commit is contained in:
Julien Fastré 2022-05-25 10:22:30 +02:00
parent b22f361368
commit f962b7543f
10 changed files with 155 additions and 15 deletions

View File

@ -37,11 +37,13 @@ use Symfony\Component\Validator\Mapping\ClassMetadata;
use function in_array;
/**
* @ORM\Table(name="chill_calendar.calendar")
* @ORM\Table(name="chill_calendar.calendar", indexes={@ORM\Index(name="idx_calendar_remote", columns={"remoteId"})}))
* @ORM\Entity(repositoryClass=CalendarRepository::class)
*/
class Calendar implements TrackCreationInterface, TrackUpdateInterface
{
use RemoteCalendarTrait;
use TrackCreationTrait;
use TrackUpdateTrait;
@ -64,7 +66,7 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface
private ?Activity $activity = null;
/**
* @ORM\ManyToOne(targetEntity="CalendarRange", inversedBy="calendars")
* @ORM\OneToOne(targetEntity="CalendarRange", inversedBy="calendar")
* @Serializer\Groups({"calendar:read", "read"})
*/
private ?CalendarRange $calendarRange = null;
@ -405,6 +407,8 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface
{
$this->calendarRange = $calendarRange;
$this->calendarRange->setCalendar($this);
return $this;
}

View File

@ -17,8 +17,6 @@ use Chill\MainBundle\Doctrine\Model\TrackUpdateInterface;
use Chill\MainBundle\Doctrine\Model\TrackUpdateTrait;
use Chill\MainBundle\Entity\User;
use DateTimeImmutable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
@ -35,10 +33,9 @@ class CalendarRange implements TrackCreationInterface, TrackUpdateInterface
use TrackUpdateTrait;
/**
* @ORM\OneToMany(targetEntity=Calendar::class,
* mappedBy="calendarRange")
* @ORM\OneToOne(targetEntity=Calendar::class, mappedBy="calendarRange")
*/
private Collection $calendars;
private Calendar $calendar;
/**
* @ORM\Column(type="datetimetz_immutable")
@ -66,13 +63,13 @@ class CalendarRange implements TrackCreationInterface, TrackUpdateInterface
*/
private ?User $user = null;
//TODO Lieu
public function __construct()
public function getCalendar(): Calendar
{
$this->calendars = new ArrayCollection();
return $this->calendar;
}
//TODO Lieu
public function getEndDate(): ?DateTimeImmutable
{
return $this->endDate;
@ -93,6 +90,14 @@ class CalendarRange implements TrackCreationInterface, TrackUpdateInterface
return $this->user;
}
/**
* @internal use {@link (Calendar::setCalendarRange)} instead
*/
public function setCalendar(Calendar $calendar): void
{
$this->calendar = $calendar;
}
public function setEndDate(DateTimeImmutable $endDate): self
{
$this->endDate = $endDate;

View File

@ -12,11 +12,51 @@ declare(strict_types=1);
namespace Chill\CalendarBundle\Messenger\Doctrine;
use Chill\CalendarBundle\Entity\Calendar;
use Chill\CalendarBundle\Messenger\Message\CalendarMessage;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Security\Core\Security;
class CalendarEntityListener
{
public function postPersistCalendar(Calendar $calendar, LifecycleEventArgs $args): void
private MessageBusInterface $messageBus;
private Security $security;
public function __construct(MessageBusInterface $messageBus, Security $security)
{
$this->messageBus = $messageBus;
$this->security = $security;
}
public function postPersist(Calendar $calendar, LifecycleEventArgs $args): void
{
if (!$calendar->preventEnqueueChanges) {
$this->messageBus->dispatch(
new CalendarMessage(
$calendar,
CalendarMessage::CALENDAR_PERSIST,
$this->security->getUser()
)
);
}
}
public function postRemove(Calendar $calendar, LifecycleEventArgs $args): void
{
// TODO
}
public function postUpdate(Calendar $calendar, LifecycleEventArgs $args): void
{
if (!$calendar->preventEnqueueChanges) {
$this->messageBus->dispatch(
new CalendarMessage(
$calendar,
CalendarMessage::CALENDAR_UPDATE,
$this->security->getUser()
)
);
}
}
}

View File

@ -12,6 +12,7 @@ declare(strict_types=1);
namespace Chill\CalendarBundle\Messenger\Message;
use Chill\CalendarBundle\Entity\Calendar;
use Chill\MainBundle\Entity\User;
class CalendarMessage
{
@ -21,11 +22,14 @@ class CalendarMessage
private string $action;
private int $byUserId;
private int $calendarId;
public function __construct(Calendar $calendar, string $action)
public function __construct(Calendar $calendar, string $action, User $byUser)
{
$this->calendarId = $calendar->getId();
$this->byUserId = $byUser->getId();
$this->action = $action;
}
@ -34,6 +38,11 @@ class CalendarMessage
return $this->action;
}
public function getByUserId(): ?int
{
return $this->byUserId;
}
public function getCalendarId(): ?int
{
return $this->calendarId;

View File

@ -11,6 +11,7 @@ declare(strict_types=1);
namespace Chill\CalendarBundle\RemoteCalendar\Connector;
use Chill\CalendarBundle\Entity\Calendar;
use Chill\CalendarBundle\Entity\CalendarRange;
use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\MachineHttpClient;
use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\MapCalendarToUser;
@ -136,6 +137,11 @@ class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface
$this->removeEvent($remoteId, $user);
}
public function syncCalendar(Calendar $calendar, string $action): void
{
// TODO: Implement syncCalendar() method.
}
public function syncCalendarRange(CalendarRange $calendarRange): void
{
if ($calendarRange->hasRemoteId()) {

View File

@ -11,6 +11,7 @@ declare(strict_types=1);
namespace Chill\CalendarBundle\RemoteCalendar\Connector;
use Chill\CalendarBundle\Entity\Calendar;
use Chill\CalendarBundle\Entity\CalendarRange;
use Chill\MainBundle\Entity\User;
use DateTimeImmutable;
@ -38,6 +39,10 @@ class NullRemoteCalendarConnector implements RemoteCalendarConnectorInterface
{
}
public function syncCalendar(Calendar $calendar, string $action): void
{
}
public function syncCalendarRange(CalendarRange $calendarRange): void
{
}

View File

@ -11,6 +11,7 @@ declare(strict_types=1);
namespace Chill\CalendarBundle\RemoteCalendar\Connector;
use Chill\CalendarBundle\Entity\Calendar;
use Chill\CalendarBundle\Entity\CalendarRange;
use Chill\CalendarBundle\RemoteCalendar\Model\RemoteEvent;
use Chill\MainBundle\Entity\User;
@ -39,5 +40,7 @@ interface RemoteCalendarConnectorInterface
public function removeCalendarRange(string $remoteId, array $remoteAttributes, User $user): void;
public function syncCalendar(Calendar $calendar, string $action): void;
public function syncCalendarRange(CalendarRange $calendarRange): void;
}

View File

@ -11,7 +11,6 @@ services:
Chill\CalendarBundle\Messenger\Doctrine\CalendarRangeEntityListener:
autowire: true
autoconfigure: true
tags:
-
name: 'doctrine.orm.entity_listener'
@ -25,3 +24,20 @@ services:
name: 'doctrine.orm.entity_listener'
event: 'postRemove'
entity: 'Chill\CalendarBundle\Entity\CalendarRange'
Chill\CalendarBundle\Messenger\Doctrine\CalendarEntityListener:
autowire: true
autoconfigure: true
tags:
-
name: 'doctrine.orm.entity_listener'
event: 'postPersist'
entity: 'Chill\CalendarBundle\Entity\Calendar'
-
name: 'doctrine.orm.entity_listener'
event: 'postUpdate'
entity: 'Chill\CalendarBundle\Entity\Calendar'
-
name: 'doctrine.orm.entity_listener'
event: 'postRemove'
entity: 'Chill\CalendarBundle\Entity\Calendar'

View File

@ -8,7 +8,7 @@
:multiple="false"
:types="['user']"
:uniqid="'main_user_calendar'"
:picked="[this.$store.getters.getMainUser]"
:picked="null !== this.$store.getters.getMainUser ? [this.$store.getters.getMainUser] : []"
:removableIfSet="false"
@addNewEntity="setMainUser"
></pick-entity>

View File

@ -0,0 +1,52 @@
<?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\Migrations\Calendar;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20220525080633 extends AbstractMigration
{
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE chill_calendar.invite ALTER status DROP NOT NULL');
$this->addSql('DROP INDEX chill_calendar.UNIQ_712315ACC5CB285D');
$this->addSql('DROP INDEX chill_calendar.idx_calendar_remote');
$this->addSql('ALTER TABLE chill_calendar.calendar ADD user_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE chill_calendar.calendar DROP remoteAttributes');
$this->addSql('ALTER TABLE chill_calendar.calendar DROP remoteId');
$this->addSql('ALTER TABLE chill_calendar.calendar ALTER status DROP DEFAULT');
$this->addSql('ALTER TABLE chill_calendar.calendar ADD CONSTRAINT fk_712315aca76ed395 FOREIGN KEY (user_id) REFERENCES users (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('CREATE INDEX idx_712315acc5cb285d ON chill_calendar.calendar (calendarrange_id)');
$this->addSql('CREATE INDEX idx_712315aca76ed395 ON chill_calendar.calendar (user_id)');
}
public function getDescription(): string
{
return 'Calendar: add remote infos and fix associations';
}
public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE chill_calendar.calendar DROP CONSTRAINT fk_712315aca76ed395');
$this->addSql('DROP INDEX chill_calendar.idx_712315acc5cb285d');
$this->addSql('DROP INDEX chill_calendar.idx_712315aca76ed395');
$this->addSql('ALTER TABLE chill_calendar.calendar ADD remoteAttributes JSON DEFAULT \'[]\' NOT NULL');
$this->addSql('ALTER TABLE chill_calendar.calendar ADD remoteId TEXT DEFAULT \'\' NOT NULL');
$this->addSql('ALTER TABLE chill_calendar.calendar DROP user_id');
$this->addSql('ALTER TABLE chill_calendar.calendar ALTER status SET DEFAULT \'valid\'');
$this->addSql('CREATE UNIQUE INDEX UNIQ_712315ACC5CB285D ON chill_calendar.calendar (calendarRange_id)');
$this->addSql('CREATE INDEX idx_calendar_remote ON chill_calendar.calendar (remoteId)');
$this->addSql('UPDATE chill_calendar.invite SET status=\'pending\' WHERE status IS NULL');
$this->addSql('ALTER TABLE chill_calendar.invite ALTER status SET NOT NULL');
}
}