Files
crewli/api/app/Http/Controllers/Api/V1/EmailLogController.php
bert.hausmans 65978104d8 feat: complete email infrastructure with queue, templates, logging, and API
Adds the full transactional email system:
- Redis queue (QUEUE_CONNECTION=redis), SES config in .env.example
- 3 migrations: organisation_email_settings, organisation_email_templates, email_logs
- EmailTemplateType and EmailLogStatus enums with Dutch defaults
- EmailService as central entry point for all email sending
- SendTransactionalEmail queued job with retries and idempotency
- TransactionalMail mailable with responsive HTML + plain text templates
- Organisation-level branding (colors, logo, footer, reply-to)
- Per-type template overrides with {variable} substitution
- Email log with filtering by status, type, date range, recipient
- Preview and send-test endpoints for template management
- API endpoints: email-settings, email-templates (CRUD), email-logs (read-only)
- Integrated into existing flows: invitations, password reset, email
  verification, registration approval/rejection
- 37 new tests across 4 test files, all existing tests updated

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-15 20:12:21 +02:00

64 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers\Api\V1;
use App\Enums\EmailLogStatus;
use App\Enums\EmailTemplateType;
use App\Http\Controllers\Controller;
use App\Http\Resources\Api\V1\EmailLogResource;
use App\Models\EmailLog;
use App\Models\Organisation;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
final class EmailLogController extends Controller
{
public function index(Request $request, Organisation $organisation): JsonResponse
{
Gate::authorize('update', $organisation);
$query = EmailLog::where('organisation_id', $organisation->id)
->with('triggeredBy')
->orderByDesc('created_at');
if ($search = $request->query('search')) {
$query->where('recipient_email', 'like', '%' . $search . '%');
}
if ($status = $request->query('status')) {
if (EmailLogStatus::tryFrom($status)) {
$query->where('status', $status);
}
}
if ($templateType = $request->query('template_type')) {
if (EmailTemplateType::tryFrom($templateType)) {
$query->where('template_type', $templateType);
}
}
if ($eventId = $request->query('event_id')) {
$query->where('event_id', $eventId);
}
if ($personId = $request->query('person_id')) {
$query->where('person_id', $personId);
}
if ($from = $request->query('from')) {
$query->where('created_at', '>=', $from);
}
if ($to = $request->query('to')) {
$query->where('created_at', '<=', $to);
}
$logs = $query->paginate($request->integer('per_page', 15));
return $this->success(EmailLogResource::collection($logs)->response()->getData(true));
}
}