Skip to content

Commit

Permalink
Merge pull request #3692 from MTES-MCT/hotfix/3690-bo---notifications…
Browse files Browse the repository at this point in the history
…-noncrees-si-pas-email

[BO - Notifications] Créer des notifications "app" même quand les notifications par mail sont désactivées
  • Loading branch information
emilschn authored Feb 11, 2025
2 parents 72ac092 + a6f9e9b commit a1d66d6
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions src/Service/NotificationAndMailSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,29 @@ public function sendNewSuiviToAdminsAndPartners(Suivi $suivi, bool $sendEmail):
$this->suivi = $suivi;
$this->signalement = $suivi->getSignalement();
$territory = $this->signalement->getTerritory();
$recipients = $this->getRecipientsAdmin($territory);
$recipientsEmail = $this->getRecipientsAdmin($territory);
$recipientsNotifications = $this->getRecipientsAdmin($territory);

foreach ($this->signalement->getAffectations() as $affectation) {
if (AffectationStatus::STATUS_WAIT->value === $affectation->getStatut()
|| AffectationStatus::STATUS_ACCEPTED->value === $affectation->getStatut()) {
$partnerRecipients = $this->getRecipientsPartner($affectation->getPartner());
$recipients = new ArrayCollection(
array_merge($recipients->toArray(), $partnerRecipients->toArray())
$partnerRecipientsFiltered = $this->getRecipientsPartner($affectation->getPartner());
$partnerRecipients = $this->getRecipientsPartner($affectation->getPartner(), false);
$recipientsEmail = new ArrayCollection(
array_merge($recipientsEmail->toArray(), $partnerRecipientsFiltered->toArray())
);
$recipientsNotifications = new ArrayCollection(
array_merge($recipientsNotifications->toArray(), $partnerRecipients->toArray())
);
}
}

$this->send(
notificationMailerType: $mailerType,
recipients: $recipients,
isInAppNotificationCreated: true
recipients: $recipientsEmail
);
$this->createInAppNotifications(
recipients: $recipientsNotifications,
);
}

Expand All @@ -102,22 +109,23 @@ public function sendNewSuiviToUsagers(Suivi $suivi): void
}
}

private function send(?NotificationMailerType $notificationMailerType, ArrayCollection $recipients, bool $isInAppNotificationCreated = false): void
private function send(?NotificationMailerType $notificationMailerType, ArrayCollection $recipients): void
{
if ($isInAppNotificationCreated) {
foreach ($recipients as $user) {
if ($user instanceof User) {
$this->createInAppNotification($user);
}
}
$this->entityManager->flush();
}

if ($notificationMailerType) {
$this->sendMail($recipients, $notificationMailerType);
}
}

private function createInAppNotifications(ArrayCollection $recipients)
{
foreach ($recipients as $user) {
if ($user instanceof User) {
$this->createInAppNotification($user);
}
}
$this->entityManager->flush();
}

private function createInAppNotification($user): void
{
if (empty($this->suivi) || Suivi::DESCRIPTION_SIGNALEMENT_VALIDE === $this->suivi->getDescription()) {
Expand Down Expand Up @@ -161,15 +169,17 @@ private function getRecipientsAdmin(?Territory $territory): ArrayCollection
return $recipients;
}

private function getRecipientsPartner(Partner $partner): ArrayCollection
private function getRecipientsPartner(Partner $partner, bool $filterMailingActive = true): ArrayCollection
{
$recipients = new ArrayCollection();
if ($partner->getEmail()) {
$recipients->add($partner);
}

foreach ($partner->getUsers() as $user) {
if ($user->getIsMailingActive() && $this->isUserNotified($partner, $user)) {
if ($filterMailingActive && $user->getIsMailingActive() && $this->isUserNotified($partner, $user)) {
$recipients->add($user);
} elseif (!$filterMailingActive && $this->isUserNotified($partner, $user)) {
$recipients->add($user);
}
}
Expand Down

0 comments on commit a1d66d6

Please sign in to comment.