feat(notification): enhance password reset emails with custom templates for different scenarios
This commit is contained in:
parent
18f1217daa
commit
7acef66ee7
|
|
@ -99,12 +99,25 @@ class NotificationMailService
|
|||
: 'https://'.$recoveryDomain.$recoveryBasePath.'/recuperar-contrasena/codigo?'.http_build_query($recoveryQuery);
|
||||
$brand = $tenant->websiteType ?? $tenant;
|
||||
|
||||
[$subject, $template] = match ($attempt->reason) {
|
||||
ResetPasswordAttempt::REASON_STAFF_CREATED => [
|
||||
'Tu cuenta de escáner está lista', 'scanner-created',
|
||||
],
|
||||
ResetPasswordAttempt::REASON_ADMINISTRATOR_CREATED => [
|
||||
'Tu cuenta de administrador está lista', 'administrator-created',
|
||||
],
|
||||
ResetPasswordAttempt::REASON_ACCOUNT_LOCKED => [
|
||||
'Desbloqueá tu cuenta', 'account-locked',
|
||||
],
|
||||
default => ['Código para recuperar tu contraseña', 'password-reset'],
|
||||
};
|
||||
|
||||
$this->mailService
|
||||
->forTenant($tenantCode)
|
||||
->send(
|
||||
$attempt->user->email,
|
||||
"Código para recuperar tu contraseña - {$brand->nombre}",
|
||||
view('mail.notifications.password-reset', [
|
||||
"{$subject} - {$brand->nombre}",
|
||||
view("mail.notifications.{$template}", [
|
||||
'attempt' => $attempt,
|
||||
'recoveryUrl' => $recoveryUrl,
|
||||
'brand' => $brand,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
@include('mail.notifications.partials.password-action', [
|
||||
'title' => 'Desbloqueá tu cuenta',
|
||||
'description' => 'registramos varios intentos fallidos de inicio de sesión en tu cuenta. Por seguridad, bloqueamos el acceso temporalmente. Utilizá este código para cambiar tu contraseña y desbloquearla.',
|
||||
'buttonLabel' => 'Ingresar código ahora',
|
||||
'footer' => 'Si no fuiste vos, por favor desestimá y borrá este correo. Tu cuenta seguirá protegida.',
|
||||
])
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
@include('mail.notifications.partials.password-action', [
|
||||
'title' => 'Tu cuenta de administrador está lista',
|
||||
'description' => 'creamos tu cuenta de administrador en '.$brand->nombre.'. Creá tu contraseña con este código para ingresar al panel de administración.',
|
||||
'buttonLabel' => 'Crear mi contraseña',
|
||||
'footer' => 'Si no esperabas recibir una cuenta de administrador, podés ignorar este mensaje.',
|
||||
])
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
<h1 style="margin: 0 0 20px; color: {{ $brand->primary_color }};">
|
||||
{{ $title }}
|
||||
</h1>
|
||||
|
||||
<p>Hola {{ $attempt->user->nombre_apellido }}, {{ $description }}</p>
|
||||
|
||||
<p>Ingresá este código en {{ $brand->nombre }}:</p>
|
||||
|
||||
<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>
|
||||
|
||||
@if($recoveryUrl)
|
||||
<div style="text-align: center; margin-bottom: 28px;">
|
||||
<a href="{{ $recoveryUrl }}"
|
||||
style="display: inline-block; padding: 12px 24px; background-color: {{ $brand->primary_color }}; color: #ffffff; text-decoration: none; border-radius: 6px; font-weight: bold;">
|
||||
{{ $buttonLabel }}
|
||||
</a>
|
||||
</div>
|
||||
@endif
|
||||
|
||||
<p style="color: #64748b; font-size: 14px;">{{ $footer }}</p>
|
||||
|
|
@ -1,45 +1,6 @@
|
|||
<h1 style="margin: 0 0 20px; color: {{ $brand->primary_color }};">
|
||||
Recuperá tu contraseña
|
||||
</h1>
|
||||
|
||||
@if($attempt->reason === \App\Domains\Auth\Models\ResetPasswordAttempt::REASON_STAFF_CREATED)
|
||||
<p>
|
||||
Hola {{ $attempt->user->nombre_apellido }}, creamos tu cuenta de scanner en {{ $brand->nombre }}. Utilizá este código para crear tu contraseña y comenzar a usarla.
|
||||
</p>
|
||||
@elseif($attempt->reason === \App\Domains\Auth\Models\ResetPasswordAttempt::REASON_ACCOUNT_LOCKED)
|
||||
<p>
|
||||
Hola {{ $attempt->user->nombre_apellido }}, registramos varios intentos fallidos de inicio de sesión en tu cuenta. Por seguridad, hemos bloqueado el acceso temporalmente. Puedes utilizar este código para cambiar tu contraseña y desbloquearla inmediatamente.
|
||||
</p>
|
||||
@else
|
||||
<p>
|
||||
Hola {{ $attempt->user->nombre_apellido }}, recibimos una solicitud para restablecer
|
||||
la contraseña de tu cuenta.
|
||||
</p>
|
||||
@endif
|
||||
|
||||
<p>Ingresá este código en {{ $brand->nombre }}:</p>
|
||||
|
||||
<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>
|
||||
|
||||
@if($recoveryUrl)
|
||||
<div style="text-align: center; margin-bottom: 28px;">
|
||||
<a href="{{ $recoveryUrl }}"
|
||||
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>
|
||||
@endif
|
||||
|
||||
<p style="color: #64748b; font-size: 14px;">
|
||||
@if($attempt->reason === \App\Domains\Auth\Models\ResetPasswordAttempt::REASON_ACCOUNT_LOCKED)
|
||||
Si no fuiste vos, por favor desestimá y borrá este correo. Tu cuenta seguirá protegida.
|
||||
@elseif($attempt->reason === \App\Domains\Auth\Models\ResetPasswordAttempt::REASON_STAFF_CREATED)
|
||||
Si no esperabas recibir una cuenta de scanner, podés ignorar este mensaje.
|
||||
@else
|
||||
Si no solicitaste recuperar tu contraseña, podés ignorar este mensaje.
|
||||
@endif
|
||||
</p>
|
||||
@include('mail.notifications.partials.password-action', [
|
||||
'title' => 'Recuperá tu contraseña',
|
||||
'description' => 'recibimos una solicitud para restablecer la contraseña de tu cuenta.',
|
||||
'buttonLabel' => 'Ingresar código ahora',
|
||||
'footer' => 'Si no solicitaste recuperar tu contraseña, podés ignorar este mensaje.',
|
||||
])
|
||||
|
|
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
@include('mail.notifications.partials.password-action', [
|
||||
'title' => 'Tu cuenta de escáner está lista',
|
||||
'description' => 'creamos tu cuenta de escáner en '.$brand->nombre.'. Creá tu contraseña con este código para ingresar y comenzar a escanear entradas.',
|
||||
'buttonLabel' => 'Crear mi contraseña',
|
||||
'footer' => 'Si no esperabas recibir una cuenta de escáner, podés ignorar este mensaje.',
|
||||
])
|
||||
|
|
@ -4,6 +4,7 @@ namespace Tests\Feature\Notification;
|
|||
|
||||
use App\Domains\Attachable\Enums\AttachmentType;
|
||||
use App\Domains\Attachable\Models\Attachment;
|
||||
use App\Domains\Auth\Models\ResetPasswordAttempt;
|
||||
use App\Domains\Auth\Models\User;
|
||||
use App\Domains\Catalog\Models\CatalogItem;
|
||||
use App\Domains\Integration\Models\Integration;
|
||||
|
|
@ -133,15 +134,64 @@ class NotificationMailServiceTest extends TestCase
|
|||
);
|
||||
|
||||
Mail::assertSent(Mailable::class, function (Mailable $mail): bool {
|
||||
$mail->assertHasSubject('Tu cuenta de escáner está lista - Scanner Mail');
|
||||
$rendered = $mail->render();
|
||||
|
||||
return str_contains($rendered, 'https://scanner.mail.local/recuperar-contrasena/codigo')
|
||||
&& str_contains($rendered, 'Tu cuenta de escáner está lista')
|
||||
&& str_contains($rendered, 'comenzar a escanear entradas')
|
||||
&& ! str_contains($rendered, 'Recuperá tu contraseña')
|
||||
&& str_contains($rendered, 'email=ada%40example.com')
|
||||
&& str_contains($rendered, 'code=0123')
|
||||
&& str_contains($rendered, 'Crear mi');
|
||||
});
|
||||
}
|
||||
|
||||
public function test_administrator_creation_has_custom_copy_and_the_existing_code_and_link(): void
|
||||
{
|
||||
$websiteType = WebsiteType::query()->create([
|
||||
'codigo' => 'admin-mail', 'nombre' => 'Admin Mail',
|
||||
'dominio' => 'admin.mail.local', 'scanner_domain' => 'scanner.mail.local',
|
||||
]);
|
||||
$this->tenant->update(['website_type_code' => $websiteType->codigo]);
|
||||
$attempt = $this->user->resetPasswordAttempts()->create([
|
||||
'codigo' => '0456', 'reason' => ResetPasswordAttempt::REASON_ADMINISTRATOR_CREATED,
|
||||
]);
|
||||
app(NotificationMailService::class)->sendPasswordResetCode($attempt->id, $this->tenant->codigo, PasswordResetRequested::CHANNEL_ADMINAPP);
|
||||
Mail::assertSent(Mailable::class, function (Mailable $mail): bool {
|
||||
$mail->assertHasSubject('Tu cuenta de administrador está lista - Admin Mail');
|
||||
$html = $mail->render();
|
||||
|
||||
return str_contains($html, 'Tu cuenta de administrador está lista')
|
||||
&& str_contains($html, 'ingresar al panel de administración')
|
||||
&& str_contains($html, '0456')
|
||||
&& str_contains($html, 'Crear mi contraseña')
|
||||
&& str_contains($html, 'https://admin.mail.local/recuperar-contrasena/codigo?email=ada%40example.com')
|
||||
&& ! str_contains($html, 'scanner.mail.local')
|
||||
&& ! str_contains($html, 'administrator_created')
|
||||
&& ! str_contains($html, 'Recuperá tu contraseña');
|
||||
});
|
||||
}
|
||||
|
||||
public function test_locked_account_has_its_own_copy_and_the_shared_code_action(): void
|
||||
{
|
||||
$attempt = $this->user->resetPasswordAttempts()->create([
|
||||
'codigo' => '0789', 'reason' => ResetPasswordAttempt::REASON_ACCOUNT_LOCKED,
|
||||
]);
|
||||
app(NotificationMailService::class)->sendPasswordResetCode($attempt->id, $this->tenant->codigo);
|
||||
Mail::assertSent(Mailable::class, function (Mailable $mail): bool {
|
||||
$mail->assertHasSubject('Desbloqueá tu cuenta - Mail Tenant');
|
||||
$html = $mail->render();
|
||||
|
||||
return str_contains($html, 'Desbloqueá tu cuenta')
|
||||
&& str_contains($html, 'bloqueamos el acceso temporalmente')
|
||||
&& str_contains($html, '0789')
|
||||
&& str_contains($html, 'Ingresar código ahora')
|
||||
&& str_contains($html, 'https://mail.local/recuperar-contrasena/codigo')
|
||||
&& ! str_contains($html, 'account_locked');
|
||||
});
|
||||
}
|
||||
|
||||
public function test_it_sends_one_purchase_confirmation_with_generated_tickets_attached(): void
|
||||
{
|
||||
$this->useWebsiteTypeBranding();
|
||||
|
|
|
|||
Loading…
Reference in New Issue