Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 38 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| UpdateUserAction | |
0.00% |
0 / 38 |
|
0.00% |
0 / 3 |
182 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| execute | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
20 | |||
| changeEmail | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
72 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Actions\Users; |
| 4 | |
| 5 | use App\DTO\User\SendUserInvitationDTO; |
| 6 | use App\DTO\User\UpdateUserDTO; |
| 7 | use App\Http\Models\Admin\AdminUserInvitation; |
| 8 | use App\Http\Models\Auth\User; |
| 9 | use App\Http\Repositories\InstancyRepository; |
| 10 | use App\Jobs\Instancy\UpdateUserEmail; |
| 11 | use App\Traits\SubscriptionTrait; |
| 12 | use stdClass; |
| 13 | |
| 14 | class UpdateUserAction |
| 15 | { |
| 16 | use SubscriptionTrait; |
| 17 | |
| 18 | public function __construct( |
| 19 | private SendUserInvitationAction $sendUserInvitationAction, |
| 20 | private InstancyRepository $instancyRepository |
| 21 | ) {} |
| 22 | |
| 23 | public function execute(User $user, UpdateUserDTO $userDTO) |
| 24 | { |
| 25 | if ($user->status == 'Invited') { |
| 26 | throw new ExpectedException('This user has not signed up yet. Please try again later.'); |
| 27 | } |
| 28 | |
| 29 | if ($user->email != $userDTO->email) { |
| 30 | $this->changeEmail($user, $userDTO->email); |
| 31 | } |
| 32 | |
| 33 | $user->email = $userDTO->email; |
| 34 | $user->first_name = $userDTO->firstName; |
| 35 | $user->last_name = $userDTO->lastName; |
| 36 | |
| 37 | $saveUser = $user->save(); |
| 38 | |
| 39 | if (! $saveUser) { |
| 40 | throw new ExpectedException('Failed to update the users data. Please try again later.'); |
| 41 | } |
| 42 | |
| 43 | return $saveUser; |
| 44 | } |
| 45 | |
| 46 | private function changeEmail(User $user, string $email) |
| 47 | { |
| 48 | $emailExists = User::where('email', $email)->first(); |
| 49 | |
| 50 | if ($emailExists) { |
| 51 | throw new ExpectedException('This email is already in use. Please try again with a different email.'); |
| 52 | } |
| 53 | |
| 54 | if ($user->status == 'Invited') { |
| 55 | $invitation = AdminUserInvitation::where('email', $user->email)->first(); |
| 56 | |
| 57 | if ($invitation) { |
| 58 | $invitationDTO = new SendUserInvitationDTO( |
| 59 | $user, |
| 60 | $invitation, |
| 61 | $email, |
| 62 | $invitation->admin_email, |
| 63 | $user->company->name, |
| 64 | ); |
| 65 | |
| 66 | $this->sendUserInvitationAction->execute($invitationDTO); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | if (strpos($this->getCurrentPlan($user)->identifier, 'pro') !== false && $user->instancy_id != null) { |
| 71 | $instancyDTO = new stdClass; |
| 72 | $instancyDTO->email = $email; |
| 73 | $instancyDTO->instancyId = $user->instancy_id; |
| 74 | |
| 75 | UpdateUserEmail::dispatch($instancyDTO); |
| 76 | } |
| 77 | |
| 78 | if ($user->hubspot_id) { |
| 79 | $data = [ |
| 80 | 'email' => $email, |
| 81 | ]; |
| 82 | |
| 83 | try { |
| 84 | (new HubspotService)->batchUpdate($user->hubspot_id, $data); |
| 85 | } catch (\Exception $e) { |
| 86 | Log::error('Error updating user email in Hubspot: '.$e->getMessage()); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | } |