AddCustomerCommand.php
1 year ago
AddCustomerCommandHandler.php
6 months ago
GetCustomerCommand.php
1 year ago
GetCustomerCommandHandler.php
6 months ago
GetCustomersCommand.php
1 year ago
GetCustomersCommandHandler.php
6 months ago
UpdateCustomerCommand.php
1 year ago
UpdateCustomerCommandHandler.php
5 months ago
UpdateCustomerStatusCommand.php
1 year ago
UpdateCustomerStatusCommandHandler.php
6 months ago
UpdateCustomerCommandHandler.php
221 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Application\Commands\User\Customer; |
| 4 | |
| 5 | use AmeliaBooking\Application\Commands\CommandHandler; |
| 6 | use AmeliaBooking\Application\Commands\CommandResult; |
| 7 | use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException; |
| 8 | use AmeliaBooking\Application\Services\User\UserApplicationService; |
| 9 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 10 | use AmeliaBooking\Domain\Entity\Entities; |
| 11 | use AmeliaBooking\Domain\Entity\User\AbstractUser; |
| 12 | use AmeliaBooking\Domain\Entity\User\Customer; |
| 13 | use AmeliaBooking\Domain\Factory\User\UserFactory; |
| 14 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 15 | use AmeliaBooking\Domain\ValueObjects\String\Name; |
| 16 | use AmeliaBooking\Domain\ValueObjects\String\Password; |
| 17 | use AmeliaBooking\Domain\ValueObjects\String\Phone; |
| 18 | use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException; |
| 19 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 20 | use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\CustomerBookingRepository; |
| 21 | use AmeliaBooking\Infrastructure\Repository\User\UserRepository; |
| 22 | use AmeliaBooking\Infrastructure\Services\Mailchimp\AbstractMailchimpService; |
| 23 | |
| 24 | /** |
| 25 | * Class UpdateCustomerCommandHandler |
| 26 | * |
| 27 | * @package AmeliaBooking\Application\Commands\User\Customer |
| 28 | */ |
| 29 | class UpdateCustomerCommandHandler extends CommandHandler |
| 30 | { |
| 31 | /** |
| 32 | * @param UpdateCustomerCommand $command |
| 33 | * |
| 34 | * @return CommandResult |
| 35 | * |
| 36 | * @throws InvalidArgumentException |
| 37 | * @throws NotFoundException |
| 38 | * @throws QueryExecutionException |
| 39 | * @throws AccessDeniedException |
| 40 | */ |
| 41 | public function handle(UpdateCustomerCommand $command) |
| 42 | { |
| 43 | $result = new CommandResult(); |
| 44 | |
| 45 | $this->checkMandatoryFields($command); |
| 46 | |
| 47 | /** @var UserApplicationService $userAS */ |
| 48 | $userAS = $this->getContainer()->get('application.user.service'); |
| 49 | |
| 50 | /** @var UserRepository $userRepository */ |
| 51 | $userRepository = $this->getContainer()->get('domain.users.repository'); |
| 52 | |
| 53 | /** @var AbstractMailchimpService $mailchimpService */ |
| 54 | $mailchimpService = $this->container->get('infrastructure.mailchimp.service'); |
| 55 | |
| 56 | $userRepository->beginTransaction(); |
| 57 | |
| 58 | $customerData = $command->getFields(); |
| 59 | |
| 60 | if (!$command->getPermissionService()->currentUserCanWrite(Entities::CUSTOMERS)) { |
| 61 | if ($command->getToken()) { |
| 62 | /** @var AbstractUser $provider */ |
| 63 | $provider = $command->getCabinetType() === 'provider' |
| 64 | ? $userAS->getAuthenticatedUser($command->getToken(), false, 'providerCabinet') |
| 65 | : null; |
| 66 | |
| 67 | $oldUser = $provider === null |
| 68 | ? $userAS->getAuthenticatedUser($command->getToken(), false, 'customerCabinet') |
| 69 | : $userRepository->getById($customerData['id']); |
| 70 | |
| 71 | if ( |
| 72 | $provider === null && |
| 73 | ($oldUser === null || $oldUser->getId()->getValue() !== intval($command->getArg('id'))) |
| 74 | ) { |
| 75 | $result->setResult(CommandResult::RESULT_ERROR); |
| 76 | $result->setMessage('Could not retrieve user'); |
| 77 | $result->setData( |
| 78 | [ |
| 79 | 'reauthorize' => true |
| 80 | ] |
| 81 | ); |
| 82 | |
| 83 | return $result; |
| 84 | } |
| 85 | } else { |
| 86 | throw new AccessDeniedException('You are not allowed to perform this action!'); |
| 87 | } |
| 88 | } else { |
| 89 | $oldUser = $userRepository->getById($command->getArg('id')); |
| 90 | } |
| 91 | |
| 92 | if ($command->getField('externalId') === -1) { |
| 93 | $command->setField('externalId', null); |
| 94 | } |
| 95 | |
| 96 | /** @var SettingsService $settingsService */ |
| 97 | $settingsService = $this->container->get('domain.settings.service'); |
| 98 | |
| 99 | /** @var AbstractUser $currentUser */ |
| 100 | $currentUser = $this->container->get('logged.in.user'); |
| 101 | |
| 102 | if ( |
| 103 | $command->getField('email') === '' && |
| 104 | !$settingsService->getSetting('roles', 'allowCustomerDeleteProfile') && |
| 105 | (!$currentUser || $currentUser->getType() === AbstractUser::USER_ROLE_CUSTOMER) |
| 106 | ) { |
| 107 | $result->setResult(CommandResult::RESULT_ERROR); |
| 108 | $result->setMessage('Could not update user.'); |
| 109 | |
| 110 | return $result; |
| 111 | } |
| 112 | |
| 113 | if (!isset($customerData['password'])) { |
| 114 | $customerData['translations'] = !empty($customerData['translations']) ? $customerData['translations'] : null; |
| 115 | |
| 116 | $customerData['birthday'] = !empty($customerData['birthday']) ? $customerData['birthday'] : null; |
| 117 | } |
| 118 | |
| 119 | $newUserData = array_merge($oldUser->toArray(), $customerData); |
| 120 | |
| 121 | $newUserData = apply_filters('amelia_before_customer_updated_filter', $newUserData); |
| 122 | |
| 123 | /** @var Customer $newUser */ |
| 124 | $newUser = UserFactory::create($newUserData); |
| 125 | |
| 126 | // If the phone is not set and the old phone is set, set the phone and country phone iso to null |
| 127 | if (!$customerData['phone'] && $oldUser->getPhone() && $oldUser->getPhone()->getValue()) { |
| 128 | $newUser->setPhone(new Phone(null)); |
| 129 | $newUser->setCountryPhoneIso(new Name(null)); |
| 130 | } |
| 131 | |
| 132 | if ( |
| 133 | $userRepository->getByEmail($newUser->getEmail()->getValue()) && |
| 134 | $oldUser->getEmail()->getValue() !== $newUser->getEmail()->getValue() |
| 135 | ) { |
| 136 | $result->setResult(CommandResult::RESULT_CONFLICT); |
| 137 | $result->setMessage('Email already exist.'); |
| 138 | $result->setData('This email is already in use.'); |
| 139 | |
| 140 | return $result; |
| 141 | } |
| 142 | |
| 143 | if ($command->getField('password')) { |
| 144 | $newPassword = new Password($command->getField('password')); |
| 145 | |
| 146 | $userRepository->updateFieldById($command->getArg('id'), $newPassword->getValue(), 'password'); |
| 147 | |
| 148 | if ($newUser->getExternalId() && $newUser->getExternalId()->getValue()) { |
| 149 | add_filter('amelia_user_profile_updated', '__return_true'); |
| 150 | wp_set_password($command->getField('password'), $newUser->getExternalId()->getValue()); |
| 151 | remove_filter('amelia_user_profile_updated', '__return_true'); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | do_action('amelia_before_customer_updated', $newUser ? $newUser->toArray() : null); |
| 156 | |
| 157 | if (!$userRepository->update($command->getArg('id'), $newUser)) { |
| 158 | $userRepository->rollback(); |
| 159 | |
| 160 | $result->setResult(CommandResult::RESULT_ERROR); |
| 161 | $result->setMessage('Could not update user.'); |
| 162 | |
| 163 | return $result; |
| 164 | } |
| 165 | |
| 166 | if ($command->getField('externalId') === 0) { |
| 167 | /** @var UserApplicationService $userAS */ |
| 168 | $userAS = $this->getContainer()->get('application.user.service'); |
| 169 | |
| 170 | $userAS->setWpUserIdForNewUser($command->getArg('id'), $newUser); |
| 171 | } elseif ($newUser->getExternalId() && $newUser->getExternalId()->getValue()) { |
| 172 | add_filter('amelia_user_profile_updated', '__return_true'); |
| 173 | wp_update_user( |
| 174 | [ |
| 175 | 'ID' => $newUser->getExternalId()->getValue(), |
| 176 | 'first_name' => $newUser->getFirstName() ? $newUser->getFirstName()->getValue() : '', |
| 177 | 'last_name' => $newUser->getLastName() ? $newUser->getLastName()->getValue() : '', |
| 178 | 'user_email' => $newUser->getEmail() ? $newUser->getEmail()->getValue() : '' |
| 179 | ] |
| 180 | ); |
| 181 | |
| 182 | if ($uid = get_current_user_id()) { |
| 183 | clean_user_cache($uid); |
| 184 | } |
| 185 | |
| 186 | remove_filter('amelia_user_profile_updated', '__return_true'); |
| 187 | } |
| 188 | |
| 189 | if ($command->getField('email') === '') { |
| 190 | /** @var CustomerBookingRepository $bookingRepository */ |
| 191 | $bookingRepository = $this->container->get('domain.booking.customerBooking.repository'); |
| 192 | |
| 193 | $bookingRepository->updateFieldByColumn('info', null, 'customerId', $oldUser->getId()->getValue()); |
| 194 | } |
| 195 | |
| 196 | if ( |
| 197 | $settingsService->isFeatureEnabled('mailchimp') && |
| 198 | $oldUser->getEmail() && $oldUser->getEmail()->getValue() && |
| 199 | $newUser->getEmail() && $newUser->getEmail()->getValue() |
| 200 | ) { |
| 201 | $mailchimpService->addOrUpdateSubscriber($oldUser->getEmail()->getValue(), $newUser->toArray(), false); |
| 202 | } |
| 203 | |
| 204 | $userRepository->commit(); |
| 205 | |
| 206 | do_action('amelia_after_customer_updated', $newUser ? $newUser->toArray() : null); |
| 207 | |
| 208 | $result = $userAS->getAuthenticatedUserResponse( |
| 209 | $newUser, |
| 210 | $oldUser->getEmail()->getValue() !== $newUser->getEmail()->getValue(), |
| 211 | true, |
| 212 | $oldUser->getLoginType(), |
| 213 | 'customer' |
| 214 | ); |
| 215 | |
| 216 | $result->setMessage('Successfully updated user'); |
| 217 | |
| 218 | return $result; |
| 219 | } |
| 220 | } |
| 221 |