Skip to content

Commit

Permalink
close polls automatically & notify user
Browse files Browse the repository at this point in the history
  • Loading branch information
Tschucki committed Mar 5, 2024
1 parent e3ec152 commit 259bbca
Show file tree
Hide file tree
Showing 9 changed files with 277 additions and 16 deletions.
44 changes: 44 additions & 0 deletions app/Console/Commands/CheckForClosedPollsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace App\Console\Commands;

use App\Jobs\SendOwnPollClosedEmailNotification;
use App\Jobs\SendOwnPollClosedPr0GrammNotification;
use App\Models\Polls\Poll;
use Illuminate\Console\Command;

class CheckForClosedPollsCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:check-for-closed-polls-command';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Checks for closed polls and sends notifications to the users.';

/**
* Execute the console command.
*/
public function handle(): void
{
Poll::where('published_at', '<', now())->where('in_review', false)->where('visible_to_public', true)->each(function (Poll $poll) {
$this->info('Close '.$poll->title);
if ($poll->isClosed()) {
$poll->update([
'visible_to_public' => false,
]);
SendOwnPollClosedEmailNotification::dispatch($poll, $poll->user);
SendOwnPollClosedPr0grammNotification::dispatch($poll, $poll->user);
}
});
}
}
2 changes: 2 additions & 0 deletions app/Console/Commands/LoginToPr0gramm.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class LoginToPr0gramm extends Command
*/
public function handle(): int
{
Pr0grammApi::logout();

$login = Pr0grammApi::login(config('services.pr0gramm.username'), config('services.pr0gramm.password'));
if (isset($login['success'])) {
$this->info('Login successful');
Expand Down
3 changes: 2 additions & 1 deletion app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule): void
{
// $schedule->command('inspire')->hourly();
$schedule->command('app:check-for-closed-polls-command')->hourly();
$schedule->command('app:login-to-pr0gramm')->hourly();
}

/**
Expand Down
48 changes: 48 additions & 0 deletions app/Jobs/SendOwnPollClosedEmailNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace App\Jobs;

use App\Models\NotificationType;
use App\Models\Polls\Poll;
use App\Models\User;
use App\Notifications\Email\OwnPollClosedEmailNotification;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Notification;

class SendOwnPollClosedEmailNotification implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

private Poll $poll;

private User $user;

public int $tries = 15;

public int $backoff = 120;

/**
* Create a new job instance.
*/
public function __construct(Poll $poll, User $user)
{
$this->poll = $poll;
$this->user = $user;
}

/**
* Execute the job.
*/
public function handle(): void
{
if ($this->user->email && in_array('mail', $this->user->getNotificationRoutesForType(NotificationType::where('identifier', \App\Enums\NotificationType::OWNPOLLHASENDED)->first()), true)) {
Notification::route('mail', [$this->user->email => $this->user->name])->notify(new OwnPollClosedEmailNotification($this->poll));
}
}
}
47 changes: 47 additions & 0 deletions app/Jobs/SendOwnPollClosedPr0grammNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace App\Jobs;

use App\Models\NotificationType;
use App\Models\Polls\Poll;
use App\Models\User;
use App\Notifications\Pr0gramm\PollAcceptedPr0grammNotification;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class SendOwnPollClosedPr0grammNotification implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

private Poll $poll;

private User $user;

public int $tries = 15;

public int $backoff = 120;

/**
* Create a new job instance.
*/
public function __construct(Poll $poll, User $user)
{
$this->poll = $poll;
$this->user = $user;
}

/**
* Execute the job.
*/
public function handle(): void
{
if (in_array('pr0gramm', $this->user->getNotificationRoutesForType(NotificationType::where('identifier', \App\Enums\NotificationType::OWNPOLLHASENDED)->first()), true)) {
$this->user->notify(new PollAcceptedPr0grammNotification($this->poll));
}
}
}
10 changes: 10 additions & 0 deletions app/Models/Abstracts/Poll.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,18 @@ public function isVisibleForPublic(): bool
return $this->isApproved() && ! $this->isInReview() && $this->visible_to_public;
}

public function isClosed(): bool
{
if ($this->published_at !== null && $this->closes_after !== null) {
return Carbon::make($this->published_at)?->add($this->closes_after)->isPast();
}

return false;
}

public function resultsArePublic(): bool
{
//TODO: Implement
if ($this->published_at !== null && $this->closes_after !== null) {
return Carbon::make($this->published_at)?->add($this->closes_after)->isPast();
}
Expand Down
65 changes: 65 additions & 0 deletions app/Notifications/Email/OwnPollClosedEmailNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace App\Notifications\Email;

use App\Models\Polls\Poll;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class OwnPollClosedEmailNotification extends Notification
{
use Queueable;

private Poll $poll;

/**
* Create a new notification instance.
*/
public function __construct(Poll $poll)
{
$this->poll = $poll;
}

/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
public function via(object $notifiable): array
{
return [
'mail',
];
}

/**
* Get the mail representation of the notification.
*/
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage)
->subject('📊 Deine Umfrage ist zu Ende! #'.$this->poll->getKey())
->greeting('Hallo,')
->line('Deine Umfrage "'.$this->poll->title.'" ist zu Ende.')
->line('Du kannst jetzt einen pr0-Post mit den Ergebnissen erstellen.')
->action('Zur Auswerung', url(route('filament.pr0p0ll.resources.my-polls.results', [
'record' => $this->poll->getKey(),
])))
->line('Danke, dass du Pr0p0ll nutzt.');
}

/**
* Get the array representation of the notification.
*
* @return array<string, mixed>
*/
public function toArray(object $notifiable): array
{
return [
//
];
}
}
59 changes: 59 additions & 0 deletions app/Notifications/Pr0gramm/OwnPollClosedPr0grammNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace App\Notifications\Pr0gramm;

use App\Models\Polls\Poll;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use NotificationChannels\Pr0gramm\Pr0grammChannel;

class OwnPollClosedPr0grammNotification extends Notification
{
use Queueable;

private Poll $poll;

/**
* Create a new notification instance.
*/
public function __construct(Poll $poll)
{
$this->poll = $poll;
}

/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
public function via($notifiable): array
{
return [
Pr0grammChannel::class,
];
}

public function toPr0gramm($notifiable): string
{
$url = route('filament.pr0p0ll.resources.my-polls.results', [
'record' => $this->poll->getKey(),
]);
$title = $this->poll->title;

return "Hallo, deine Umfrage ({$title}) ist nun zu Ende. Du kannst jetzt einen pr0-Post erstellen.\n".'Auswertung: '.$url;
}

/**
* Get the array representation of the notification.
*
* @return array<string, mixed>
*/
public function toArray(object $notifiable): array
{
return [
//
];
}
}
15 changes: 0 additions & 15 deletions app/Notifications/Pr0gramm/PollAcceptedPr0grammNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use App\Models\Polls\Poll;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use NotificationChannels\Pr0gramm\Pr0grammChannel;

Expand Down Expand Up @@ -36,20 +35,6 @@ public function via($notifiable): array
];
}

/**
* Get the mail representation of the notification.
*/
public function toMail($notifiable): MailMessage
{
return (new MailMessage)
->greeting('Hallo,')
->line('Deine Umfrage mit dem Titel "'.$this->poll->title.'" wurde genehmigt und ist nun öffentlich sichtbar.')
->action('An Umfrage teilnehmen', url(route('filament.pr0p0ll.resources.public-polls.teilnehmen', [
'record' => $this->poll->getKey(),
])))
->line('Danke, dass du Pr0p0ll nutzt.');
}

public function toPr0gramm($notifiable): string
{
$url = route('filament.pr0p0ll.resources.public-polls.teilnehmen', [
Expand Down

0 comments on commit 259bbca

Please sign in to comment.