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:
35
api/app/Http/Resources/Api/V1/EmailLogResource.php
Normal file
35
api/app/Http/Resources/Api/V1/EmailLogResource.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Resources\Api\V1;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
final class EmailLogResource extends JsonResource
|
||||
{
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'recipient_email' => $this->recipient_email,
|
||||
'recipient_name' => $this->recipient_name,
|
||||
'template_type' => $this->template_type->value,
|
||||
'template_label' => $this->template_type->label(),
|
||||
'subject' => $this->subject,
|
||||
'status' => $this->status->value,
|
||||
'error_message' => $this->error_message,
|
||||
'queued_at' => $this->queued_at?->toIso8601String(),
|
||||
'sent_at' => $this->sent_at?->toIso8601String(),
|
||||
'failed_at' => $this->failed_at?->toIso8601String(),
|
||||
'triggered_by' => $this->whenLoaded('triggeredBy', fn () => [
|
||||
'id' => $this->triggeredBy->id,
|
||||
'name' => $this->triggeredBy->first_name . ' ' . $this->triggeredBy->last_name,
|
||||
]),
|
||||
'event_id' => $this->event_id,
|
||||
'person_id' => $this->person_id,
|
||||
'created_at' => $this->created_at->toIso8601String(),
|
||||
];
|
||||
}
|
||||
}
|
||||
27
api/app/Http/Resources/Api/V1/EmailSettingsResource.php
Normal file
27
api/app/Http/Resources/Api/V1/EmailSettingsResource.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Resources\Api\V1;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
final class EmailSettingsResource extends JsonResource
|
||||
{
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'organisation_id' => $this->organisation_id,
|
||||
'logo_url' => $this->logo_url,
|
||||
'primary_color' => $this->primary_color,
|
||||
'secondary_color' => $this->secondary_color,
|
||||
'footer_text' => $this->footer_text,
|
||||
'reply_to_email' => $this->reply_to_email,
|
||||
'reply_to_name' => $this->reply_to_name,
|
||||
'created_at' => $this->created_at->toIso8601String(),
|
||||
'updated_at' => $this->updated_at->toIso8601String(),
|
||||
];
|
||||
}
|
||||
}
|
||||
37
api/app/Http/Resources/Api/V1/EmailTemplateResource.php
Normal file
37
api/app/Http/Resources/Api/V1/EmailTemplateResource.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Resources\Api\V1;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
final class EmailTemplateResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* @param Request $request
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
// This resource handles both Eloquent models and raw arrays from EmailService
|
||||
if (is_array($this->resource)) {
|
||||
return $this->resource;
|
||||
}
|
||||
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'organisation_id' => $this->organisation_id,
|
||||
'type' => $this->type instanceof \App\Enums\EmailTemplateType ? $this->type->value : $this->type,
|
||||
'label' => $this->type instanceof \App\Enums\EmailTemplateType ? $this->type->label() : $this->type,
|
||||
'subject' => $this->subject,
|
||||
'heading' => $this->heading,
|
||||
'body_text' => $this->body_text,
|
||||
'button_text' => $this->button_text,
|
||||
'is_custom' => true,
|
||||
'created_at' => $this->created_at->toIso8601String(),
|
||||
'updated_at' => $this->updated_at->toIso8601String(),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user