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>
64 lines
1.8 KiB
PHP
64 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
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;
|
|
|
|
class ExpiredInviteCleanup implements ShouldQueue
|
|
{
|
|
use Queueable;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*/
|
|
public function handle(): void
|
|
{
|
|
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) use (&$notified) {
|
|
foreach ($invites as $invite) {
|
|
Mail::to($invite->requestor_email)
|
|
->queue(new InviteExpiringSoon($invite));
|
|
|
|
$notified++;
|
|
$invite->notified = true;
|
|
$invite->save();
|
|
|
|
Log::debug("Notified approved invite {$invite->invitation_code}, sent to {$invite->requestor_email}.");
|
|
}
|
|
});
|
|
|
|
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.");
|
|
}
|
|
}
|