add history for user in accompanying period (+ counter)

This commit is contained in:
Julien Fastré 2022-01-28 14:42:18 +01:00
parent 2cd51eed2e
commit fcd5fba13e
6 changed files with 196 additions and 31 deletions

View File

@ -193,6 +193,29 @@ final class NotificationRepository implements ObjectRepository
return $this->repository->findOneBy($criteria, $orderBy); return $this->repository->findOneBy($criteria, $orderBy);
} }
/**
* @return array|Notification[]
*/
public function findUnreadByUser(User $user, int $limit = 20, int $offset = 0): array
{
$rsm = new Query\ResultSetMappingBuilder($this->em);
$rsm->addRootEntityFromClassMetadata(Notification::class, 'cmn');
$sql = 'SELECT ' . $rsm->generateSelectClause(['cmn' => 'cmn']) . ' ' .
'FROM chill_main_notification cmn ' .
'WHERE ' .
'EXISTS (select 1 FROM chill_main_notification_addresses_unread cmnau WHERE cmnau.user_id = :userId and cmnau.notification_id = cmn.id) ' .
'ORDER BY cmn.date DESC ' .
'LIMIT :limit OFFSET :offset';
$nq = $this->em->createNativeQuery($sql, $rsm)
->setParameter('userId', $user->getId())
->setParameter('limit', $limit)
->setParameter('offset', $offset);
return $nq->getResult();
}
public function getClassName() public function getClassName()
{ {
return Notification::class; return Notification::class;
@ -219,29 +242,6 @@ final class NotificationRepository implements ObjectRepository
return $qb->getQuery()->getResult(); return $qb->getQuery()->getResult();
} }
/**
* @return array|Notification[]
*/
public function findUnreadByUser(User $user, int $limit = 20, int $offset = 0): array
{
$rsm = new Query\ResultSetMappingBuilder($this->em);
$rsm->addRootEntityFromClassMetadata(Notification::class, 'cmn');
$sql = 'SELECT ' . $rsm->generateSelectClause(['cmn' => 'cmn']) . ' ' .
'FROM chill_main_notification cmn ' .
'WHERE ' .
'EXISTS (select 1 FROM chill_main_notification_addresses_unread cmnau WHERE cmnau.user_id = :userId and cmnau.notification_id = cmn.id) ' .
'ORDER BY cmn.date DESC ' .
'LIMIT :limit OFFSET :offset';
$nq = $this->em->createNativeQuery($sql, $rsm)
->setParameter('userId', $user->getId())
->setParameter('limit', $limit)
->setParameter('offset', $offset);
return $nq->getResult();
}
private function queryByAddressee(User $addressee, bool $countQuery = false): QueryBuilder private function queryByAddressee(User $addressee, bool $countQuery = false): QueryBuilder
{ {
$qb = $this->repository->createQueryBuilder('n'); $qb = $this->repository->createQueryBuilder('n');

View File

@ -1,8 +1,19 @@
<?php <?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\MainBundle\Serializer\Model; namespace Chill\MainBundle\Serializer\Model;
class Counter implements \JsonSerializable use JsonSerializable;
class Counter implements JsonSerializable
{ {
private int $counter; private int $counter;
@ -16,16 +27,15 @@ class Counter implements \JsonSerializable
return $this->counter; return $this->counter;
} }
public function setCounter(?int $counter): Counter
{
$this->counter = $counter;
return $this;
}
public function jsonSerialize() public function jsonSerialize()
{ {
return ['count' => $this->counter]; return ['count' => $this->counter];
} }
public function setCounter(?int $counter): Counter
{
$this->counter = $counter;
return $this;
}
} }

View File

@ -26,6 +26,7 @@ use Chill\PersonBundle\Entity\AccompanyingPeriod\ClosingMotive;
use Chill\PersonBundle\Entity\AccompanyingPeriod\Comment; use Chill\PersonBundle\Entity\AccompanyingPeriod\Comment;
use Chill\PersonBundle\Entity\AccompanyingPeriod\Origin; use Chill\PersonBundle\Entity\AccompanyingPeriod\Origin;
use Chill\PersonBundle\Entity\AccompanyingPeriod\Resource; use Chill\PersonBundle\Entity\AccompanyingPeriod\Resource;
use Chill\PersonBundle\Entity\AccompanyingPeriod\UserHistory;
use Chill\PersonBundle\Entity\SocialWork\SocialIssue; use Chill\PersonBundle\Entity\SocialWork\SocialIssue;
use Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod\AccompanyingPeriodValidity; use Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod\AccompanyingPeriodValidity;
use Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod\ParticipationOverlap; use Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod\ParticipationOverlap;
@ -336,6 +337,13 @@ class AccompanyingPeriod implements
*/ */
private ?User $user = null; private ?User $user = null;
/**
* @ORM\OneToMany(targetEntity=UserHistory::class, mappedBy="accompanyingPeriod")
*
* @var Collection|UserHistory[]
*/
private Collection $userHistories;
/** /**
* Temporary field, which is filled when the user is changed. * Temporary field, which is filled when the user is changed.
* *
@ -368,6 +376,7 @@ class AccompanyingPeriod implements
$this->comments = new ArrayCollection(); $this->comments = new ArrayCollection();
$this->works = new ArrayCollection(); $this->works = new ArrayCollection();
$this->resources = new ArrayCollection(); $this->resources = new ArrayCollection();
$this->userHistories = new ArrayCollection();
} }
/** /**
@ -1212,10 +1221,20 @@ class AccompanyingPeriod implements
return $this; return $this;
} }
public function setUser(User $user): self public function setUser(?User $user): self
{ {
if ($this->user !== $user) { if ($this->user !== $user) {
$this->userPrevious = $this->user; $this->userPrevious = $this->user;
if (null !== $user) {
$this->userHistories->add(new UserHistory($this, $user));
}
foreach ($this->userHistories as $history) {
if (null === $history->getEndDate()) {
$history->setEndDate(new DateTimeImmutable('now'));
}
}
} }
$this->user = $user; $this->user = $user;

View File

@ -0,0 +1,91 @@
<?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\PersonBundle\Entity\AccompanyingPeriod;
use Chill\MainBundle\Entity\User;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table("chill_person_accompanying_period_user_history")
*/
class UserHistory
{
/**
* @ORM\ManyToOne(targetEntity=AccompanyingPeriod::class, inversedBy="userHistories")
* @ORM\JoinColumn(nullable=false)
*/
private ?AccompanyingPeriod $accompanyingPeriod;
/**
* @ORM\Column(type="datetime_immutable", nullable=true, options={"default": null})
*/
private ?\DateTimeImmutable $endDate = null;
/**
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private ?int $id = null;
/**
* @ORM\Column(type="datetime_immutable", nullable=false)
*/
private \DateTimeImmutable $startDate;
/**
* @ORM\ManyToOne(targetEntity=User::class)
* @ORM\JoinColumn(nullable=false)
*/
private User $user;
public function __construct(AccompanyingPeriod $accompanyingPeriod, User $user, ?\DateTimeImmutable $startDate = null)
{
$this->startDate = $startDate ?? new \DateTimeImmutable('now');
$this->accompanyingPeriod = $accompanyingPeriod;
$this->user = $user;
}
public function getAccompanyingPeriod(): AccompanyingPeriod
{
return $this->accompanyingPeriod;
}
public function getEndDate(): ?DateTimeImmutable
{
return $this->endDate;
}
public function getId(): ?int
{
return $this->id;
}
public function getStartDate(): DateTimeImmutable
{
return $this->startDate;
}
public function getUser(): User
{
return $this->user;
}
public function setEndDate(?DateTimeImmutable $endDate): UserHistory
{
$this->endDate = $endDate;
return $this;
}
}

View File

@ -50,6 +50,7 @@ class AccompanyingPeriodWorkEvaluationRepository implements ObjectRepository
/** /**
* @param int $limit * @param int $limit
* @param int $offset * @param int $offset
*
* @return array|AccompanyingPeriodWorkEvaluation[] * @return array|AccompanyingPeriodWorkEvaluation[]
*/ */
public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null): array public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null): array

View File

@ -0,0 +1,44 @@
<?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\Person;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20220128133039 extends AbstractMigration
{
public function down(Schema $schema): void
{
$this->addSql('DROP SEQUENCE chill_person_accompanying_period_user_history_id_seq CASCADE');
$this->addSql('DROP TABLE chill_person_accompanying_period_user_history');
}
public function getDescription(): string
{
return 'Add table for tracking user history on accompanying period';
}
public function up(Schema $schema): void
{
$this->addSql('CREATE SEQUENCE chill_person_accompanying_period_user_history_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
$this->addSql('CREATE TABLE chill_person_accompanying_period_user_history (id INT NOT NULL, user_id INT, endDate TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, startDate TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, accompanyingPeriod_id INT, PRIMARY KEY(id))');
$this->addSql('CREATE INDEX IDX_6C258C49D7FA8EF0 ON chill_person_accompanying_period_user_history (accompanyingPeriod_id)');
$this->addSql('CREATE INDEX IDX_6C258C49A76ED395 ON chill_person_accompanying_period_user_history (user_id)');
$this->addSql('COMMENT ON COLUMN chill_person_accompanying_period_user_history.endDate IS \'(DC2Type:datetime_immutable)\'');
$this->addSql('COMMENT ON COLUMN chill_person_accompanying_period_user_history.startDate IS \'(DC2Type:datetime_immutable)\'');
$this->addSql('ALTER TABLE chill_person_accompanying_period_user_history ADD CONSTRAINT FK_6C258C49D7FA8EF0 FOREIGN KEY (accompanyingPeriod_id) REFERENCES chill_person_accompanying_period (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE chill_person_accompanying_period_user_history ADD CONSTRAINT FK_6C258C49A76ED395 FOREIGN KEY (user_id) REFERENCES users (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE chill_person_accompanying_period_user_history ADD CHECK (startdate <= enddate)');
$this->addSql('INSERT INTO chill_person_accompanying_period_user_history (id, user_id, accompanyingperiod_id, startDate, endDate) ' .
'SELECT nextval(\'chill_person_accompanying_period_user_history_id_seq\'), user_id, id, openingDate, null FROM chill_person_accompanying_period WHERE user_id IS NOT NULL');
}
}