diff --git a/app/Domains/Notification/Services/NotificationMailService.php b/app/Domains/Notification/Services/NotificationMailService.php index 9cb54ad..c76d759 100644 --- a/app/Domains/Notification/Services/NotificationMailService.php +++ b/app/Domains/Notification/Services/NotificationMailService.php @@ -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, diff --git a/resources/views/mail/notifications/account-locked.blade.php b/resources/views/mail/notifications/account-locked.blade.php new file mode 100644 index 0000000..7f2fec4 --- /dev/null +++ b/resources/views/mail/notifications/account-locked.blade.php @@ -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.', +]) diff --git a/resources/views/mail/notifications/administrator-created.blade.php b/resources/views/mail/notifications/administrator-created.blade.php new file mode 100644 index 0000000..9fe2d05 --- /dev/null +++ b/resources/views/mail/notifications/administrator-created.blade.php @@ -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.', +]) diff --git a/resources/views/mail/notifications/partials/password-action.blade.php b/resources/views/mail/notifications/partials/password-action.blade.php new file mode 100644 index 0000000..1d72694 --- /dev/null +++ b/resources/views/mail/notifications/partials/password-action.blade.php @@ -0,0 +1,24 @@ +

+ {{ $title }} +

+ +

Hola {{ $attempt->user->nombre_apellido }}, {{ $description }}

+ +

Ingresá este código en {{ $brand->nombre }}:

+ +
+ + {{ $attempt->codigo }} + +
+ +@if($recoveryUrl) +
+ + {{ $buttonLabel }} + +
+@endif + +

{{ $footer }}

diff --git a/resources/views/mail/notifications/password-reset.blade.php b/resources/views/mail/notifications/password-reset.blade.php index 2cc4aab..417c2fe 100644 --- a/resources/views/mail/notifications/password-reset.blade.php +++ b/resources/views/mail/notifications/password-reset.blade.php @@ -1,45 +1,6 @@ -

- Recuperá tu contraseña -

- -@if($attempt->reason === \App\Domains\Auth\Models\ResetPasswordAttempt::REASON_STAFF_CREATED) -

- 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. -

-@elseif($attempt->reason === \App\Domains\Auth\Models\ResetPasswordAttempt::REASON_ACCOUNT_LOCKED) -

- 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. -

-@else -

- Hola {{ $attempt->user->nombre_apellido }}, recibimos una solicitud para restablecer - la contraseña de tu cuenta. -

-@endif - -

Ingresá este código en {{ $brand->nombre }}:

- -
- - {{ $attempt->codigo }} - -
- -@if($recoveryUrl) -
- - {{ $attempt->reason === \App\Domains\Auth\Models\ResetPasswordAttempt::REASON_STAFF_CREATED ? 'Crear mi contraseña' : 'Ingresar código ahora' }} - -
-@endif - -

-@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 -

+@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.', +]) diff --git a/resources/views/mail/notifications/scanner-created.blade.php b/resources/views/mail/notifications/scanner-created.blade.php new file mode 100644 index 0000000..c9de5b8 --- /dev/null +++ b/resources/views/mail/notifications/scanner-created.blade.php @@ -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.', +]) diff --git a/tests/Feature/Notification/NotificationMailServiceTest.php b/tests/Feature/Notification/NotificationMailServiceTest.php index 26dbc29..6190db9 100644 --- a/tests/Feature/Notification/NotificationMailServiceTest.php +++ b/tests/Feature/Notification/NotificationMailServiceTest.php @@ -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();