Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BO - Agent multi territoires] Ajouter des modales à l'ajout / suppression d'un territoire #3651

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assets/scripts/app-back-bo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import './vue/signalement-carto.ts';
import './vue/dashboard.ts';

import './vanilla/services/data_delete.js'
import './vanilla/services/modale_cgu_bo.js';
import './vanilla/services/bo_modales.js';
import './vanilla/services/pagination.js'
import './vanilla/services/search_filter_form.js'
import './vanilla/services/table_sortable.js'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,27 @@ function handleAcceptButton () {
acceptCguBoCheckbox?.addEventListener('click', handleAcceptCheck)
acceptCguBoButton?.addEventListener('click', handleAcceptButton)
modalCguBo?.addEventListener('dsfr.disclose', handleModalDisclose)

const modalPopNotification = document.getElementById('fr-modal-pop-notification')
if(modalPopNotification){
//prevent error dsfr is not defined or null
const checkDsfrInterval = setInterval(() => {
if (typeof dsfr !== 'undefined' && dsfr !== null) {
clearInterval(checkDsfrInterval);

modalPopNotification.addEventListener('dsfr.conceal', (e) => {
const deletePopNotificationUrl = modalPopNotification.dataset.deleteUrl;
fetch(deletePopNotificationUrl, {})
});

if (modalCguBo) {
modalCguBo.addEventListener('dsfr.conceal', (e) => {
dsfr(modalPopNotification).modal.disclose();
});
} else {
dsfr(modalPopNotification).modal.disclose();
}
}
}, 100);
}

28 changes: 28 additions & 0 deletions migrations/Version20250129155205.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20250129155205 extends AbstractMigration
{
public function getDescription(): string
{
return 'Create pop_notification table';
}

public function up(Schema $schema): void
{
$this->addSql('CREATE TABLE pop_notification (id INT AUTO_INCREMENT NOT NULL, user_id INT NOT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', params JSON NOT NULL, INDEX IDX_257F9F96A76ED395 (user_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE pop_notification ADD CONSTRAINT FK_257F9F96A76ED395 FOREIGN KEY (user_id) REFERENCES user (id)');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE pop_notification DROP FOREIGN KEY FK_257F9F96A76ED395');
$this->addSql('DROP TABLE pop_notification');
}
}
7 changes: 7 additions & 0 deletions src/Controller/Back/PartnerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use App\Manager\AffectationManager;
use App\Manager\InterventionManager;
use App\Manager\PartnerManager;
use App\Manager\PopNotificationManager;
use App\Manager\UserManager;
use App\Repository\AutoAffectationRuleRepository;
use App\Repository\JobEventRepository;
Expand Down Expand Up @@ -267,6 +268,7 @@ public function delete(
WorkflowInterface $interventionPlanningStateMachine,
InterventionManager $interventionManager,
AffectationManager $affectationManager,
PopNotificationManager $popNotificationManager,
): Response {
$partnerId = $request->request->get('partner_id');
/** @var ?Partner $partner */
Expand All @@ -284,6 +286,7 @@ public function delete(
if ($user->getUserPartners()->count() > 1) {
foreach ($user->getUserPartners() as $userPartner) {
if ($userPartner->getPartner()->getId() === $partner->getId()) {
$popNotificationManager->createOrUpdatePopNotification($user, 'removePartner', $partner);
$entityManager->remove($userPartner);
break;
}
Expand Down Expand Up @@ -360,6 +363,7 @@ public function addUserPartnerMulti(
UserRepository $userRepository,
UserManager $userManager,
NotificationMailerRegistry $notificationMailerRegistry,
PopNotificationManager $popNotificationManager,
): JsonResponse|RedirectResponse {
if (!$this->featureMultiTerritories) {
throw $this->createNotFoundException();
Expand All @@ -375,6 +379,7 @@ public function addUserPartnerMulti(
$user = $userRepository->findAgentByEmail($userTmp->getEmail());
if ($user) {
$userPartner->setUser($user);
$popNotificationManager->createOrUpdatePopNotification($user, 'addPartner', $partner);
$userManager->save($userPartner);
$notificationMailerRegistry->send(
new NotificationMail(
Expand Down Expand Up @@ -612,6 +617,7 @@ public function deleteUser(
Partner $partner,
UserManager $userManager,
NotificationMailerRegistry $notificationMailerRegistry,
PopNotificationManager $popNotificationManager,
): Response {
$userId = $request->request->get('user_id');
if (!$this->isCsrfTokenValid('partner_user_delete', $request->request->get('_token'))) {
Expand All @@ -636,6 +642,7 @@ public function deleteUser(
if ($user->getUserPartners()->count() > 1) {
foreach ($user->getUserPartners() as $userPartner) {
if ($userPartner->getPartner()->getId() === $partner->getId()) {
$popNotificationManager->createOrUpdatePopNotification($user, 'removePartner', $partner);
$userManager->remove($userPartner);
break;
}
Expand Down
47 changes: 47 additions & 0 deletions src/Controller/Back/PopNotificationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace App\Controller\Back;

use App\Entity\User;
use App\Manager\PopNotificationManager;
use App\Repository\PartnerRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

#[Route('/bo/pop-notification')]
class PopNotificationController extends AbstractController
{
public function show(
PartnerRepository $partnerRepository,
): Response {
/** @var ?User $user */
$user = $this->getUser();
$popNotification = $user->getPopNotifications()->first();
$addedPartnersIds = isset($popNotification->getParams()['addedPartners']) ? $popNotification->getParams()['addedPartners'] : [];
$removedPartnersIds = isset($popNotification->getParams()['removedPartners']) ? $popNotification->getParams()['removedPartners'] : [];
$addedPartners = $partnerRepository->findBy(['id' => $addedPartnersIds]);
$removedPartners = $partnerRepository->findBy(['id' => $removedPartnersIds]);

return $this->render('back/pop-notification/index.html.twig', [
'user' => $user,
'addedPartners' => $addedPartners,
'removedPartners' => $removedPartners,
]);
}

#[Route('/delete', name: 'pop_notification_delete')]
public function delete(
PopNotificationManager $popNotificationManager,
): JsonResponse {
/** @var ?User $user */
$user = $this->getUser();
$popNotification = $user->getPopNotifications()->count() ? $user->getPopNotifications()->first() : null;
if ($popNotification) {
$popNotificationManager->remove($popNotification);
}

return new JsonResponse(['success' => true]);
}
}
71 changes: 71 additions & 0 deletions src/Entity/PopNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace App\Entity;

use App\Repository\PopNotificationRepository;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: PopNotificationRepository::class)]
class PopNotification
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;

#[ORM\ManyToOne(inversedBy: 'popNotifications')]
#[ORM\JoinColumn(nullable: false)]
private ?User $user = null;

#[ORM\Column]
private ?\DateTimeImmutable $createdAt = null;

#[ORM\Column]
private array $params = [];

public function __construct()
{
$this->createdAt = new \DateTimeImmutable();
}

public function getId(): ?int
{
return $this->id;
}

public function getUser(): ?User
{
return $this->user;
}

public function setUser(?User $user): static
{
$this->user = $user;

return $this;
}

public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}

public function setCreatedAt(\DateTimeImmutable $createdAt): static
{
$this->createdAt = $createdAt;

return $this;
}

public function getParams(): array
{
return $this->params;
}

public function setParams(array $params): static
{
$this->params = $params;

return $this;
}
}
37 changes: 37 additions & 0 deletions src/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ class User implements UserInterface, EntityHistoryInterface, PasswordAuthenticat
#[ORM\OneToMany(mappedBy: 'user', targetEntity: UserPartner::class, orphanRemoval: true)]
private Collection $userPartners;

/**
* @var Collection<int, PopNotification>
*/
#[ORM\OneToMany(mappedBy: 'user', targetEntity: PopNotification::class, orphanRemoval: true)]
private Collection $popNotifications;

public function __construct()
{
$this->suivis = new ArrayCollection();
Expand All @@ -189,6 +195,7 @@ public function __construct()
$this->signalementUsagerOccupants = new ArrayCollection();
$this->apiUserTokens = new ArrayCollection();
$this->userPartners = new ArrayCollection();
$this->popNotifications = new ArrayCollection();
}

public function getId(): ?int
Expand Down Expand Up @@ -840,4 +847,34 @@ public function removeApiUserToken(ApiUserToken $apiUserToken): self

return $this;
}

/**
* @return Collection<int, PopNotification>
*/
public function getPopNotifications(): Collection
{
return $this->popNotifications;
}

public function addPopNotification(PopNotification $popNotification): static
{
if (!$this->popNotifications->contains($popNotification)) {
$this->popNotifications->add($popNotification);
$popNotification->setUser($this);
}

return $this;
}

public function removePopNotification(PopNotification $popNotification): static
{
if ($this->popNotifications->removeElement($popNotification)) {
// set the owning side to null (unless already changed)
if ($popNotification->getUser() === $this) {
$popNotification->setUser(null);
}
}

return $this;
}
}
73 changes: 73 additions & 0 deletions src/Manager/PopNotificationManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace App\Manager;

use App\Entity\Partner;
use App\Entity\PopNotification;
use App\Entity\User;
use Doctrine\Persistence\ManagerRegistry;

class PopNotificationManager extends Manager
{
public function __construct(
protected ManagerRegistry $managerRegistry,
string $entityName = PopNotification::class,
) {
parent::__construct($managerRegistry, $entityName);
}

public function createOrUpdatePopNotification(
User $user,
string $type,
Partner $partner,
): ?PopNotification {
if (User::STATUS_ACTIVE !== $user->getStatut()) {
return null;
}
if ($user->getPopNotifications()->count()) {
$popNotification = $user->getPopNotifications()->first();
} else {
$popNotification = new PopNotification();
$user->addPopNotification($popNotification);
$this->persist($popNotification);
$popNotification->setUser($user);
}
switch ($type) {
case 'addPartner':
$this->managePartners($popNotification, $partner, 'add');
break;
case 'removePartner':
$this->managePartners($popNotification, $partner, 'remove');
break;
}
if (empty($popNotification->getParams()['addedPartners']) && empty($popNotification->getParams()['removedPartners'])) {
$this->remove($popNotification, false);

return null;
}

return $popNotification;
}

private function managePartners(PopNotification $popNotification, Partner $partner, string $type): void
{
$keyToAdd = 'addedPartners';
$keyToRemove = 'removedPartners';
if ('remove' === $type) {
$keyToAdd = 'removedPartners';
$keyToRemove = 'addedPartners';
}
$list = isset($popNotification->getParams()[$keyToRemove]) ? $popNotification->getParams()[$keyToRemove] : [];
if (!empty($list) && in_array($partner->getId(), $list)) {
unset($list[array_search($partner->getId(), $list)]);
$params = array_merge($popNotification->getParams(), [$keyToRemove => $list]);
$popNotification->setParams($params);

return;
}
$list = isset($popNotification->getParams()[$keyToAdd]) ? $popNotification->getParams()[$keyToAdd] : [];
$list[] = $partner->getId();
$params = array_merge($popNotification->getParams(), [$keyToAdd => $list]);
$popNotification->setParams($params);
}
}
18 changes: 18 additions & 0 deletions src/Repository/PopNotificationRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Repository;

use App\Entity\PopNotification;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

/**
* @extends ServiceEntityRepository<PopNotification>
*/
class PopNotificationRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, PopNotification::class);
}
}
Loading
Loading