diff --git a/app/Console/Commands/NotifyPendingArticles.php b/app/Console/Commands/NotifyPendingArticles.php new file mode 100644 index 00000000..b34572da --- /dev/null +++ b/app/Console/Commands/NotifyPendingArticles.php @@ -0,0 +1,26 @@ +get(); + + if ($pendingArticles->isNotEmpty()) { + $notifiable->notify(new PendingArticlesNotification($pendingArticles)); + } + } +} \ No newline at end of file diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index ad9992d9..3ee86670 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -23,6 +23,7 @@ protected function schedule(Schedule $schedule): void $schedule->command('lcm:post-article-to-telegram')->everyFourHours(); $schedule->command('lcm:send-unverified-mails')->weeklyOn(1, '8:00'); $schedule->command('sitemap:generate')->daily(); + $schedule->command('lcm:notify-pending-articles')->days(2); } } @@ -30,4 +31,4 @@ protected function commands(): void { $this->load(__DIR__.'/Commands'); } -} +} \ No newline at end of file diff --git a/app/Notifications/PendingArticlesNotification.php b/app/Notifications/PendingArticlesNotification.php new file mode 100644 index 00000000..9288cd45 --- /dev/null +++ b/app/Notifications/PendingArticlesNotification.php @@ -0,0 +1,52 @@ +content(); + + return TelegramMessage::create() + ->to(config('services.telegram-bot-api.channel')) + ->content($message); + } + + private function content(): string + { + $message = __("Pending approval articles:\n\n"); + foreach ($this->pendingArticles as $article) { + $message .= __( + "[@:username](:profile_url) submitted the article [:title](:url) on: :date\n\n", [ + 'username' => $article->user?->username, + 'profile_url' => route('profile', $article->user?->username), + 'title' => $article->title, + 'url' => route('articles.show', $article->slug), + 'date' => $article->submitted_at->translatedFormat('d/m/Y') + ] + ); + } + + return $message; + } +} \ No newline at end of file diff --git a/lang/en.json b/lang/en.json index a41ce909..4ebcf659 100644 --- a/lang/en.json +++ b/lang/en.json @@ -3,5 +3,7 @@ "The given :attribute has appeared in a data leak. Please choose a different :attribute.": "The given :attribute has appeared in a data leak. Please choose a different :attribute.", "Verify Email Address": "Verify Email Address", "Please click the button below to verify your email address.": "Please click the button below to verify your email address.", - "If you did not create an account, no further action is required.": "If you did not create an account, no further action is required." -} + "If you did not create an account, no further action is required.": "If you did not create an account, no further action is required.", + "Pending approval articles:" : "Pending approval articles:", + "[@:username](:profile_url) submitted the article [:title](:url) on: :date" : "[@:username](:profile_url) submitted the article [:title](:url) on: :date" +} \ No newline at end of file diff --git a/lang/fr.json b/lang/fr.json index bbd27e39..083b53b1 100644 --- a/lang/fr.json +++ b/lang/fr.json @@ -3,5 +3,7 @@ "The given :attribute has appeared in a data leak. Please choose a different :attribute.": "Le champ :attribute donné est apparu dans une fuite de données. Veuillez choisir un autre :attribute.", "Verify Email Address": "Vérifier l'adresse e-mail", "Please click the button below to verify your email address.": "Veuillez cliquer sur le bouton ci-dessous pour vérifier votre adresse email.", - "If you did not create an account, no further action is required.": "Si vous n'avez pas créé de compte, aucune autre action n'est requise." -} + "If you did not create an account, no further action is required.": "Si vous n'avez pas créé de compte, aucune autre action n'est requise.", + "Pending approval articles:" : "Articles soumis en attente d'approbation:", + "[@:username](:profile_url) submitted the article [:title](:url) on: :date" : "[@:username](:profile_url) a soumit l'article [:title](:url) le: :date" +} \ No newline at end of file diff --git a/tests/Feature/NotifyPendingArticlesTest.php b/tests/Feature/NotifyPendingArticlesTest.php new file mode 100644 index 00000000..7e260cba --- /dev/null +++ b/tests/Feature/NotifyPendingArticlesTest.php @@ -0,0 +1,49 @@ + Notification::fake()); + +it('will send a notification when there are pending articles', function (): void { + Article::factory()->createMany([ + [ + 'submitted_at' => now(), + ], + [ + 'submitted_at' => now()->subDay(), + 'approved_at' => now(), + ], + [ + 'submitted_at' => now()->subDay(), + 'declined_at' => now(), + ], + ]); + + $this->assertDatabaseCount('articles', 3); + $this->artisan(NotifyPendingArticles::class)->assertExitCode(0); + + Notification::assertCount(1); +}); + +it('will not send a notification when there are no pending articles', function (): void { + Article::factory()->createMany([ + [ + 'submitted_at' => now()->subDay(), + 'approved_at' => now(), + ], + [ + 'submitted_at' => now()->subDay(), + 'declined_at' => now(), + ], + ]); + + $this->assertDatabaseCount('articles', 2); + $this->artisan(NotifyPendingArticles::class)->assertExitCode(0); + + Notification::assertNothingSent(); + Notification::assertCount(0); +}); \ No newline at end of file diff --git a/tests/Integration/DiscussionTest.php b/tests/Integration/DiscussionTest.php index b9ec428e..28bd459b 100644 --- a/tests/Integration/DiscussionTest.php +++ b/tests/Integration/DiscussionTest.php @@ -5,11 +5,6 @@ use App\Models\Activity; use App\Models\Discussion; use App\Models\Tag; -use Illuminate\Foundation\Testing\DatabaseMigrations; -use Illuminate\Foundation\Testing\RefreshDatabase; - -uses(RefreshDatabase::class); -uses(DatabaseMigrations::class); it('can find by slug', function (): void { Discussion::factory()->create(['slug' => 'foo']); @@ -74,4 +69,4 @@ // When providing a slug with invalid url characters, a random 5 character string is returned. expect($discussion->slug())->toMatch('/\w{5}/'); -}); +}); \ No newline at end of file diff --git a/tests/Integration/ThreadTest.php b/tests/Integration/ThreadTest.php index 71a0c0cd..0aaefeaa 100644 --- a/tests/Integration/ThreadTest.php +++ b/tests/Integration/ThreadTest.php @@ -8,11 +8,6 @@ use App\Models\Thread; use App\Models\User; use Carbon\Carbon; -use Illuminate\Foundation\Testing\DatabaseMigrations; -use Illuminate\Foundation\Testing\RefreshDatabase; - -uses(RefreshDatabase::class); -uses(DatabaseMigrations::class); it('can find by slug', function (): void { Thread::factory()->create(['slug' => 'foo']); @@ -192,4 +187,4 @@ function createActiveThread(): Thread $reply->save(); return $thread; -} +} \ No newline at end of file