2021-06-08 17:02:35 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
|
|
|
|
use App\Models\Media;
|
2021-06-09 12:18:02 +02:00
|
|
|
use App\Models\Setting;
|
2021-06-08 17:02:35 +02:00
|
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
|
use App\Mail\SendMediaReportMail;
|
|
|
|
|
use App\Models\Sujet;
|
|
|
|
|
use Illuminate\Support\Carbon;
|
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
|
|
|
|
|
|
class SendMediasModificationsReport implements ShouldQueue
|
|
|
|
|
{
|
2022-04-19 16:07:27 +02:00
|
|
|
use Dispatchable;
|
|
|
|
|
use InteractsWithQueue;
|
|
|
|
|
use Queueable;
|
|
|
|
|
use SerializesModels;
|
2021-06-08 17:02:35 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Execute the job.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function handle()
|
|
|
|
|
{
|
2022-06-20 14:57:58 +02:00
|
|
|
// Gather Medias to report.
|
|
|
|
|
$subjectsToFetch = [
|
|
|
|
|
// * Marques
|
|
|
|
|
2,
|
|
|
|
|
// * Sociétés
|
|
|
|
|
4,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$yesterday = Carbon::now()->subDay(1);
|
2021-06-08 17:02:35 +02:00
|
|
|
|
2022-06-20 14:57:58 +02:00
|
|
|
/** @var \Illuminate\Database\Eloquent\Collection<\App\Models\Sujet> */
|
|
|
|
|
$sujets = Sujet::whereIn('codeSujet', $subjectsToFetch)->get();
|
2021-06-08 17:02:35 +02:00
|
|
|
|
2022-06-20 14:57:58 +02:00
|
|
|
/** @var \Illuminate\Database\Eloquent\Collection<\App\Models\Media> */
|
|
|
|
|
$mediaList = Media::whereDate('updated_at', '>=', $yesterday)
|
2021-06-08 17:02:35 +02:00
|
|
|
->whereIn('sujet_id', [2,4])
|
|
|
|
|
->get();
|
2022-06-20 14:57:58 +02:00
|
|
|
/** @var \App\Models\Setting|null Send mail */
|
2021-06-09 12:18:02 +02:00
|
|
|
$toMailSetting = Setting::where('key', 'media_report_to_mail')->first();
|
2022-06-20 14:57:58 +02:00
|
|
|
if ($toMailSetting and !empty(\strval(optional($toMailSetting)->value))) {
|
|
|
|
|
Mail::to(\strval($toMailSetting->value))->send(new SendMediaReportMail(
|
|
|
|
|
$mediaList,
|
|
|
|
|
$yesterday,
|
|
|
|
|
$sujets->pluck('libelle')->implode(', ')
|
|
|
|
|
));
|
2021-06-09 12:18:02 +02:00
|
|
|
}
|
2021-06-08 17:02:35 +02:00
|
|
|
}
|
|
|
|
|
}
|