Skip to content

Commit 1953c96

Browse files
Refactor scheduled tasks to use command calls with configurable notif… (#420)
* Refactor scheduled tasks to use command calls with configurable notification periods and add task names. * fix: update project expiration notification logic to use settings for days before expiration and reminder * Refactor: clean up unused imports and remove redundant whitespace in Console Kernel * wip --------- Co-authored-by: Andrei Ioniță <hi@andrei.io>
1 parent 3b34442 commit 1953c96

2 files changed

Lines changed: 27 additions & 26 deletions

File tree

app/Console/Commands/EndProjectPeriod.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
namespace App\Console\Commands;
66

77
use App\Models\Project;
8+
use App\Models\Setting;
89
use App\Notifications\Ngo\ProjectEndingNotification;
910
use Illuminate\Console\Command;
11+
use Illuminate\Database\Eloquent\Builder;
12+
use Illuminate\Support\Facades\Notification;
1013

1114
class EndProjectPeriod extends Command
1215
{
@@ -15,7 +18,7 @@ class EndProjectPeriod extends Command
1518
*
1619
* @var string
1720
*/
18-
protected $signature = 'app:notification-end-project-period {--days=7}';
21+
protected $signature = 'app:notification-end-project-period';
1922

2023
/**
2124
* The console command description.
@@ -29,20 +32,28 @@ class EndProjectPeriod extends Command
2932
*/
3033
public function handle(): void
3134
{
32-
$this->info('Ending project period...');
33-
$daysBeforeEnding = (int) $this->option('days') ?? 7;
34-
$this->info("Ending period {$daysBeforeEnding}...");
35-
$projects = Project::withoutEagerLoads()->with(['organization'])
35+
$this->projectsEndingNotification('project_expiration_notification_days_before', 7);
36+
$this->projectsEndingNotification('project_expiration_notification_days_before_reminder', 2);
37+
}
38+
39+
private function projectsEndingNotification(string $setting, int $fallbackDays)
40+
{
41+
$days = (int) (Setting::value($setting) ?? $fallbackDays);
42+
43+
$this->info('Notifying projects ending in ' . $days . ' days');
44+
45+
$projects = Project::withoutEagerLoads()
46+
->with([
47+
'organization.users' => function (Builder $query) {
48+
$query->whereNotNull('email_verified_at');
49+
},
50+
])
3651
->whereIsApproved()
37-
->whereDate('end', now()->addDays($daysBeforeEnding))
52+
->whereDate('end', now()->addDays($days))
3853
->get();
39-
$projects->each(function (Project $project) use ($daysBeforeEnding) {
40-
41-
$users = $project->organization->load('users')->users->filter(function ($user) {
42-
return $user->hasVerifiedEmail();
43-
});
44-
\Notification::send($users, new ProjectEndingNotification($project, $daysBeforeEnding));
4554

55+
$projects->each(function (Project $project) use ($days) {
56+
Notification::send($project->organization->users, new ProjectEndingNotification($project, $days));
4657
});
4758
}
4859
}

app/Console/Kernel.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
namespace App\Console;
66

77
use App\Jobs\ProcessAuthorizedTransactionsJob;
8-
use App\Models\Setting;
98
use Illuminate\Console\Scheduling\Schedule;
109
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
11-
use Illuminate\Support\Facades\Artisan;
1210

1311
class Kernel extends ConsoleKernel
1412
{
@@ -19,29 +17,21 @@ protected function schedule(Schedule $schedule): void
1917
{
2018
$schedule->command('model:prune')
2119
->daily()
20+
->name('model-prune')
2221
->onOneServer()
2322
->sentryMonitor('model-prune');
2423

2524
$schedule->job(ProcessAuthorizedTransactionsJob::class)
2625
->everyFourHours()
26+
->name('transactions-process-authorized')
2727
->onOneServer()
2828
->sentryMonitor('process-authorized-transactions-job');
2929

30-
$schedule->call(function () {
31-
$days = (int) (Setting::value('project_expiration_notification_days_before') ?? 10);
32-
Artisan::call('app:notification-end-project-period', ['--days' => $days]);
33-
})
30+
$schedule->command('app:notification-end-project-period', ['--days' => $daysBeforeProjectExpiration])
3431
->dailyAt('09:00')
32+
->name('notification-end-project-period')
3533
->onOneServer()
3634
->sentryMonitor('notification-end-project-period');
37-
38-
$schedule->call(function () {
39-
$days = (int) (Setting::value('project_expiration_notification_days_before_reminder') ?? 2);
40-
Artisan::call('app:notification-end-project-period', ['--days' => $days]);
41-
})
42-
->dailyAt('10:00')
43-
->onOneServer()
44-
->sentryMonitor('notification-end-project-period-reminder');
4535
}
4636

4737
/**

0 commit comments

Comments
 (0)