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 | |
| 24 | public function execute(User $user, UpdateUserDTO $userDTO) |
| 25 | { |
| 26 | if ($user->status == "Invited") { |
| 27 | throw new ExpectedException("This user has not signed up yet. Please try again later."); |
| 28 | } |
| 29 | |
| 30 | if ($user->email != $userDTO->email) { |
| 31 | $this->changeEmail($user, $userDTO->email); |
| 32 | } |
| 33 | |
| 34 | $user->email = $userDTO->email; |
| 35 | $user->first_name = $userDTO->firstName; |
| 36 | $user->last_name = $userDTO->lastName; |
| 37 | |
| 38 | $saveUser = $user->save(); |
| 39 | |
| 40 | if (!$saveUser) { |
| 41 | throw new ExpectedException("Failed to update the users data. Please try again later."); |
| 42 | } |
| 43 | |
| 44 | return $saveUser; |
| 45 | } |
| 46 | |
| 47 | private function changeEmail(User $user, string $email) |
| 48 | { |
| 49 | $emailExists = User::where('email', $email)->first(); |
| 50 | |
| 51 | if ($emailExists) { |
| 52 | throw new ExpectedException("This email is already in use. Please try again with a different email."); |
| 53 | } |
| 54 | |
| 55 | if ($user->status == "Invited") { |
| 56 | $invitation = AdminUserInvitation::where('email', $user->email)->first(); |
| 57 | |
| 58 | if ($invitation) { |
| 59 | $invitationDTO = new SendUserInvitationDTO( |
| 60 | $user, |
| 61 | $invitation, |
| 62 | $email, |
| 63 | $invitation->admin_email, |
| 64 | $user->company->name, |
| 65 | ); |
| 66 | |
| 67 | $this->sendUserInvitationAction->execute($invitationDTO); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | if (strpos($this->getCurrentPlan($user)->identifier, 'pro') !== false && $user->instancy_id != null) { |
| 72 | $instancyDTO = new stdClass(); |
| 73 | $instancyDTO->email = $email; |
| 74 | $instancyDTO->instancyId = $user->instancy_id; |
| 75 | |
| 76 | UpdateUserEmail::dispatch($instancyDTO); |
| 77 | } |
| 78 | |
| 79 | if ($user->hubspot_id) { |
| 80 | $data = [ |
| 81 | "email" => $email |
| 82 | ]; |
| 83 | |
| 84 | try { |
| 85 | (new HubspotService)->batchUpdate($user->hubspot_id, $data); |
| 86 | } catch (\Exception $e) { |
| 87 | Log::error('Error updating user email in Hubspot: ' . $e->getMessage()); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | } |