Skip to content

Commit

Permalink
feat(notification): send an email notice to the user on `password cha…
Browse files Browse the repository at this point in the history
…nge`
  • Loading branch information
Mohammad-Alavi committed Apr 21, 2022
1 parent 78b7131 commit 760b2e0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Apiato\Core\Exceptions\IncorrectIdException;
use App\Containers\AppSection\User\Models\User;
use App\Containers\AppSection\User\Notifications\PasswordUpdatedNotification;
use App\Containers\AppSection\User\Tasks\UpdateUserTask;
use App\Containers\AppSection\User\UI\API\Requests\UpdateUserPasswordRequest;
use App\Ship\Exceptions\NotFoundException;
Expand All @@ -22,10 +23,13 @@ class UpdateUserPasswordAction extends Action
public function run(UpdateUserPasswordRequest $request): User
{
$sanitizedData = $request->sanitizeInput([
'current_password',
'new_password',
]);

return app(UpdateUserTask::class)->run(['password' => $sanitizedData['new_password']], $request->id);
$user = app(UpdateUserTask::class)->run(['password' => $sanitizedData['new_password']], $request->id);

$user->notify(new PasswordUpdatedNotification());

return $user;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Containers\AppSection\User\Notifications;

use App\Ship\Parents\Models\UserModel;
use App\Ship\Parents\Notifications\Notification;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class PasswordUpdatedNotification extends Notification implements ShouldQueue
{
use Queueable;

public function via($notifiable): array
{
return ['mail'];
}

public function toMail(UserModel $notifiable): MailMessage
{
return (new MailMessage())
->subject('Account Change Notice')
->line('We wanted to let you know that some information was changed for your account:')
->line('Your password has been change.')
->line('If you recently made account changes, please disregard this message. However, if you did NOT make any changes to your account, we recommend you change your password and make appropriate corrections as soon as possible to ensure account security.');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace App\Containers\AppSection\User\UI\API\Tests\Functional;

use App\Containers\AppSection\User\Notifications\PasswordUpdatedNotification;
use App\Containers\AppSection\User\UI\API\Tests\ApiTestCase;
use Illuminate\Support\Facades\Notification;
use Illuminate\Testing\Fluent\AssertableJson;

/**
Expand All @@ -22,6 +24,8 @@ class UpdateUserPasswordTest extends ApiTestCase

public function testGivenUserAlreadyHaveAPassword_UpdateUserPassword(): void
{
Notification::fake();

$user = $this->getTestingUser([
'password' => 'Av@dakedavra!',
]);
Expand All @@ -40,6 +44,8 @@ public function testGivenUserAlreadyHaveAPassword_UpdateUserPassword(): void
->missing('data.password')
->etc()
);

Notification::assertSentTo($user, PasswordUpdatedNotification::class);
}

public function testNewPasswordFieldShouldBeRequired(): void
Expand Down

0 comments on commit 760b2e0

Please sign in to comment.