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:
72
api/app/Models/EmailLog.php
Normal file
72
api/app/Models/EmailLog.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\EmailLogStatus;
|
||||
use App\Enums\EmailTemplateType;
|
||||
use Illuminate\Database\Eloquent\Concerns\HasUlids;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
final class EmailLog extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
use HasUlids;
|
||||
|
||||
protected $fillable = [
|
||||
'organisation_id',
|
||||
'event_id',
|
||||
'person_id',
|
||||
'user_id',
|
||||
'recipient_email',
|
||||
'recipient_name',
|
||||
'mailable_class',
|
||||
'template_type',
|
||||
'subject',
|
||||
'status',
|
||||
'error_message',
|
||||
'queued_at',
|
||||
'sent_at',
|
||||
'failed_at',
|
||||
'triggered_by_user_id',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'template_type' => EmailTemplateType::class,
|
||||
'status' => EmailLogStatus::class,
|
||||
'queued_at' => 'datetime',
|
||||
'sent_at' => 'datetime',
|
||||
'failed_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
|
||||
public function organisation(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Organisation::class);
|
||||
}
|
||||
|
||||
public function event(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Event::class);
|
||||
}
|
||||
|
||||
public function person(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Person::class);
|
||||
}
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function triggeredBy(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'triggered_by_user_id');
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
final class Organisation extends Model
|
||||
@@ -72,4 +73,19 @@ final class Organisation extends Model
|
||||
{
|
||||
return $this->hasMany(RegistrationFieldTemplate::class);
|
||||
}
|
||||
|
||||
public function emailSettings(): HasOne
|
||||
{
|
||||
return $this->hasOne(OrganisationEmailSettings::class);
|
||||
}
|
||||
|
||||
public function emailTemplates(): HasMany
|
||||
{
|
||||
return $this->hasMany(OrganisationEmailTemplate::class);
|
||||
}
|
||||
|
||||
public function emailLogs(): HasMany
|
||||
{
|
||||
return $this->hasMany(EmailLog::class);
|
||||
}
|
||||
}
|
||||
|
||||
31
api/app/Models/OrganisationEmailSettings.php
Normal file
31
api/app/Models/OrganisationEmailSettings.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Concerns\HasUlids;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
final class OrganisationEmailSettings extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
use HasUlids;
|
||||
|
||||
protected $fillable = [
|
||||
'organisation_id',
|
||||
'logo_url',
|
||||
'primary_color',
|
||||
'secondary_color',
|
||||
'footer_text',
|
||||
'reply_to_email',
|
||||
'reply_to_name',
|
||||
];
|
||||
|
||||
public function organisation(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Organisation::class);
|
||||
}
|
||||
}
|
||||
38
api/app/Models/OrganisationEmailTemplate.php
Normal file
38
api/app/Models/OrganisationEmailTemplate.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\EmailTemplateType;
|
||||
use Illuminate\Database\Eloquent\Concerns\HasUlids;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
final class OrganisationEmailTemplate extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
use HasUlids;
|
||||
|
||||
protected $fillable = [
|
||||
'organisation_id',
|
||||
'type',
|
||||
'subject',
|
||||
'heading',
|
||||
'body_text',
|
||||
'button_text',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'type' => EmailTemplateType::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function organisation(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Organisation::class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user