Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 33 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
SendUserInvitationAction | |
0.00% |
0 / 33 |
|
0.00% |
0 / 2 |
30 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 32 |
|
0.00% |
0 / 1 |
20 |
1 | <?php |
2 | |
3 | namespace App\Actions\Users; |
4 | |
5 | use App\DTO\User\SendUserInvitationDTO; |
6 | use App\Mail\AdminCenterAddExistingUser; |
7 | use App\Mail\AdminCenterAddNewUser; |
8 | use App\Services\Email\EmailService; |
9 | use Carbon\Carbon; |
10 | use Illuminate\Support\Str; |
11 | |
12 | class SendUserInvitationAction |
13 | { |
14 | public function __construct( |
15 | private readonly EmailService $emailService |
16 | ) {} |
17 | |
18 | public function execute(SendUserInvitationDTO $dto) |
19 | { |
20 | if ($dto->user && $dto->user->created_at->diffInMinutes($dto->invitation?->created_at) > 5) { |
21 | $this->emailService->send( |
22 | $dto->newEmail, |
23 | new AdminCenterAddExistingUser( |
24 | admin_email: $dto->adminEmail, |
25 | email: $dto->newEmail, |
26 | company: $dto->companyName, |
27 | ), |
28 | 'send_user_invitation' |
29 | ); |
30 | } else { |
31 | $password = Str::random(16); |
32 | $encrypted_password = bcrypt($password); |
33 | $temp_password_expiry = Carbon::parse($dto->invitation->temp_password_expiry); |
34 | |
35 | if ($dto->user) { |
36 | $dto->user->password = $encrypted_password; |
37 | $dto->user->temp_password = $encrypted_password; |
38 | $dto->user->save(); |
39 | } |
40 | |
41 | $dto->invitation->email = $dto->newEmail; |
42 | $dto->invitation->password = $encrypted_password; |
43 | $dto->invitation->temp_password = $encrypted_password; |
44 | $dto->invitation->admin_email = $dto->adminEmail; |
45 | $dto->invitation->save(); |
46 | |
47 | $this->emailService->send( |
48 | $dto->newEmail, |
49 | new AdminCenterAddNewUser( |
50 | email: $dto->newEmail, |
51 | password: $password, |
52 | inviter: $dto->adminEmail, |
53 | password_expiry: $temp_password_expiry->format("m/d/Y") . " at " . $temp_password_expiry->format("h:i A") |
54 | ), |
55 | 'send_new_user_invitation' |
56 | ); |
57 | } |
58 | } |
59 | } |