AddCustomerCommand.php
1 year ago
AddCustomerCommandHandler.php
3 hours ago
GetCustomerCommand.php
1 year ago
GetCustomerCommandHandler.php
6 months ago
GetCustomersCommand.php
1 year ago
GetCustomersCommandHandler.php
2 months ago
UpdateCustomerCommand.php
1 year ago
UpdateCustomerCommandHandler.php
3 hours ago
UpdateCustomerNoteCommand.php
3 months ago
UpdateCustomerNoteCommandHandler.php
3 months ago
UpdateCustomerStatusCommand.php
1 year ago
UpdateCustomerStatusCommandHandler.php
6 months ago
UpdateCustomerCommandHandler.php
271 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 | $customerData['type'] = Entities::CUSTOMER; |
| 61 | |
| 62 | if (!$command->getPermissionService()->currentUserCanWrite(Entities::CUSTOMERS)) { |
| 63 | if ($command->getToken()) { |
| 64 | /** @var AbstractUser $provider */ |
| 65 | $provider = $command->getCabinetType() === 'provider' |
| 66 | ? $userAS->getAuthenticatedUser($command->getToken(), false, 'providerCabinet') |
| 67 | : null; |
| 68 | |
| 69 | $oldUser = $provider === null |
| 70 | ? $userAS->getAuthenticatedUser($command->getToken(), false, 'customerCabinet') |
| 71 | : $userRepository->getById($customerData['id']); |
| 72 | |
| 73 | if ( |
| 74 | $provider === null && |
| 75 | ($oldUser === null || $oldUser->getId()->getValue() !== intval($command->getArg('id'))) |
| 76 | ) { |
| 77 | $userRepository->rollback(); |
| 78 | |
| 79 | $result->setResult(CommandResult::RESULT_ERROR); |
| 80 | $result->setMessage('Could not retrieve user'); |
| 81 | $result->setData( |
| 82 | [ |
| 83 | 'reauthorize' => true |
| 84 | ] |
| 85 | ); |
| 86 | |
| 87 | return $result; |
| 88 | } |
| 89 | |
| 90 | // Customers updating their own profile via cabinet token cannot change their role. |
| 91 | if ($provider === null && $oldUser !== null) { |
| 92 | $customerData['type'] = $oldUser->getType(); |
| 93 | $command->setField('type', $oldUser->getType()); |
| 94 | } |
| 95 | } else { |
| 96 | throw new AccessDeniedException('You are not allowed to perform this action!'); |
| 97 | } |
| 98 | } else { |
| 99 | $oldUser = $userRepository->getById($command->getArg('id')); |
| 100 | if ($oldUser === null) { |
| 101 | $userRepository->rollback(); |
| 102 | |
| 103 | $result->setResult(CommandResult::RESULT_ERROR); |
| 104 | $result->setMessage('Could not retrieve user'); |
| 105 | return $result; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | if ($command->getField('externalId') === -1) { |
| 110 | $command->setField('externalId', null); |
| 111 | } |
| 112 | |
| 113 | /** @var SettingsService $settingsService */ |
| 114 | $settingsService = $this->container->get('domain.settings.service'); |
| 115 | |
| 116 | /** @var AbstractUser $currentUser */ |
| 117 | $currentUser = $this->container->get('logged.in.user'); |
| 118 | |
| 119 | if ( |
| 120 | $command->getField('email') === '' && |
| 121 | !$settingsService->getSetting('roles', 'allowCustomerDeleteProfile') && |
| 122 | (!$currentUser || $currentUser->getType() === AbstractUser::USER_ROLE_CUSTOMER) |
| 123 | ) { |
| 124 | $result->setResult(CommandResult::RESULT_ERROR); |
| 125 | $result->setMessage('Could not update user.'); |
| 126 | |
| 127 | return $result; |
| 128 | } |
| 129 | |
| 130 | if (!isset($customerData['password'])) { |
| 131 | $customerData['translations'] = !empty($customerData['translations']) ? $customerData['translations'] : null; |
| 132 | |
| 133 | $customerData['birthday'] = !empty($customerData['birthday']) ? $customerData['birthday'] : null; |
| 134 | } |
| 135 | |
| 136 | $newUserData = array_merge($oldUser->toArray(), $customerData); |
| 137 | |
| 138 | $newUserData = apply_filters('amelia_before_customer_updated_filter', $newUserData); |
| 139 | |
| 140 | /** @var Customer $newUser */ |
| 141 | $newUser = UserFactory::create($newUserData); |
| 142 | |
| 143 | $oldExternalId = $oldUser->getExternalId() ? $oldUser->getExternalId()->getValue() : null; |
| 144 | $newExternalId = $newUser->getExternalId() ? $newUser->getExternalId()->getValue() : null; |
| 145 | |
| 146 | if ( |
| 147 | $oldExternalId !== $newExternalId && |
| 148 | ( |
| 149 | !$currentUser || |
| 150 | !( |
| 151 | $currentUser->getType() === AbstractUser::USER_ROLE_ADMIN || |
| 152 | $currentUser->getType() === AbstractUser::USER_ROLE_MANAGER |
| 153 | ) |
| 154 | ) |
| 155 | ) { |
| 156 | // Non-admin/manager cannot change externalId at all |
| 157 | |
| 158 | $result->setResult(CommandResult::RESULT_ERROR); |
| 159 | $result->setMessage('Could not update user.'); |
| 160 | |
| 161 | return $result; |
| 162 | } |
| 163 | |
| 164 | if ( |
| 165 | $newExternalId && |
| 166 | !$userAS->isRoleForExternalIdAllowed($newExternalId, Entities::CUSTOMER) |
| 167 | ) { |
| 168 | // Linking to existing WP user must pass role check |
| 169 | |
| 170 | $result->setResult(CommandResult::RESULT_ERROR); |
| 171 | $result->setMessage('Could not update user.'); |
| 172 | |
| 173 | return $result; |
| 174 | } |
| 175 | |
| 176 | // If the phone is not set and the old phone is set, set the phone and country phone iso to null |
| 177 | if (empty($customerData['phone']) && $oldUser->getPhone() && $oldUser->getPhone()->getValue()) { |
| 178 | $newUser->setPhone(new Phone(null)); |
| 179 | $newUser->setCountryPhoneIso(new Name(null)); |
| 180 | } |
| 181 | |
| 182 | if ( |
| 183 | $userRepository->getByEmail($newUser->getEmail()->getValue()) && |
| 184 | $oldUser->getEmail()->getValue() !== $newUser->getEmail()->getValue() |
| 185 | ) { |
| 186 | $result->setResult(CommandResult::RESULT_CONFLICT); |
| 187 | $result->setMessage('Email already exist.'); |
| 188 | $result->setData('This email is already in use.'); |
| 189 | |
| 190 | return $result; |
| 191 | } |
| 192 | |
| 193 | if ($command->getField('password')) { |
| 194 | $newPassword = new Password($command->getField('password')); |
| 195 | |
| 196 | $userRepository->updateFieldById($command->getArg('id'), $newPassword->getValue(), 'password'); |
| 197 | |
| 198 | if ($newUser->getExternalId() && $newUser->getExternalId()->getValue()) { |
| 199 | add_filter('amelia_user_profile_updated', '__return_true'); |
| 200 | wp_set_password($command->getField('password'), $newUser->getExternalId()->getValue()); |
| 201 | remove_filter('amelia_user_profile_updated', '__return_true'); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | do_action('amelia_before_customer_updated', $newUser ? $newUser->toArray() : null); |
| 206 | |
| 207 | if (!$userRepository->update($command->getArg('id'), $newUser)) { |
| 208 | $userRepository->rollback(); |
| 209 | |
| 210 | $result->setResult(CommandResult::RESULT_ERROR); |
| 211 | $result->setMessage('Could not update user.'); |
| 212 | |
| 213 | return $result; |
| 214 | } |
| 215 | |
| 216 | if ($command->getField('externalId') === 0) { |
| 217 | /** @var UserApplicationService $userAS */ |
| 218 | $userAS = $this->getContainer()->get('application.user.service'); |
| 219 | |
| 220 | $userAS->setWpUserIdForNewUser($command->getArg('id'), $newUser, Entities::CUSTOMER); |
| 221 | } elseif ($newUser->getExternalId() && $newUser->getExternalId()->getValue()) { |
| 222 | add_filter('amelia_user_profile_updated', '__return_true'); |
| 223 | wp_update_user( |
| 224 | [ |
| 225 | 'ID' => $newUser->getExternalId()->getValue(), |
| 226 | 'first_name' => $newUser->getFirstName() ? $newUser->getFirstName()->getValue() : '', |
| 227 | 'last_name' => $newUser->getLastName() ? $newUser->getLastName()->getValue() : '', |
| 228 | 'user_email' => $newUser->getEmail() ? $newUser->getEmail()->getValue() : '' |
| 229 | ] |
| 230 | ); |
| 231 | |
| 232 | if ($uid = get_current_user_id()) { |
| 233 | clean_user_cache($uid); |
| 234 | } |
| 235 | |
| 236 | remove_filter('amelia_user_profile_updated', '__return_true'); |
| 237 | } |
| 238 | |
| 239 | if ($command->getField('email') === '') { |
| 240 | /** @var CustomerBookingRepository $bookingRepository */ |
| 241 | $bookingRepository = $this->container->get('domain.booking.customerBooking.repository'); |
| 242 | |
| 243 | $bookingRepository->updateFieldByColumn('info', null, 'customerId', $oldUser->getId()->getValue()); |
| 244 | } |
| 245 | |
| 246 | if ( |
| 247 | $settingsService->isFeatureEnabled('mailchimp') && |
| 248 | $oldUser->getEmail() && $oldUser->getEmail()->getValue() && |
| 249 | $newUser->getEmail() && $newUser->getEmail()->getValue() |
| 250 | ) { |
| 251 | $mailchimpService->addOrUpdateSubscriber($oldUser->getEmail()->getValue(), $newUser->toArray(), false); |
| 252 | } |
| 253 | |
| 254 | $userRepository->commit(); |
| 255 | |
| 256 | do_action('amelia_after_customer_updated', $newUser ? $newUser->toArray() : null); |
| 257 | |
| 258 | $result = $userAS->getAuthenticatedUserResponse( |
| 259 | $newUser, |
| 260 | $oldUser->getEmail()->getValue() !== $newUser->getEmail()->getValue(), |
| 261 | true, |
| 262 | $oldUser->getLoginType(), |
| 263 | 'customer' |
| 264 | ); |
| 265 | |
| 266 | $result->setMessage('Successfully updated user'); |
| 267 | |
| 268 | return $result; |
| 269 | } |
| 270 | } |
| 271 |