Skip to content

Commit

Permalink
[Event] send push notification when live starts
Browse files Browse the repository at this point in the history
  • Loading branch information
ottaviano committed Feb 10, 2025
1 parent 4276d6e commit 139aa1c
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 1 deletion.
19 changes: 19 additions & 0 deletions migrations/2025/Version20250210161256.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Migrations;

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

final class Version20250210161256 extends AbstractMigration
{
public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE events ADD push_sent_at DATETIME DEFAULT NULL');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE `events` DROP push_sent_at');
}
}
39 changes: 39 additions & 0 deletions src/Command/EventPushNotificationCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\Command;

use App\JeMengage\Push\Command\EventLiveBeginNotificationCommand;
use App\Repository\Event\EventRepository;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Messenger\MessageBusInterface;

#[AsCommand(
name: 'app:events:push',
description: 'This command finds upcoming events and send push notification',
)]
class EventPushNotificationCommand extends Command
{
public function __construct(
private readonly MessageBusInterface $bus,
private readonly EventRepository $eventRepository,
) {
parent::__construct();
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$events = $this->eventRepository->findWithLiveToNotify();

foreach ($events as $event) {
$this->bus->dispatch(new EventLiveBeginNotificationCommand($event->getUuid()));
}

return self::SUCCESS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
name: 'app:events:remind',
description: 'This command finds upcoming events and send reminders',
)]
class RemindEventCommand extends Command
class EventReminderCommand extends Command
{
private SymfonyStyle $io;

Expand Down
3 changes: 3 additions & 0 deletions src/Entity/Event/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ class Event implements ReportableInterface, GeoPointInterface, AddressHolderInte
#[ORM\Column(type: 'boolean', options: ['default' => false])]
private bool $national = false;

#[ORM\Column(type: 'datetime', nullable: true)]
public ?\DateTime $pushSentAt = null;

/**
* @var string|null
*/
Expand Down
13 changes: 13 additions & 0 deletions src/JeMengage/Push/Command/EventLiveBeginNotificationCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\JeMengage\Push\Command;

use App\Entity\Event\Event;

class EventLiveBeginNotificationCommand extends AbstractSendNotificationCommand
{
public function getClass(): string
{
return Event::class;
}
}
14 changes: 14 additions & 0 deletions src/JeMengage/Push/Notification/EventLiveBeginNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\JeMengage\Push\Notification;

use App\Entity\Event\Event;
use App\Firebase\Notification\AbstractMulticastNotification;

class EventLiveBeginNotification extends AbstractMulticastNotification
{
public static function create(Event $event): self
{
return new self('🔴 On est en direct !', $event->getName());
}
}
4 changes: 4 additions & 0 deletions src/JeMengage/Push/NotificationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ private function initiateNotification(NotificationObjectInterface $object, Comma
return Notification\EventReminderNotification::create($object);
}

if ($command instanceof Command\EventLiveBeginNotificationCommand) {
return Notification\EventLiveBeginNotification::create($object);
}

if ($command instanceof Command\NewsCreatedNotificationCommand) {
return Notification\NewsCreatedNotification::create($object);
}
Expand Down
18 changes: 18 additions & 0 deletions src/Repository/Event/EventRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,4 +521,22 @@ public function findWithLiveStream(): array
->getResult()
;
}

public function findWithLiveToNotify(): array
{
return $this->createQueryBuilder('e')
->where('e.status = :status')
->andWhere('e.national = 1')
->andWhere('e.liveUrl LIKE :live_url')
->andWhere('e.pushSentAt IS NULL')
->andWhere('e.beginAt < :now AND e.finishAt >= :now')
->setParameters([
'status' => Event::STATUS_SCHEDULED,
'live_url' => 'https://vimeo.com/%',
'now' => $now = new \DateTime('now'),
])
->getQuery()
->getResult()
;
}
}

0 comments on commit 139aa1c

Please sign in to comment.