diff --git a/src/Bundle/ChillMainBundle/Entity/Notification.php b/src/Bundle/ChillMainBundle/Entity/Notification.php index 5257a7a90..9b2dded40 100644 --- a/src/Bundle/ChillMainBundle/Entity/Notification.php +++ b/src/Bundle/ChillMainBundle/Entity/Notification.php @@ -110,7 +110,14 @@ class Notification implements TrackUpdateInterface * @var array|string[] * @ORM\Column(type="json") */ - public array $adressesEmails = []; + private array $addressesEmails = []; + + /** + * a list of emails adresses which were added to the notification + * + * @var array|string[] + */ + private array $addressesEmailsAdded = []; /** * @ORM\Column(type="text", nullable=false) @@ -126,6 +133,46 @@ class Notification implements TrackUpdateInterface $this->accessKey = bin2hex(openssl_random_pseudo_bytes(24)); } + /** + * @return array|string[] + */ + public function getAddressesEmails(): array + { + return $this->addressesEmails; + } + + /** + * @return array|string[] + */ + public function getAddressesEmailsAdded(): array + { + return $this->addressesEmailsAdded; + } + + /** + * @return string + */ + public function getAccessKey(): string + { + return $this->accessKey; + } + + public function addAddressesEmail(string $email) + { + if (!in_array($email, $this->addressesEmails)) { + $this->addressesEmails[] = $email; + $this->addressesEmailsAdded[] = $email; + } + } + + public function removeAddressesEmail(string $email) + { + if (in_array($email, $this->addressesEmails)) { + $this->addressesEmails = array_filter($this->addressesEmails, fn ($e) => $e !== $email); + $this->addressesEmailsAdded = array_filter($this->addressesEmailsAdded, fn ($e) => $e !== $email); + } + } + public function addAddressee(User $addressee): self { if (!$this->addressees->contains($addressee)) { diff --git a/src/Bundle/ChillMainBundle/Tests/Entity/NotificationTest.php b/src/Bundle/ChillMainBundle/Tests/Entity/NotificationTest.php index 8110fa11d..9fc0d6cb2 100644 --- a/src/Bundle/ChillMainBundle/Tests/Entity/NotificationTest.php +++ b/src/Bundle/ChillMainBundle/Tests/Entity/NotificationTest.php @@ -122,4 +122,22 @@ final class NotificationTest extends KernelTestCase $this->assertContains($addresseeId, $unreadIds); } } + + public function testAddressesEmail(): void + { + $notification = new Notification(); + + $notification->addAddressesEmail('test'); + $notification->addAddressesEmail('other'); + + $this->assertContains('test', $notification->getAddressesEmails()); + $this->assertContains('other', $notification->getAddressesEmails()); + $this->assertContains('test', $notification->getAddressesEmailsAdded()); + $this->assertContains('other', $notification->getAddressesEmailsAdded()); + + $notification->removeAddressesEmail('other'); + + $this->assertNotContains('other', $notification->getAddressesEmails()); + $this->assertNotContains('other', $notification->getAddressesEmailsAdded()); + } } diff --git a/src/Bundle/ChillMainBundle/migrations/Version20220413154743.php b/src/Bundle/ChillMainBundle/migrations/Version20220413154743.php index 88a31c92c..87da6a7f8 100644 --- a/src/Bundle/ChillMainBundle/migrations/Version20220413154743.php +++ b/src/Bundle/ChillMainBundle/migrations/Version20220413154743.php @@ -16,7 +16,7 @@ final class Version20220413154743 extends AbstractMigration public function up(Schema $schema): void { - $this->addSql('ALTER TABLE chill_main_notification ADD adressesEmails JSON NOT NULL DEFAULT \'[]\';'); + $this->addSql('ALTER TABLE chill_main_notification ADD addressesEmails JSON NOT NULL DEFAULT \'[]\';'); $this->addSql('ALTER TABLE chill_main_notification ADD accessKey TEXT DEFAULT NULL'); $this->addSql('WITH randoms AS (select n.id,