feat: Phase 5.1 - Scheduled Report Emails
- Create SendScheduledReport job (runs daily 08:00) - Create ScheduledReportMail Mailable with Blade template - Add receive_reports boolean to users table (migration) - Register job in routes/console.php Tests: 101 passing (319 assertions)
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Models\Project;
|
||||
use App\Models\User;
|
||||
use App\Services\ReportGenerator;
|
||||
use App\DTO\ReportFilters;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
|
||||
class SendScheduledReport implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
public int $tries = 3;
|
||||
public int $backoff = 60;
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
// Get users who want to receive scheduled reports
|
||||
$users = User::where('receive_reports', true)
|
||||
->whereHas('projects')
|
||||
->with('projects')
|
||||
->get();
|
||||
|
||||
foreach ($users as $user) {
|
||||
foreach ($user->projects as $project) {
|
||||
$filters = new ReportFilters();
|
||||
$filters->entityTypes = ['phases', 'features', 'inspections', 'issues', 'tasks'];
|
||||
|
||||
$generator = new ReportGenerator($project, $filters);
|
||||
$data = $generator->generate();
|
||||
|
||||
Mail::to($user)->send(new \App\Mail\ScheduledReportMail(
|
||||
$project,
|
||||
$user,
|
||||
$data,
|
||||
$filters
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Mail;
|
||||
|
||||
use App\Models\Project;
|
||||
use App\Models\User;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Mail\Mailable;
|
||||
use Illuminate\Mail\Mailables\Content;
|
||||
use Illuminate\Mail\Mailables\Envelope;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class ScheduledReportMail extends Mailable implements ShouldQueue
|
||||
{
|
||||
use Queueable, SerializesModels;
|
||||
|
||||
public function __construct(
|
||||
public Project $project,
|
||||
public User $user,
|
||||
public array $reportData,
|
||||
public \App\DTO\ReportFilters $filters
|
||||
) {}
|
||||
|
||||
public function envelope(): Envelope
|
||||
{
|
||||
return new Envelope(
|
||||
subject: "Informe programado: {$this->project->name}",
|
||||
);
|
||||
}
|
||||
|
||||
public function content(): Content
|
||||
{
|
||||
return new Content(
|
||||
view: 'emails.scheduled-report',
|
||||
with: [
|
||||
'project' => $this->project,
|
||||
'user' => $this->user,
|
||||
'data' => $this->reportData,
|
||||
'filters' => $this->filters,
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user