fix: make sure approved invites are not deleted daily
even though people would have time to use approved invites (24 hrs at least), it would be better to delete them when they expire instead. Signed-off-by: Miguel Nogueira <me@nogueira.codes>
This commit is contained in:
@@ -6,6 +6,7 @@ use App\Invitation;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Queue\Queueable;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use App\Mail\InviteExpiringSoon;
|
||||
|
||||
@@ -26,27 +27,37 @@ class ExpiredInviteCleanup implements ShouldQueue
|
||||
*/
|
||||
public function handle(): void
|
||||
{
|
||||
// 1. Notify invites expiring within the next 24 hours that haven't been notified yet
|
||||
Invitation::where('status', 'pending')
|
||||
Log::info('Cleaning up expired invites.');
|
||||
|
||||
$notified = 0;
|
||||
$deleted = 0;
|
||||
|
||||
Invitation::where('status', 'approved')
|
||||
->where('notified', false)
|
||||
->whereBetween('expiration', [Carbon::now(), Carbon::now()->addDay()])
|
||||
->chunkById(100, function ($invites) {
|
||||
->chunkById(100, function ($invites) use (&$notified) {
|
||||
foreach ($invites as $invite) {
|
||||
Mail::to($invite->requestor_email)
|
||||
->send(new InviteExpiringSoon($invite));
|
||||
->queue(new InviteExpiringSoon($invite));
|
||||
|
||||
$notified++;
|
||||
$invite->notified = true;
|
||||
$invite->save();
|
||||
|
||||
Log::debug("Notified approved invite {$invite->invitation_code}, sent to {$invite->requestor_email}.");
|
||||
}
|
||||
});
|
||||
|
||||
// 2. Delete invites that have actually expired
|
||||
Invitation::where('status', 'pending')
|
||||
->where('expiration', '<', Carbon::now())
|
||||
->chunkById(100, function ($invites) {
|
||||
Invitation::where('expiration', '<', Carbon::now())
|
||||
->where('status', '!=', 'denied')
|
||||
->chunkById(100, function ($invites) use (&$deleted, &$notified) {
|
||||
foreach ($invites as $invite) {
|
||||
Log::debug("Deleted invite {$invite->invitation_code} for {$invite->requestor_email}.");
|
||||
$invite->delete();
|
||||
$deleted++;
|
||||
}
|
||||
});
|
||||
|
||||
Log::info("Deleted {$deleted} invites and notified {$notified} approved invites close to expiration.");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user