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>
This commit is contained in:
2026-04-15 20:12:21 +02:00
parent c64875b6ef
commit 65978104d8
42 changed files with 2420 additions and 48 deletions

View File

@@ -5,13 +5,15 @@ declare(strict_types=1);
namespace Tests\Feature\Api\V1;
use App\Enums\EmailChangeStatus;
use App\Enums\EmailTemplateType;
use App\Jobs\SendTransactionalEmail;
use App\Mail\EmailChangedConfirmationMail;
use App\Mail\VerifyEmailChangeMail;
use App\Models\EmailChangeRequest;
use App\Models\Organisation;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Queue;
use Tests\TestCase;
class EmailChangeTest extends TestCase
@@ -22,7 +24,7 @@ class EmailChangeTest extends TestCase
public function test_user_can_request_email_change(): void
{
Mail::fake();
Queue::fake();
$user = User::factory()->create([
'email' => 'oud@voorbeeld.nl',
@@ -44,8 +46,9 @@ class EmailChangeTest extends TestCase
'status' => 'pending',
]);
Mail::assertQueued(VerifyEmailChangeMail::class, function ($mail) {
return $mail->hasTo('nieuw@voorbeeld.nl');
Queue::assertPushed(SendTransactionalEmail::class, function ($job) {
return $job->recipientEmail === 'nieuw@voorbeeld.nl'
&& $job->type === EmailTemplateType::EMAIL_VERIFICATION;
});
}
@@ -249,7 +252,7 @@ class EmailChangeTest extends TestCase
public function test_admin_can_change_member_email(): void
{
Mail::fake();
Queue::fake();
$organisation = Organisation::factory()->create();
$admin = User::factory()->create();
@@ -265,8 +268,9 @@ class EmailChangeTest extends TestCase
$response->assertOk();
Mail::assertQueued(VerifyEmailChangeMail::class, function ($mail) {
return $mail->hasTo('nieuw-lid@voorbeeld.nl');
Queue::assertPushed(SendTransactionalEmail::class, function ($job) {
return $job->recipientEmail === 'nieuw-lid@voorbeeld.nl'
&& $job->type === EmailTemplateType::EMAIL_VERIFICATION;
});
}

View File

@@ -4,12 +4,13 @@ declare(strict_types=1);
namespace Tests\Feature\Api\V1;
use App\Enums\EmailTemplateType;
use App\Jobs\SendTransactionalEmail;
use App\Models\User;
use App\Notifications\ResetPasswordNotification;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Password;
use Illuminate\Support\Facades\Queue;
use Tests\TestCase;
class PasswordResetTest extends TestCase
@@ -20,7 +21,7 @@ class PasswordResetTest extends TestCase
public function test_forgot_password_returns_success_for_existing_email(): void
{
Notification::fake();
Queue::fake();
$user = User::factory()->create(['email' => 'jan@voorbeeld.nl']);
@@ -31,11 +32,16 @@ class PasswordResetTest extends TestCase
$response->assertOk();
Notification::assertSentTo($user, ResetPasswordNotification::class);
Queue::assertPushed(SendTransactionalEmail::class, function ($job) {
return $job->recipientEmail === 'jan@voorbeeld.nl'
&& $job->type === EmailTemplateType::PASSWORD_RESET;
});
}
public function test_forgot_password_returns_same_success_for_nonexisting_email(): void
{
Queue::fake();
$response = $this->postJson('/api/v1/auth/forgot-password', [
'email' => 'onbekend@voorbeeld.nl',
'app' => 'app',

View File

@@ -4,8 +4,8 @@ declare(strict_types=1);
namespace Tests\Feature\Api\V1;
use App\Mail\RegistrationApprovedMail;
use App\Mail\RegistrationRejectedMail;
use App\Jobs\SendTransactionalEmail;
use App\Enums\EmailTemplateType;
use App\Models\CrowdType;
use App\Models\Event;
use App\Models\Organisation;
@@ -13,7 +13,7 @@ use App\Models\Person;
use App\Models\User;
use Database\Seeders\RoleSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Queue;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
@@ -46,7 +46,7 @@ class PersonApprovalEmailTest extends TestCase
public function test_approving_person_sends_approved_email(): void
{
Mail::fake();
Queue::fake();
$person = Person::factory()->create([
'event_id' => $this->event->id,
@@ -61,14 +61,20 @@ class PersonApprovalEmailTest extends TestCase
$response->assertOk();
Mail::assertQueued(RegistrationApprovedMail::class, function ($mail) {
return $mail->hasTo('volunteer@test.nl');
Queue::assertPushed(SendTransactionalEmail::class, function ($job) {
return $job->recipientEmail === 'volunteer@test.nl'
&& $job->type === EmailTemplateType::REGISTRATION_APPROVED;
});
$this->assertDatabaseHas('email_logs', [
'recipient_email' => 'volunteer@test.nl',
'template_type' => 'registration_approved',
]);
}
public function test_rejecting_person_sends_rejected_email_with_reason(): void
{
Mail::fake();
Queue::fake();
$person = Person::factory()->create([
'event_id' => $this->event->id,
@@ -90,15 +96,15 @@ class PersonApprovalEmailTest extends TestCase
'status' => 'rejected',
]);
Mail::assertQueued(RegistrationRejectedMail::class, function ($mail) {
return $mail->hasTo('volunteer@test.nl')
&& $mail->reason === 'Geen beschikbaarheid op de juiste momenten.';
Queue::assertPushed(SendTransactionalEmail::class, function ($job) {
return $job->recipientEmail === 'volunteer@test.nl'
&& $job->type === EmailTemplateType::REGISTRATION_REJECTED;
});
}
public function test_rejecting_person_sends_rejected_email_without_reason(): void
{
Mail::fake();
Queue::fake();
$person = Person::factory()->create([
'event_id' => $this->event->id,
@@ -113,9 +119,9 @@ class PersonApprovalEmailTest extends TestCase
$response->assertOk();
Mail::assertQueued(RegistrationRejectedMail::class, function ($mail) {
return $mail->hasTo('volunteer@test.nl')
&& $mail->reason === null;
Queue::assertPushed(SendTransactionalEmail::class, function ($job) {
return $job->recipientEmail === 'volunteer@test.nl'
&& $job->type === EmailTemplateType::REGISTRATION_REJECTED;
});
}