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:
55
api/database/factories/EmailLogFactory.php
Normal file
55
api/database/factories/EmailLogFactory.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Enums\EmailLogStatus;
|
||||
use App\Enums\EmailTemplateType;
|
||||
use App\Mail\TransactionalMail;
|
||||
use App\Models\EmailLog;
|
||||
use App\Models\Organisation;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/** @extends Factory<EmailLog> */
|
||||
final class EmailLogFactory extends Factory
|
||||
{
|
||||
protected $model = EmailLog::class;
|
||||
|
||||
public function definition(): array
|
||||
{
|
||||
$type = fake()->randomElement(EmailTemplateType::cases());
|
||||
|
||||
return [
|
||||
'organisation_id' => Organisation::factory(),
|
||||
'event_id' => null,
|
||||
'person_id' => null,
|
||||
'user_id' => null,
|
||||
'recipient_email' => fake()->safeEmail(),
|
||||
'recipient_name' => fake()->name(),
|
||||
'mailable_class' => TransactionalMail::class,
|
||||
'template_type' => $type->value,
|
||||
'subject' => fake()->sentence(),
|
||||
'status' => EmailLogStatus::QUEUED->value,
|
||||
'queued_at' => now(),
|
||||
'triggered_by_user_id' => null,
|
||||
];
|
||||
}
|
||||
|
||||
public function sent(): static
|
||||
{
|
||||
return $this->state(fn () => [
|
||||
'status' => EmailLogStatus::SENT->value,
|
||||
'sent_at' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function failed(): static
|
||||
{
|
||||
return $this->state(fn () => [
|
||||
'status' => EmailLogStatus::FAILED->value,
|
||||
'failed_at' => now(),
|
||||
'error_message' => 'Connection refused',
|
||||
]);
|
||||
}
|
||||
}
|
||||
28
api/database/factories/OrganisationEmailSettingsFactory.php
Normal file
28
api/database/factories/OrganisationEmailSettingsFactory.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Models\Organisation;
|
||||
use App\Models\OrganisationEmailSettings;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/** @extends Factory<OrganisationEmailSettings> */
|
||||
final class OrganisationEmailSettingsFactory extends Factory
|
||||
{
|
||||
protected $model = OrganisationEmailSettings::class;
|
||||
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'organisation_id' => Organisation::factory(),
|
||||
'logo_url' => fake()->optional()->imageUrl(200, 60),
|
||||
'primary_color' => fake()->hexColor(),
|
||||
'secondary_color' => fake()->hexColor(),
|
||||
'footer_text' => '© ' . date('Y') . ' ' . fake()->company(),
|
||||
'reply_to_email' => fake()->optional()->safeEmail(),
|
||||
'reply_to_name' => fake()->optional()->name(),
|
||||
];
|
||||
}
|
||||
}
|
||||
35
api/database/factories/OrganisationEmailTemplateFactory.php
Normal file
35
api/database/factories/OrganisationEmailTemplateFactory.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Enums\EmailTemplateType;
|
||||
use App\Models\Organisation;
|
||||
use App\Models\OrganisationEmailTemplate;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/** @extends Factory<OrganisationEmailTemplate> */
|
||||
final class OrganisationEmailTemplateFactory extends Factory
|
||||
{
|
||||
protected $model = OrganisationEmailTemplate::class;
|
||||
|
||||
public function definition(): array
|
||||
{
|
||||
$type = fake()->randomElement(EmailTemplateType::cases());
|
||||
|
||||
return [
|
||||
'organisation_id' => Organisation::factory(),
|
||||
'type' => $type->value,
|
||||
'subject' => fake()->sentence(),
|
||||
'heading' => fake()->optional()->sentence(4),
|
||||
'body_text' => fake()->paragraph(),
|
||||
'button_text' => fake()->optional()->words(3, true),
|
||||
];
|
||||
}
|
||||
|
||||
public function forType(EmailTemplateType $type): static
|
||||
{
|
||||
return $this->state(fn () => ['type' => $type->value]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user