Files
crewli/api/app/Services/InvitationService.php
bert.hausmans 65978104d8 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>
2026-04-15 20:12:21 +02:00

129 lines
4.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services;
use App\Enums\EmailTemplateType;
use App\Models\Organisation;
use App\Models\User;
use App\Models\UserInvitation;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
final class InvitationService
{
public function __construct(
private readonly EmailService $emailService,
) {}
public function invite(Organisation $org, string $email, string $role, User $invitedBy): UserInvitation
{
$existingInvitation = UserInvitation::where('email', $email)
->where('organisation_id', $org->id)
->pending()
->where('expires_at', '>', now())
->first();
if ($existingInvitation) {
throw ValidationException::withMessages([
'email' => ['Er is al een openstaande uitnodiging voor dit e-mailadres.'],
]);
}
$existingMember = $org->users()->where('email', $email)->first();
if ($existingMember) {
throw ValidationException::withMessages([
'email' => ['Gebruiker is al lid van deze organisatie.'],
]);
}
$plainToken = bin2hex(random_bytes(32));
$invitation = new UserInvitation(['email' => $email]);
$invitation->invited_by_user_id = $invitedBy->id;
$invitation->organisation_id = $org->id;
$invitation->role = $role;
$invitation->token = hash('sha256', $plainToken);
$invitation->status = 'pending';
$invitation->expires_at = now()->addDays(7);
$invitation->save();
// Set transient plain token for use in the email URL
$invitation->plainToken = $plainToken;
$this->emailService->send(
type: EmailTemplateType::INVITATION,
recipientEmail: $email,
recipientName: $email,
variables: [
'organisation_name' => $org->name,
],
actionUrl: config('app.frontend_app_url') . '/invitations/' . $plainToken . '/accept',
organisation: $org,
triggeredByUserId: $invitedBy->id,
);
activity('invitation')
->performedOn($invitation)
->causedBy($invitedBy)
->withProperties(['email' => $email, 'role' => $role])
->log("Invited {$email} as {$role}");
return $invitation;
}
public function accept(UserInvitation $invitation, ?string $password = null): User
{
if (! $invitation->isPending() || $invitation->isExpired()) {
throw ValidationException::withMessages([
'token' => ['Deze uitnodiging is niet meer geldig.'],
]);
}
$user = User::where('email', $invitation->email)->first();
if (! $user) {
if (! $password) {
throw ValidationException::withMessages([
'password' => ['Een wachtwoord is vereist om een nieuw account aan te maken.'],
]);
}
$user = User::create([
'first_name' => Str::before($invitation->email, '@'),
'last_name' => '',
'email' => $invitation->email,
'password' => $password,
'email_verified_at' => now(),
]);
app(PersonIdentityService::class)->detectMatchesForUser($user);
}
$organisation = $invitation->organisation;
if (! $organisation->users()->where('user_id', $user->id)->exists()) {
$organisation->users()->attach($user, ['role' => $invitation->role]);
}
$invitation->markAsAccepted();
activity('invitation')
->performedOn($invitation)
->causedBy($user)
->withProperties(['email' => $invitation->email])
->log("Accepted invitation for {$organisation->name}");
return $user;
}
public function expireOldInvitations(): int
{
return UserInvitation::where('status', 'pending')
->where('expires_at', '<', now())
->update(['status' => 'expired']);
}
}