Skip to content

Commit ed6d01d

Browse files
committed
fix: coding error in NotifyProjectClosureDueJob.php and add rate limiter
1 parent bbbe106 commit ed6d01d

4 files changed

Lines changed: 25 additions & 2 deletions

File tree

app/Jobs/NotifyProjectClosureDueJob.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace App\Jobs;
44

55
use App\Models\Project;
6+
use App\Models\ProjectParticipant;
67
use App\Notifications\ClosureDueNotification;
78
use Illuminate\Bus\Queueable;
89
use Illuminate\Contracts\Queue\ShouldQueue;
10+
use Illuminate\Database\Eloquent\Collection;
911
use Illuminate\Foundation\Bus\Dispatchable;
1012
use Illuminate\Queue\InteractsWithQueue;
1113
use Illuminate\Queue\SerializesModels;
@@ -18,13 +20,21 @@ public function handle(): void {
1820
Project::where('created_at', '>=', now()->subYear())
1921
->where('year', '>=', 2567)
2022
->where('period_end', '<=', now()->subDays(2))
23+
->where('period_end', '>=', now()->subDays(20))
2124
->whereNull('closure_reminded_at')
2225
->get()
23-
->filter(fn(Project $project) => $project->canSubmitClosure() and $project->documents()->where('tag', 'summary')->isEmpty())
26+
->filter(fn(Project $project) => $project->canSubmitClosure() and $project->documents()->where('tag', 'summary')->doesntExist())
2427
->each(function (Project $project) {
2528
$project->participants()->with('user')->get()
2629
->reject(fn($participant) => $participant->type == 'attendee' and $participant->user?->student_id < 6700000000)
27-
->each(fn($participant) => $participant->user->notify(new ClosureDueNotification($project, $participant)));
30+
->pipe(function (Collection $participants) {
31+
// select all organizers + other participants not more than 30 people
32+
$organizers = $participants->where('type', 'organizer');
33+
$others = $participants->where('type', '!=', 'organizer')->take(max(0, 30 - $organizers->count()));
34+
35+
return $organizers->merge($others);
36+
})
37+
->each(fn(ProjectParticipant $participant) => $participant->user->notify(new ClosureDueNotification($project, $participant)));
2838
$project->update(['closure_reminded_at' => now()]);
2939
});
3040
}

app/Notifications/ClosureApprovalNotification.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Contracts\Queue\ShouldQueue;
88
use Illuminate\Notifications\Messages\MailMessage;
99
use Illuminate\Notifications\Notification;
10+
use Illuminate\Queue\Middleware\RateLimited;
1011

1112
/**
1213
* Notification sent to project organizers when the faculty staff updates the remark on project closure.
@@ -17,6 +18,10 @@ class ClosureApprovalNotification extends Notification implements ShouldQueue {
1718
public function __construct(public Project $project) {
1819
}
1920

21+
public function middleware(): array {
22+
return [(new RateLimited('mails'))->releaseAfter(3600)];
23+
}
24+
2025
public function via($notifiable): array {
2126
return ['mail'];
2227
}

app/Notifications/ClosureDueNotification.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@
88
use Illuminate\Contracts\Queue\ShouldQueue;
99
use Illuminate\Notifications\Messages\MailMessage;
1010
use Illuminate\Notifications\Notification;
11+
use Illuminate\Queue\Middleware\RateLimited;
1112

1213
class ClosureDueNotification extends Notification implements ShouldQueue {
1314
use Queueable;
1415

1516
public function __construct(public Project $project, public ProjectParticipant $participant) {
1617
}
1718

19+
public function middleware(): array {
20+
return [(new RateLimited('mails'))->releaseAfter(3600)];
21+
}
22+
1823
public function via($notifiable): array {
1924
return ['mail'];
2025
}

app/Providers/AppServiceProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace App\Providers;
44

55
use App\Checks\StorageCheck;
6+
use Illuminate\Cache\RateLimiting\Limit;
7+
use Illuminate\Support\Facades\RateLimiter;
68
use Illuminate\Support\ServiceProvider;
79
use Spatie\Health\Checks\Checks\CacheCheck;
810
use Spatie\Health\Checks\Checks\DatabaseCheck;
@@ -48,5 +50,6 @@ public function boot()
4850
UsedDiskSpaceCheck::new()->warnWhenUsedSpaceIsAbovePercentage(90)
4951
->failWhenUsedSpaceIsAbovePercentage(95),
5052
]);
53+
RateLimiter::for('mails', fn() => Limit::perHour(100));
5154
}
5255
}

0 commit comments

Comments
 (0)