notification / add email: fix entity Notification

This commit is contained in:
Julien Fastré 2022-04-13 21:33:18 +02:00
parent ef9fd80ad5
commit a8db07a383
3 changed files with 67 additions and 2 deletions

View File

@ -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)) {

View File

@ -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());
}
}

View File

@ -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,