44 lines
1.8 KiB
PHP
44 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Enums\Tickets\TicketStatusEnum;
|
|
use App\Events\NotifySubscriptions;
|
|
use App\Events\NotifyTicketCreator;
|
|
use App\Models\Ticket;
|
|
use Illuminate\Support\Carbon;
|
|
|
|
class WarnBeforeAutoCloseTicket
|
|
{
|
|
/**
|
|
* Execute the job.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function handle()
|
|
{
|
|
$warningDays = 7;
|
|
// phpcs:ignore Generic.Files.LineLength.TooLong
|
|
$messageToAll = "En l'absence d'activitée, ou de réponse de la part du requérant ce ticket sera automatiquement clos et archivé dans $warningDays jours.";
|
|
// phpcs:ignore Generic.Files.LineLength.TooLong
|
|
$messageToUser = 'En l\'absence d\'activitée, ou de réponse de votre part ce ticket sera automatiquement clos et archivé dans %s jours.';
|
|
$tickets = Ticket::where(function ($q) {
|
|
$q->where('status', TicketStatusEnum::production)
|
|
->orWhere('status', TicketStatusEnum::waitingValidation);
|
|
})->whereDate('updated_at', '<=', Carbon::now()->subDays($warningDays))
|
|
->whereDoesntHave('messages', function ($q) use ($warningDays) {
|
|
$q->whereDate('updated_at', '>', Carbon::now()->subDays($warningDays));
|
|
})->get();
|
|
foreach ($tickets as $ticket) {
|
|
$date = $ticket->messages->sortByDesc('updated_at')->first()->updated_at ?? $ticket->updated_at;
|
|
$limitDate = $date->addDays($warningDays);
|
|
if (intval(Carbon::now()->diff($limitDate)->format('%a')) >= $warningDays) {
|
|
event(new NotifySubscriptions($ticket, $messageToAll, collect([$ticket->user])));
|
|
}
|
|
event(new NotifyTicketCreator($ticket, sprintf(
|
|
$messageToUser,
|
|
intval($date->diff(Carbon::now()->subDays($warningDays))->format('%a') + 1)
|
|
)));
|
|
}
|
|
}
|
|
}
|