send system notification on period confirmation

This commit is contained in:
Julien Fastré 2021-12-30 02:10:35 +01:00
parent 7bc4ad9779
commit 5bf1b9d8bd
12 changed files with 233 additions and 23 deletions

View File

@ -74,9 +74,14 @@ class Notification implements TrackUpdateInterface
/**
* @ORM\ManyToOne(targetEntity=User::class)
* @ORM\JoinColumn(nullable=false)
* @ORM\JoinColumn(nullable=true)
*/
private User $sender;
private ?User $sender = null;
/**
* @ORM\Column(type="text", options={"default": ""})
*/
private string $title = '';
/**
* @ORM\ManyToMany(targetEntity=User::class)
@ -179,6 +184,11 @@ class Notification implements TrackUpdateInterface
return $this->sender;
}
public function getTitle(): string
{
return $this->title;
}
public function getUnreadBy(): Collection
{
return $this->unreadBy;
@ -199,6 +209,11 @@ class Notification implements TrackUpdateInterface
return !$this->unreadBy->contains($user);
}
public function isSystem(): bool
{
return null === $this->sender;
}
public function markAsReadBy(User $user): self
{
return $this->removeUnreadBy($user);
@ -299,6 +314,13 @@ class Notification implements TrackUpdateInterface
return $this;
}
public function setTitle(string $title): Notification
{
$this->title = $title;
return $this;
}
public function setUpdatedAt(DateTimeInterface $datetime): self
{
$this->updatedAt = $datetime;

View File

@ -1,5 +1,14 @@
<?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\Notification;
use Chill\MainBundle\Entity\Notification;
@ -8,14 +17,14 @@ use Chill\MainBundle\Repository\NotificationRepository;
use Symfony\Component\Security\Core\Security;
/**
* Helps to find if a notification exist for a given entity
* Helps to find if a notification exist for a given entity.
*/
class NotificationPresence
{
private Security $security;
private NotificationRepository $notificationRepository;
private Security $security;
public function __construct(Security $security, NotificationRepository $notificationRepository)
{
$this->security = $security;

View File

@ -1,5 +1,14 @@
<?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\Notification\Templating;
use Twig\Extension\AbstractExtension;

View File

@ -1,5 +1,14 @@
<?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\Notification\Templating;
use Chill\MainBundle\Notification\NotificationPresence;
@ -27,5 +36,4 @@ class NotificationTwigExtensionRuntime implements RuntimeExtensionInterface
'notifications' => $notifications,
]);
}
}

View File

@ -134,9 +134,17 @@ final class NotificationRepository implements ObjectRepository
}
/**
* @param string $relatedEntityClass
* @param int $relatedEntityId
* @param User $user
* @param mixed|null $limit
* @param mixed|null $offset
*
* @return Notification[]
*/
public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null): array
{
return $this->repository->findBy($criteria, $orderBy, $limit, $offset);
}
/**
* @return array|Notification[]
*/
public function findNotificationAsAddresseeByRelatedEntityAndUser(string $relatedEntityClass, int $relatedEntityId, User $user): array
@ -150,23 +158,11 @@ final class NotificationRepository implements ObjectRepository
->andWhere($qb->expr()->isMemberOf(':user', 'n.addressees'))
->setParameter('relatedEntityClass', $relatedEntityClass)
->setParameter('relatedEntityId', $relatedEntityId)
->setParameter('user', $user)
;
->setParameter('user', $user);
return $qb->getQuery()->getResult();
}
/**
* @param mixed|null $limit
* @param mixed|null $offset
*
* @return Notification[]
*/
public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null): array
{
return $this->repository->findBy($criteria, $orderBy, $limit, $offset);
}
public function findOneBy(array $criteria, ?array $orderBy = null): ?Notification
{
return $this->repository->findOneBy($criteria, $orderBy);

View File

@ -1,7 +1,11 @@
<div class="list_notification_for">
{% for notification in notifications %}
<div class="notification {% if notification.isReadBy(app.user) %}read{% else %}unread{% endif %}">
{% if not notification.isSystem %}
<div>{{ 'notification.you were notified by %sender%'|trans({'%sender%': notification.sender|chill_entity_render_string }) }}</div>
{% else %}
<div>{{ 'notification.you were notified by system'|trans }}</div>
{% endif %}
<div>
<ul class="record_actions_small">
<li>

View File

@ -0,0 +1,38 @@
<?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\Main;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20211230003532 extends AbstractMigration
{
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE chill_main_notification DROP title');
$this->addSql('ALTER TABLE chill_main_notification ALTER sender_id SET NOT NULL');
$this->addSql('ALTER TABLE chill_main_notification ALTER updatedAt DROP NOT NULL');
}
public function getDescription(): string
{
return 'Add title and allow system notification (sender is null)';
}
public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE chill_main_notification ADD title TEXT DEFAULT \'\' NOT NULL');
$this->addSql('ALTER TABLE chill_main_notification ALTER sender_id DROP NOT NULL');
$this->addSql('UPDATE chill_main_notification set updatedat="date"');
$this->addSql('ALTER TABLE chill_main_notification ALTER updatedat SET NOT NULL');
}
}

View File

@ -0,0 +1,77 @@
<?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\AccompanyingPeriod\Workflow;
use Chill\MainBundle\Entity\Notification;
use Chill\MainBundle\Entity\User;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Templating\EngineInterface;
use Symfony\Component\Workflow\Event\EnteredEvent;
use Symfony\Contracts\Translation\TranslatorInterface;
class WorkflowEventSubscriber implements EventSubscriberInterface
{
private EntityManagerInterface $em;
private EngineInterface $engine;
private Security $security;
private TranslatorInterface $translator;
public function __construct(Security $security, TranslatorInterface $translator, EngineInterface $engine, EntityManagerInterface $em)
{
$this->security = $security;
$this->translator = $translator;
$this->engine = $engine;
$this->em = $em;
}
public static function getSubscribedEvents()
{
return [
'workflow.accompanying_period_lifecycle.entered' => [
'onStateEntered',
],
];
}
public function onStateEntered(EnteredEvent $enteredEvent): void
{
if ($enteredEvent->getMarking()->has(AccompanyingPeriod::STEP_CONFIRMED)) {
$this->onPeriodConfirmed($enteredEvent->getSubject());
}
}
private function onPeriodConfirmed(AccompanyingPeriod $period)
{
if ($period->getUser() instanceof User
&& $period->getUser() !== $this->security->getUser()) {
$notification = new Notification();
$notification
->setRelatedEntityId($period->getId())
->setRelatedEntityClass(AccompanyingPeriod::class)
->setTitle($this->translator->trans('period_notification.period designated_subject'))
->setMessage($this->engine->render(
'@ChillPerson/Notification/accompanying_course_designation.md.twig',
[
'accompanyingCourse' => $period,
]
))
->addAddressee($period->getUser());
$this->em->persist($notification);
}
}
}

View File

@ -0,0 +1,12 @@
{{ 'period_notification.You are designated to a new period.'|trans }}
{{ 'period_notification.See it online'|trans }}: {{ path('chill_person_accompanying_course_index', {'accompanying_period_id': accompanyingCourse.id}, true) }}
{{ 'period_notification.Persons are'|trans }}:
{% for p in accompanyingCourse.getCurrentParticipations %}
* {{ p.person|chill_entity_render_string }}
{% endfor %}
{{ 'period_notification.Social issues are'|trans }}: {% for s in accompanyingCourse.socialIssues %}{{ s|chill_entity_render_string }}{% if not loop.last %}, {% endif %}{% endfor %}.

View File

@ -0,0 +1,22 @@
<?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 AccompanyingPeriod\Workflow;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
/**
* @internal
* @coversNothing
*/
final class WorkflowEventSubscriberTest extends KernelTestCase
{
}

View File

@ -20,3 +20,9 @@ services:
Chill\PersonBundle\AccompanyingPeriod\Suggestion\ReferralsSuggestionInterface: '@Chill\PersonBundle\AccompanyingPeriod\Suggestion\ReferralsSuggestion'
Chill\PersonBundle\AccompanyingPeriod\Workflow\:
resource: './../../AccompanyingPeriod/Workflow'
autowire: true
autoconfigure: true

View File

@ -475,3 +475,10 @@ docgen:
A basic context for accompanying period: Contexte pour les parcours
A context for accompanying period work: Contexte pour les actions d'accompagnement
A context for accompanying period work evaluation: Contexte pour les évaluations dans les actions d'accompagnement
period_notification:
period_designated_subject: Vous êtes référent d'un parcours d'accompagnement
You are designated to a new period: Vous avez été désigné référent d'un parcours d'accompagnement.
Persons are: Les usagers concernés sont les suivants
Social issues are: Les problématiques sociales renseignées sont les suivantes
See it online: Visualisez le parcours en ligne