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

@@ -1760,3 +1760,76 @@ Exception: when `shifts.allow_overlap = true`, the **application layer** skips t
- Any role explicitly marked as overlap-allowed in the planning document
The DB constraint remains as a safety net for all other cases.
---
## 3.5.10 Email Infrastructure
### `organisation_email_settings`
Per-organisation email branding configuration. One-to-one with organisations.
| Column | Type | Notes |
| ------------------ | ------------------ | ---------------------------------------- |
| `id` | ULID | PK |
| `organisation_id` | ULID FK | → organisations, UNIQUE, CASCADE DELETE |
| `logo_url` | string(500) nullable | Logo URL for email header |
| `primary_color` | string(7) | Hex color, default `#6366F1` |
| `secondary_color` | string(7) | Hex color, default `#4F46E5` |
| `footer_text` | string(200) nullable | Custom footer text |
| `reply_to_email` | string nullable | Override reply-to per org |
| `reply_to_name` | string(100) nullable | Reply-to display name |
**Relations:** `belongsTo` organisation
**Soft delete:** no
---
### `organisation_email_templates`
Per-organisation, per-type email text overrides. When no override exists, system defaults from `EmailTemplateType` enum are used.
| Column | Type | Notes |
| ------------------ | ------------------ | --------------------------------------------------- |
| `id` | ULID | PK |
| `organisation_id` | ULID FK | → organisations, CASCADE DELETE |
| `type` | string(50) | `EmailTemplateType` enum value |
| `subject` | string(200) | Custom subject line |
| `heading` | string(200) nullable | Custom heading in email body |
| `body_text` | text | Custom body text (supports `{variable}` placeholders) |
| `button_text` | string(100) nullable | Custom CTA button label |
**Unique constraint:** `UNIQUE(organisation_id, type)`
**Relations:** `belongsTo` organisation
**Soft delete:** no
**Template types:** `invitation`, `password_reset`, `email_verification`, `registration_approved`, `registration_rejected`, `shift_assignment`
---
### `email_logs`
Immutable audit record of every email sent. No soft deletes.
| Column | Type | Notes |
| ---------------------- | ------------------ | ----------------------------------------------- |
| `id` | ULID | PK |
| `organisation_id` | ULID FK nullable | → organisations, NULL ON DELETE |
| `event_id` | ULID FK nullable | → events, NULL ON DELETE |
| `person_id` | ULID nullable | Person context if applicable |
| `user_id` | ULID nullable | User context if applicable |
| `recipient_email` | string | |
| `recipient_name` | string nullable | |
| `mailable_class` | string | e.g. `App\Mail\TransactionalMail` |
| `template_type` | string(50) | `EmailTemplateType` enum value |
| `subject` | string | Resolved subject (after variable substitution) |
| `status` | string(20) | `queued\|sent\|failed` |
| `error_message` | text nullable | Failure reason |
| `queued_at` | timestamp | |
| `sent_at` | timestamp nullable | |
| `failed_at` | timestamp nullable | |
| `triggered_by_user_id` | ULID nullable | Who triggered the email |
**Indexes:** `(organisation_id, created_at)`, `(recipient_email, created_at)`, `(template_type, status)`, `(event_id)`, `(person_id)`
**Relations:** `belongsTo` organisation (nullable), event (nullable), person (nullable), user (nullable), triggeredBy → user
**Soft delete:** no — immutable audit table