Update schema to store accompanying period work referrer history

This commit is contained in:
2023-10-05 09:33:23 +02:00
parent 0b6b25fd95
commit 239372270e
3 changed files with 234 additions and 14 deletions

View File

@@ -26,6 +26,7 @@ use DateTimeImmutable;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ReadableCollection;
use Doctrine\ORM\Mapping as ORM;
use LogicException;
use Symfony\Component\Serializer\Annotation as Serializer;
@@ -154,14 +155,11 @@ class AccompanyingPeriodWork implements AccompanyingPeriodLinkedWithSocialIssues
private PrivateCommentEmbeddable $privateComment;
/**
* @var Collection<User>
* @ORM\ManyToMany(targetEntity=User::class)
* @var Collection<int, AccompanyingPeriodWorkReferrerHistory>
* @ORM\OneToMany(targetEntity=AccompanyingPeriodWorkReferrerHistory::class, cascade={"persist", "remove"}, mappedBy="accompanyingPeriodWork")
* @ORM\JoinTable(name="chill_person_accompanying_period_work_referrer")
* @Serializer\Groups({"read", "docgen:read", "read:accompanyingPeriodWork:light"})
* @Serializer\Groups({"accompanying_period_work:edit"})
* @Serializer\Groups({"accompanying_period_work:create"})
*/
private Collection $referrers;
private Collection $referrersHistory;
/**
* @var Collection<Result>
@@ -220,7 +218,7 @@ class AccompanyingPeriodWork implements AccompanyingPeriodLinkedWithSocialIssues
$this->thirdParties = new ArrayCollection();
$this->persons = new ArrayCollection();
$this->accompanyingPeriodWorkEvaluations = new ArrayCollection();
$this->referrers = new ArrayCollection();
$this->referrersHistory = new ArrayCollection();
}
public function addAccompanyingPeriodWorkEvaluation(AccompanyingPeriodWorkEvaluation $evaluation): self
@@ -254,8 +252,9 @@ class AccompanyingPeriodWork implements AccompanyingPeriodLinkedWithSocialIssues
public function addReferrer(User $referrer): self
{
if (!$this->referrers->contains($referrer)) {
$this->referrers[] = $referrer;
if (!$this->getReferrers()->contains($referrer)) {
$this->referrersHistory[] =
new AccompanyingPeriodWorkReferrerHistory($this, $referrer, new DateTimeImmutable('today'));
}
return $this;
@@ -351,15 +350,18 @@ class AccompanyingPeriodWork implements AccompanyingPeriodLinkedWithSocialIssues
}
/**
* @return Collection|User[]
* @return ReadableCollection<int, User>
* @Serializer\Groups({"read", "docgen:read", "read:accompanyingPeriodWork:light"})
* @Serializer\Groups({"accompanying_period_work:edit"})
* @Serializer\Groups({"accompanying_period_work:create"})
*/
public function getReferrers(): Collection
public function getReferrers(): ReadableCollection
{
return $this->referrers;
return $this->referrersHistory->map(fn (AccompanyingPeriodWorkReferrerHistory $h) => $h->getUser());
}
/**
* @return Collection|Result[]
* @return Collection<int, Result>
*/
public function getResults(): Collection
{
@@ -434,7 +436,15 @@ class AccompanyingPeriodWork implements AccompanyingPeriodLinkedWithSocialIssues
public function removeReferrer(User $referrer): self
{
$this->referrers->removeElement($referrer);
foreach ($this->referrersHistory as $history) {
if ($history->isOpen() && $referrer === $history->getUser()) {
$history->setEndDate(new DateTimeImmutable('today'));
if ($history->isDateRangeEmpty()) {
$history->removeAccompanyingPeriodWork();
}
}
}
return $this;
}