-
Notifications
You must be signed in to change notification settings - Fork 17
Feature/lar 10 schedule notification #151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
StevyMarlino
wants to merge
2
commits into
laravelcm:develop
from
StevyMarlino:feature/lar-10-schedule-notification
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,33 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Console\Commands; | ||
|
|
||
| use App\Models\Article; | ||
| use App\Notifications\PendingArticlesNotification; | ||
| use Illuminate\Console\Command; | ||
| use Illuminate\Notifications\AnonymousNotifiable; | ||
| use Illuminate\Support\Facades\Notification; | ||
|
|
||
| final class NotifyPendingArticles extends Command | ||
| { | ||
| protected $signature = 'lcm:notify-pending-articles'; | ||
|
|
||
| protected $description = 'Send a Telegram notification for articles that are submitted but neither approved nor declined'; | ||
|
|
||
| public function handle(AnonymousNotifiable $notifiable): void | ||
| { | ||
| // Récupérer les articles soumis mais non approuvés ni déclinés via le scope awaitingApproval | ||
| $pendingArticles = Article::awaitingApproval()->get(); | ||
|
|
||
| if ($pendingArticles->isEmpty()) { | ||
StevyMarlino marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $this->info('❌ No pending articles found.'); | ||
| return; | ||
| } | ||
|
|
||
| //Envoi de la notification via télégram | ||
| $notifiable->notify(new PendingArticlesNotification($pendingArticles)); | ||
| $this->info('✅ Notification sent successfully.'); | ||
StevyMarlino marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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,45 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Notifications; | ||
|
|
||
| use Illuminate\Bus\Queueable; | ||
| use Illuminate\Database\Eloquent\Collection; | ||
| use Illuminate\Notifications\Notification; | ||
| use NotificationChannels\Telegram\TelegramChannel; | ||
| use NotificationChannels\Telegram\TelegramMessage; | ||
|
|
||
| final class PendingArticlesNotification extends Notification | ||
| { | ||
| use Queueable; | ||
|
|
||
| public function __construct(public Collection $pendingArticles) {} | ||
|
|
||
| public function via(mixed $notifiable): array | ||
| { | ||
| return [TelegramChannel::class]; | ||
| } | ||
|
|
||
| public function toTelegram(): TelegramMessage | ||
| { | ||
| $message = $this->content(); | ||
|
|
||
| return TelegramMessage::create() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Il faut t'inspirer de ce qui a été fait dans le classe |
||
| ->to(config('services.telegram-bot-api.chat_id')) | ||
| ->content($message); | ||
| } | ||
|
|
||
| private function content(): string | ||
| { | ||
| $message = "Articles soumis en attente d'approbation: \n"; | ||
StevyMarlino marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| foreach ($this->pendingArticles as $article) { | ||
| $url = route('articles.show', $article->slug); | ||
| $message .= "• Titre: [{$article->title}]({$url})\n"; | ||
| $message .= '• Par: [@'.$article->user?->username.']('.route('profile', $article->user?->username).")\n"; | ||
| $message .= "• Soumis le: {$article->submitted_at->format('D/m/Y')}\n\n"; | ||
| } | ||
|
|
||
| return $message; | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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,39 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use App\Console\Commands\NotifyPendingArticles; | ||
| use App\Models\Article; | ||
| use App\Notifications\PendingArticlesNotification; | ||
| use Illuminate\Notifications\AnonymousNotifiable; | ||
| use Illuminate\Support\Facades\Notification; | ||
|
|
||
| it('will send a notification when there are pending articles', function (): void { | ||
| // Simuler l'envoi d'une fake notification | ||
| Notification::fake(); | ||
|
|
||
| //création de l'article et on s'assure qu'elle est en bd. | ||
| $articles = Article::factory()->create(['submitted_at' => now()]); | ||
StevyMarlino marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $this->assertDatabaseCount('articles', 1); | ||
|
|
||
| // Exécution de la commande lcm:notify-pending-articles | ||
| $this->artisan(NotifyPendingArticles::class)->expectsOutput('✅ Notification sent successfully.') | ||
| ->assertExitCode(0); | ||
|
|
||
| // Vérification qu'une notification à été envoyée pour l'article en attente | ||
| Notification::assertSentTo( | ||
| new AnonymousNotifiable(), | ||
| PendingArticlesNotification::class, | ||
| fn ($notification) => $notification->pendingArticles->contains($articles) | ||
| ); | ||
| }); | ||
|
|
||
| it('will not send a notification when there are no pending articles', function (): void { | ||
| Notification::fake(); | ||
|
|
||
| $this->artisan(NotifyPendingArticles::class)->expectsOutput('❌ No pending articles found.') | ||
| ->assertExitCode(0); | ||
|
|
||
| // Vérifier qu'aucune notification n'a été envoyée | ||
| Notification::assertNothingSent(); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.