2021-09-29 12:04:46 +02:00
< ? php
namespace App\Jobs ;
use App\Enums\TicketStatus ;
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.' ;
2022-03-29 16:57:54 +02:00
$tickets = Ticket :: where ( function ( $q ) {
2021-09-29 12:04:46 +02:00
$q -> where ( 'status' , TicketStatus :: production ())
-> orWhere ( 'status' , TicketStatus :: processed ());
}) -> 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 ) {
2022-03-29 16:57:54 +02:00
$date = $ticket -> messages -> sortByDesc ( 'updated_at' ) -> first () -> updated_at ? ? $ticket -> updated_at ;
2021-09-29 12:04:46 +02:00
$limitDate = $date -> addDays ( $warningDays );
if ( intval ( Carbon :: now () -> diff ( $limitDate ) -> format ( '%a' )) >= $warningDays ) {
2022-03-29 17:06:31 +02:00
event ( new NotifySubscriptions ( $ticket , $messageToAll , collect ([ $ticket -> user ])));
2021-09-29 12:04:46 +02:00
}
event ( new NotifyTicketCreator ( $ticket , sprintf (
$messageToUser ,
intval ( $date -> diff ( Carbon :: now () -> subDays ( $warningDays )) -> format ( '%a' ) + 1 )
)));
}
}
}