-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
close polls automatically & notify user
- Loading branch information
Showing
9 changed files
with
277 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
app/Notifications/Email/OwnPollClosedEmailNotification.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
59
app/Notifications/Pr0gramm/OwnPollClosedPr0grammNotification.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 [ | ||
// | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters