feat(notification): implement branding for emails using WebsiteType and update templates
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
namespace App\Domains\Integration\Services;
|
||||
|
||||
use App\Domains\Client\Models\Client;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use App\Domains\Tenant\Models\WebsiteType;
|
||||
use Exception;
|
||||
use Illuminate\Contracts\Mail\Factory as MailFactory;
|
||||
use Illuminate\Contracts\Mail\Mailer;
|
||||
@@ -70,24 +72,31 @@ class MailService extends BaseIntegrationService
|
||||
return [];
|
||||
}
|
||||
|
||||
public function send(string|array $recipient, string $subject, string $content): void
|
||||
{
|
||||
public function send(
|
||||
string|array $recipient,
|
||||
string $subject,
|
||||
string $content,
|
||||
Tenant|WebsiteType|null $brand = null,
|
||||
): void {
|
||||
if (! $this->mailer || ! $this->tenant) {
|
||||
throw new Exception('MailService no está configurado. Llamá a forTenant() o forClient() primero.');
|
||||
}
|
||||
|
||||
$this->tenant->loadMissing(['headerLogo', 'footerLogo']);
|
||||
$brand ??= $this->tenant;
|
||||
$branding = $this->brandingFor($brand);
|
||||
|
||||
$html = Blade::render(
|
||||
<<<'BLADE'
|
||||
<x-mail.branded-layout :tenant="$tenant" :header-logo-url="$headerLogoUrl" :footer-logo-url="$footerLogoUrl">
|
||||
<x-mail.branded-layout :branding="$branding" :header-logo-url="$headerLogoUrl" :footer-logo-url="$footerLogoUrl">
|
||||
{!! $content !!}
|
||||
</x-mail.branded-layout>
|
||||
BLADE,
|
||||
[
|
||||
'tenant' => $this->tenant,
|
||||
'headerLogoUrl' => $this->tenant->headerLogo?->getTemporaryUrl(1440),
|
||||
'footerLogoUrl' => $this->tenant->footerLogo?->getTemporaryUrl(1440),
|
||||
'branding' => $branding,
|
||||
'headerLogoUrl' => $brand instanceof WebsiteType
|
||||
? $brand->siteLogo?->getTemporaryUrl(1440)
|
||||
: $brand->headerLogo?->getTemporaryUrl(1440),
|
||||
'footerLogoUrl' => $brand->footerLogo?->getTemporaryUrl(1440),
|
||||
'content' => $content,
|
||||
],
|
||||
);
|
||||
@@ -106,6 +115,36 @@ class MailService extends BaseIntegrationService
|
||||
: (string) config('mail.default');
|
||||
}
|
||||
|
||||
/** @return array{name: string, primary_color: string, body_color: string, background_color: string, surface_color: string, header_bg_color: string, footer_bg_color: string} */
|
||||
private function brandingFor(Tenant|WebsiteType $brand): array
|
||||
{
|
||||
if ($brand instanceof WebsiteType) {
|
||||
$brand->loadMissing(['siteLogo', 'footerLogo']);
|
||||
|
||||
return [
|
||||
'name' => $brand->nombre,
|
||||
'primary_color' => $brand->primary_color ?? '#FF7006',
|
||||
'body_color' => $brand->body_color ?? '#666666',
|
||||
'background_color' => $brand->background_color ?? '#f8f8f8',
|
||||
'surface_color' => $brand->surface_color ?? '#ffffff',
|
||||
'header_bg_color' => $brand->surface_color ?? '#ffffff',
|
||||
'footer_bg_color' => $brand->login_header_footer_color ?? '#838383',
|
||||
];
|
||||
}
|
||||
|
||||
$brand->loadMissing(['headerLogo', 'footerLogo']);
|
||||
|
||||
return [
|
||||
'name' => $brand->nombre,
|
||||
'primary_color' => $brand->primary_color ?? '#6376f3',
|
||||
'body_color' => '#334155',
|
||||
'background_color' => '#f1f5f9',
|
||||
'surface_color' => '#ffffff',
|
||||
'header_bg_color' => $brand->header_bg_color ?? '#ffffff',
|
||||
'footer_bg_color' => $brand->footer_bg_color ?? '#334155',
|
||||
];
|
||||
}
|
||||
|
||||
public function onSetup(): void
|
||||
{
|
||||
if (! $this->mailer || ! $this->clientContext) {
|
||||
|
||||
@@ -28,10 +28,21 @@ class TestMail extends Mailable
|
||||
{
|
||||
$this->tenant->loadMissing(['headerLogo', 'footerLogo']);
|
||||
|
||||
$branding = [
|
||||
'name' => $this->tenant->nombre,
|
||||
'primary_color' => $this->tenant->primary_color ?? '#6376f3',
|
||||
'body_color' => '#334155',
|
||||
'background_color' => '#f1f5f9',
|
||||
'surface_color' => '#ffffff',
|
||||
'header_bg_color' => $this->tenant->header_bg_color ?? '#ffffff',
|
||||
'footer_bg_color' => $this->tenant->footer_bg_color ?? '#334155',
|
||||
];
|
||||
|
||||
return new Content(
|
||||
view: 'mail.test',
|
||||
with: [
|
||||
'tenant' => $this->tenant,
|
||||
'branding' => $branding,
|
||||
'headerLogoUrl' => $this->tenant->headerLogo?->getTemporaryUrl(1440),
|
||||
'footerLogoUrl' => $this->tenant->footerLogo?->getTemporaryUrl(1440),
|
||||
],
|
||||
|
||||
@@ -21,7 +21,7 @@ class NotificationMailService
|
||||
|
||||
public function sendWelcome(int $userId, string $tenantCode): void
|
||||
{
|
||||
$tenant = Tenant::query()->where('codigo', $tenantCode)->firstOrFail();
|
||||
$tenant = Tenant::query()->with('websiteType')->where('codigo', $tenantCode)->firstOrFail();
|
||||
$user = User::query()->findOrFail($userId);
|
||||
|
||||
$this->mailService
|
||||
@@ -30,6 +30,7 @@ class NotificationMailService
|
||||
$user->email,
|
||||
"Bienvenido a {$tenant->nombre}",
|
||||
view('mail.notifications.welcome', compact('tenant', 'user'))->render(),
|
||||
$tenant->websiteType ?? $tenant,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -81,7 +82,13 @@ class NotificationMailService
|
||||
->send(
|
||||
$attempt->user->email,
|
||||
"Código para recuperar tu contraseña - {$tenant->nombre}",
|
||||
view('mail.notifications.password-reset', compact('tenant', 'attempt', 'recoveryUrl'))->render(),
|
||||
view('mail.notifications.password-reset', [
|
||||
'tenant' => $tenant,
|
||||
'attempt' => $attempt,
|
||||
'recoveryUrl' => $recoveryUrl,
|
||||
'brand' => $tenant->websiteType ?? $tenant,
|
||||
])->render(),
|
||||
$tenant->websiteType ?? $tenant,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -23,4 +23,6 @@ No expone rutas HTTP. Consume datos de `Auth`, `Tenant`, `Purchase` y `Ticket`,
|
||||
|
||||
- Los listeners reciben identificadores y vuelven a cargar los modelos, evitando transportar entidades obsoletas.
|
||||
- La recuperación no se envía si el intento dejó de estar pendiente.
|
||||
- Los correos de cuenta (bienvenida y recuperación de contraseña) usan la identidad visual del `WebsiteType` asociado al tenant, con fallback al tenant si no tiene uno configurado.
|
||||
- Los correos transaccionales (pago confirmado y tickets disponibles) usan la identidad visual del tenant/evento de la compra.
|
||||
- Los handlers deben permanecer idempotentes o tolerantes a reintentos de cola.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
@props(['tenant', 'headerLogoUrl' => null, 'footerLogoUrl' => null])
|
||||
@props(['branding', 'headerLogoUrl' => null, 'footerLogoUrl' => null])
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html lang="es">
|
||||
@@ -6,7 +6,7 @@
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<meta name="color-scheme" content="light">
|
||||
<title>{{ $tenant->nombre }}</title>
|
||||
<title>{{ $branding['name'] }}</title>
|
||||
<style>
|
||||
@media only screen and (max-width: 620px) {
|
||||
.mail-container { width: 100% !important; }
|
||||
@@ -14,17 +14,17 @@
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body style="margin: 0; padding: 0; background-color: #f1f5f9; color: #334155; font-family: Arial, Helvetica, sans-serif;">
|
||||
<table role="presentation" width="100%" cellspacing="0" cellpadding="0" border="0" style="background-color: #f1f5f9;">
|
||||
<body style="margin: 0; padding: 0; background-color: {{ $branding['background_color'] }}; color: {{ $branding['body_color'] }}; font-family: Arial, Helvetica, sans-serif;">
|
||||
<table role="presentation" width="100%" cellspacing="0" cellpadding="0" border="0" style="background-color: {{ $branding['background_color'] }};">
|
||||
<tr>
|
||||
<td align="center" style="padding: 32px 12px;">
|
||||
<table role="presentation" width="600" cellspacing="0" cellpadding="0" border="0" class="mail-container" style="width: 600px; max-width: 600px; background-color: #ffffff; border-top: 4px solid {{ $tenant->primary_color }}; border-radius: 8px; overflow: hidden; box-shadow: 0 2px 8px rgba(15, 23, 42, 0.08);">
|
||||
<table role="presentation" width="600" cellspacing="0" cellpadding="0" border="0" class="mail-container" style="width: 600px; max-width: 600px; background-color: {{ $branding['surface_color'] }}; border-top: 4px solid {{ $branding['primary_color'] }}; border-radius: 8px; overflow: hidden; box-shadow: 0 2px 8px rgba(15, 23, 42, 0.08);">
|
||||
<tr>
|
||||
<td align="center" bgcolor="{{ $tenant->header_bg_color }}" style="padding: 24px 32px; background-color: {{ $tenant->header_bg_color }};">
|
||||
<td align="center" bgcolor="{{ $branding['header_bg_color'] }}" style="padding: 24px 32px; background-color: {{ $branding['header_bg_color'] }};">
|
||||
@if ($headerLogoUrl)
|
||||
<img src="{{ $headerLogoUrl }}" alt="{{ $tenant->nombre }}" width="180" style="display: block; width: auto; max-width: 180px; max-height: 64px; border: 0;">
|
||||
<img src="{{ $headerLogoUrl }}" alt="{{ $branding['name'] }}" width="180" style="display: block; width: auto; max-width: 180px; max-height: 64px; border: 0;">
|
||||
@else
|
||||
<span style="color: {{ $tenant->primary_color }}; font-size: 24px; font-weight: 700; line-height: 1.2;">{{ $tenant->nombre }}</span>
|
||||
<span style="color: {{ $branding['primary_color'] }}; font-size: 24px; font-weight: 700; line-height: 1.2;">{{ $branding['name'] }}</span>
|
||||
@endif
|
||||
</td>
|
||||
</tr>
|
||||
@@ -34,11 +34,11 @@
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center" bgcolor="{{ $tenant->footer_bg_color }}" style="padding: 24px 32px; background-color: {{ $tenant->footer_bg_color }}; color: #ffffff; font-size: 12px; line-height: 1.5;">
|
||||
<td align="center" bgcolor="{{ $branding['footer_bg_color'] }}" style="padding: 24px 32px; background-color: {{ $branding['footer_bg_color'] }}; color: #ffffff; font-size: 12px; line-height: 1.5;">
|
||||
@if ($footerLogoUrl)
|
||||
<img src="{{ $footerLogoUrl }}" alt="{{ $tenant->nombre }}" width="140" style="display: block; width: auto; max-width: 140px; max-height: 48px; margin: 0 auto 16px; border: 0;">
|
||||
<img src="{{ $footerLogoUrl }}" alt="{{ $branding['name'] }}" width="140" style="display: block; width: auto; max-width: 140px; max-height: 48px; margin: 0 auto 16px; border: 0;">
|
||||
@endif
|
||||
{{ $footer ?? 'Este correo fue enviado por '.$tenant->nombre.'.' }}
|
||||
{{ $footer ?? 'Este correo fue enviado por '.$branding['name'].'.' }}
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<h1 style="margin: 0 0 20px; color: {{ $tenant->primary_color }};">
|
||||
<h1 style="margin: 0 0 20px; color: {{ $brand->primary_color }};">
|
||||
Recuperá tu contraseña
|
||||
</h1>
|
||||
|
||||
@@ -19,8 +19,8 @@
|
||||
|
||||
<p>Ingresá este código en {{ $tenant->nombre }}:</p>
|
||||
|
||||
<div style="margin: 28px 0; padding: 20px; border: 2px solid {{ $tenant->primary_color }}; border-radius: 8px; text-align: center;">
|
||||
<span style="color: {{ $tenant->primary_color }}; font-size: 36px; font-weight: 700; letter-spacing: 12px;">
|
||||
<div style="margin: 28px 0; padding: 20px; border: 2px solid {{ $brand->primary_color }}; border-radius: 8px; text-align: center;">
|
||||
<span style="color: {{ $brand->primary_color }}; font-size: 36px; font-weight: 700; letter-spacing: 12px;">
|
||||
{{ $attempt->codigo }}
|
||||
</span>
|
||||
</div>
|
||||
@@ -28,7 +28,7 @@
|
||||
@if($recoveryUrl)
|
||||
<div style="text-align: center; margin-bottom: 28px;">
|
||||
<a href="{{ $recoveryUrl }}"
|
||||
style="display: inline-block; padding: 12px 24px; background-color: {{ $tenant->primary_color }}; color: #ffffff; text-decoration: none; border-radius: 6px; font-weight: bold;">
|
||||
style="display: inline-block; padding: 12px 24px; background-color: {{ $brand->primary_color }}; color: #ffffff; text-decoration: none; border-radius: 6px; font-weight: bold;">
|
||||
{{ $attempt->reason === \App\Domains\Auth\Models\ResetPasswordAttempt::REASON_STAFF_CREATED ? 'Crear mi contraseña' : 'Ingresar código ahora' }}
|
||||
</a>
|
||||
</div>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<x-mail.branded-layout :tenant="$tenant" :header-logo-url="$headerLogoUrl" :footer-logo-url="$footerLogoUrl">
|
||||
<x-mail.branded-layout :branding="$branding" :header-logo-url="$headerLogoUrl" :footer-logo-url="$footerLogoUrl">
|
||||
<h1 style="margin: 0 0 20px; color: {{ $tenant->primary_color }}; font-size: 26px; line-height: 1.3;">
|
||||
Prueba de correo de Shopit
|
||||
</h1>
|
||||
|
||||
@@ -71,6 +71,8 @@ class NotificationMailServiceTest extends TestCase
|
||||
|
||||
public function test_it_sends_a_branded_welcome_email(): void
|
||||
{
|
||||
$this->useWebsiteTypeBranding();
|
||||
|
||||
app(NotificationMailService::class)->sendWelcome($this->user->id, $this->tenant->codigo);
|
||||
|
||||
Mail::assertSent(Mailable::class, function (Mailable $mail): bool {
|
||||
@@ -78,12 +80,15 @@ class NotificationMailServiceTest extends TestCase
|
||||
$mail->assertHasSubject('Bienvenido a Mail Tenant');
|
||||
|
||||
return str_contains($mail->render(), 'Ada Lovelace')
|
||||
&& str_contains($mail->render(), 'Mail Tenant');
|
||||
&& str_contains($mail->render(), 'Mail Tenant')
|
||||
&& str_contains($mail->render(), 'OnTicket')
|
||||
&& str_contains($mail->render(), 'border-top: 4px solid #ff7006');
|
||||
});
|
||||
}
|
||||
|
||||
public function test_it_sends_a_branded_password_reset_email(): void
|
||||
{
|
||||
$this->useWebsiteTypeBranding();
|
||||
$attempt = $this->user->resetPasswordAttempts()->create([
|
||||
'codigo' => '0123',
|
||||
]);
|
||||
@@ -101,7 +106,8 @@ class NotificationMailServiceTest extends TestCase
|
||||
return str_contains($rendered, '0123')
|
||||
&& str_contains($rendered, 'Ada Lovelace')
|
||||
&& str_contains($rendered, 'Mail Tenant')
|
||||
&& str_contains($rendered, '#112233');
|
||||
&& str_contains($rendered, '#ff7006')
|
||||
&& ! str_contains($rendered, 'border: 2px solid #112233');
|
||||
});
|
||||
}
|
||||
|
||||
@@ -137,6 +143,7 @@ class NotificationMailServiceTest extends TestCase
|
||||
|
||||
public function test_it_sends_purchase_and_ticket_emails_to_the_purchase_recipient(): void
|
||||
{
|
||||
$this->useWebsiteTypeBranding();
|
||||
$purchase = Purchase::query()->create([
|
||||
'tenant_codigo' => $this->tenant->codigo,
|
||||
'user_id' => $this->user->id,
|
||||
@@ -180,13 +187,34 @@ class NotificationMailServiceTest extends TestCase
|
||||
$mail->assertTo('checkout@example.com');
|
||||
|
||||
return $mail->subject === "Pago confirmado - Compra #{$purchase->id}"
|
||||
&& str_contains($mail->render(), 'Total pagado');
|
||||
&& str_contains($mail->render(), 'Total pagado')
|
||||
&& str_contains($mail->render(), 'border-top: 4px solid #112233')
|
||||
&& ! str_contains($mail->render(), 'border-top: 4px solid #ff7006');
|
||||
});
|
||||
Mail::assertSent(Mailable::class, function (Mailable $mail): bool {
|
||||
$mail->assertTo('checkout@example.com');
|
||||
|
||||
return $mail->subject === 'Tus tickets ya están disponibles'
|
||||
&& str_contains($mail->render(), 'Entrada general');
|
||||
&& str_contains($mail->render(), 'Entrada general')
|
||||
&& str_contains($mail->render(), 'border-top: 4px solid #112233')
|
||||
&& ! str_contains($mail->render(), 'border-top: 4px solid #ff7006');
|
||||
});
|
||||
}
|
||||
|
||||
private function useWebsiteTypeBranding(): void
|
||||
{
|
||||
$websiteType = WebsiteType::query()->create([
|
||||
'codigo' => 'onticket',
|
||||
'nombre' => 'OnTicket',
|
||||
'dominio' => 'onticket.local',
|
||||
'primary_color' => '#ff7006',
|
||||
'body_color' => '#666666',
|
||||
'background_color' => '#f8f8f8',
|
||||
'surface_color' => '#ffffff',
|
||||
'login_header_footer_color' => '#838383',
|
||||
]);
|
||||
|
||||
$this->tenant->update(['website_type_code' => $websiteType->codigo]);
|
||||
$this->tenant->unsetRelation('websiteType');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user