AddProviderCommand.php
1 year ago
AddProviderCommandHandler.php
1 year ago
GetProviderCommand.php
7 years ago
GetProviderCommandHandler.php
6 months ago
GetProvidersCommand.php
1 year ago
GetProvidersCommandHandler.php
6 months ago
UpdateProviderCommand.php
1 year ago
UpdateProviderCommandHandler.php
5 months ago
UpdateProviderStatusCommand.php
1 year ago
UpdateProviderStatusCommandHandler.php
6 months ago
UpdateProviderCommandHandler.php
285 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Application\Commands\User\Provider; |
| 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\Entity\EntityApplicationService; |
| 9 | use AmeliaBooking\Application\Services\User\ProviderApplicationService; |
| 10 | use AmeliaBooking\Application\Services\User\UserApplicationService; |
| 11 | use AmeliaBooking\Domain\Collection\Collection; |
| 12 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 13 | use AmeliaBooking\Domain\Entity\Entities; |
| 14 | use AmeliaBooking\Domain\Entity\User\AbstractUser; |
| 15 | use AmeliaBooking\Domain\Entity\User\Provider; |
| 16 | use AmeliaBooking\Domain\Factory\User\UserFactory; |
| 17 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 18 | use AmeliaBooking\Domain\ValueObjects\String\Password; |
| 19 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 20 | use AmeliaBooking\Infrastructure\Repository\User\ProviderRepository; |
| 21 | use AmeliaBooking\Infrastructure\Services\Apple\AbstractAppleCalendarService; |
| 22 | use Exception; |
| 23 | use Interop\Container\Exception\ContainerException; |
| 24 | use Slim\Exception\ContainerValueNotFoundException; |
| 25 | use AmeliaBooking\Domain\ValueObjects\String\Name; |
| 26 | use AmeliaBooking\Domain\ValueObjects\String\Phone; |
| 27 | |
| 28 | /** |
| 29 | * Class UpdateProviderCommandHandler |
| 30 | * |
| 31 | * @package AmeliaBooking\Application\Commands\User\Provider |
| 32 | */ |
| 33 | class UpdateProviderCommandHandler extends CommandHandler |
| 34 | { |
| 35 | /** |
| 36 | * @param UpdateProviderCommand $command |
| 37 | * |
| 38 | * @return CommandResult |
| 39 | * @throws ContainerValueNotFoundException |
| 40 | * @throws AccessDeniedException |
| 41 | * @throws InvalidArgumentException |
| 42 | * @throws QueryExecutionException |
| 43 | * @throws ContainerException |
| 44 | * @throws Exception |
| 45 | */ |
| 46 | public function handle(UpdateProviderCommand $command) |
| 47 | { |
| 48 | $result = new CommandResult(); |
| 49 | |
| 50 | $this->checkMandatoryFields($command); |
| 51 | |
| 52 | /** @var ProviderRepository $providerRepository */ |
| 53 | $providerRepository = $this->container->get('domain.users.providers.repository'); |
| 54 | |
| 55 | /** @var ProviderApplicationService $providerAS */ |
| 56 | $providerAS = $this->container->get('application.user.provider.service'); |
| 57 | |
| 58 | /** @var SettingsService $settingsDS */ |
| 59 | $settingsDS = $this->container->get('domain.settings.service'); |
| 60 | |
| 61 | $userId = (int)$command->getArg('id'); |
| 62 | |
| 63 | /** @var AbstractUser $currentUser */ |
| 64 | $currentUser = $this->container->get('logged.in.user'); |
| 65 | |
| 66 | /** @var UserApplicationService $userAS */ |
| 67 | $userAS = $this->getContainer()->get('application.user.service'); |
| 68 | |
| 69 | if ( |
| 70 | !$command->getPermissionService()->currentUserCanWrite(Entities::EMPLOYEES) || |
| 71 | ( |
| 72 | !$command->getPermissionService()->currentUserCanWriteOthers(Entities::EMPLOYEES) && |
| 73 | ( |
| 74 | !$currentUser->getId() || |
| 75 | $currentUser->getId()->getValue() !== $userId |
| 76 | ) |
| 77 | ) |
| 78 | ) { |
| 79 | $oldUser = $userAS->getAuthenticatedUser($command->getToken(), false, 'providerCabinet'); |
| 80 | |
| 81 | if ( |
| 82 | $oldUser === null || |
| 83 | ($command->getField('externalId') && (!$oldUser->getExternalId() || $oldUser->getExternalId()->getValue() !== $command->getField('externalId'))) |
| 84 | ) { |
| 85 | $result->setResult(CommandResult::RESULT_ERROR); |
| 86 | $result->setMessage('Could not retrieve user'); |
| 87 | $result->setData( |
| 88 | [ |
| 89 | 'reauthorize' => true |
| 90 | ] |
| 91 | ); |
| 92 | |
| 93 | return $result; |
| 94 | } |
| 95 | |
| 96 | $oldUser = $providerAS->getProviderWithServicesAndSchedule($oldUser->getId()->getValue()); |
| 97 | } else { |
| 98 | $oldUser = $providerAS->getProviderWithServicesAndSchedule($userId); |
| 99 | } |
| 100 | |
| 101 | $command->setField('id', $userId); |
| 102 | |
| 103 | $providerData = $command->getFields(); |
| 104 | |
| 105 | if (!isset($providerData['stripeConnect'])) { |
| 106 | $providerData['stripeConnect'] = null; |
| 107 | } |
| 108 | |
| 109 | if (!isset($providerData['zoomUserId'])) { |
| 110 | $providerData['zoomUserId'] = null; |
| 111 | } |
| 112 | |
| 113 | if (!isset($providerData['appleCalendarId'])) { |
| 114 | $providerData['appleCalendarId'] = null; |
| 115 | } |
| 116 | |
| 117 | if (!isset($providerData['employeeAppleCalendar'])) { |
| 118 | $providerData['employeeAppleCalendar'] = null; |
| 119 | } else { |
| 120 | /** @var AbstractAppleCalendarService $appleCalendarService */ |
| 121 | $appleCalendarService = $this->container->get('infrastructure.apple.calendar.service'); |
| 122 | |
| 123 | $appleId = $providerData['employeeAppleCalendar']['iCloudId']; |
| 124 | $applePassword = $providerData['employeeAppleCalendar']['appSpecificPassword']; |
| 125 | |
| 126 | $credentials = $appleCalendarService->handleAppleCredentials($appleId, $applePassword); |
| 127 | |
| 128 | if (!$credentials) { |
| 129 | $providerData['employeeAppleCalendar'] = null; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /** @var EntityApplicationService $entityService */ |
| 134 | $entityService = $this->container->get('application.entity.service'); |
| 135 | |
| 136 | $entityService->removeMissingEntitiesForProvider($providerData); |
| 137 | |
| 138 | if (!!$oldUser->getBadgeId() && !isset($providerData['badgeId'])) { |
| 139 | $providerData['badgeId'] = null; |
| 140 | } |
| 141 | |
| 142 | if ($oldUser->getTimeZone() && $settingsDS->isFeatureEnabled('timezones') === false) { |
| 143 | $providerData['timeZone'] = $oldUser->getTimeZone()->getValue(); |
| 144 | } |
| 145 | |
| 146 | $newUserData = array_merge($oldUser->toArray(), $providerData); |
| 147 | |
| 148 | $newUserData = apply_filters('amelia_before_provider_updated_filter', $newUserData, $oldUser->toArray()); |
| 149 | |
| 150 | /** @var Provider $newUser */ |
| 151 | $newUser = UserFactory::create($newUserData); |
| 152 | |
| 153 | // If the phone is not set and the old phone is set, set the phone and country phone iso to null |
| 154 | if (!$providerData['phone'] && $oldUser->getPhone() && $oldUser->getPhone()->getValue()) { |
| 155 | $newUser->setPhone(new Phone(null)); |
| 156 | $newUser->setCountryPhoneIso(new Name(null)); |
| 157 | } |
| 158 | |
| 159 | $newUser->setDayOffList( |
| 160 | $providerAS->getModifiedDayList( |
| 161 | $newUser->getDayOffList(), |
| 162 | $oldUser->getDayOffList(), |
| 163 | !empty($newUserData['removedDayOffList']) |
| 164 | ? UserFactory::createDayOffList($newUserData['removedDayOffList']) |
| 165 | : new Collection() |
| 166 | ) |
| 167 | ); |
| 168 | |
| 169 | $newUser->setSpecialDayList( |
| 170 | $providerAS->getModifiedDayList( |
| 171 | $newUser->getSpecialDayList(), |
| 172 | $oldUser->getSpecialDayList(), |
| 173 | !empty($newUserData['removedSpecialDayList']) |
| 174 | ? UserFactory::createSpecialDayList($newUserData['removedSpecialDayList']) |
| 175 | : new Collection() |
| 176 | ) |
| 177 | ); |
| 178 | |
| 179 | if ($command->getUserApplicationService()->checkProviderPermissions($currentUser, $command->getToken())) { |
| 180 | $rolesSettings = $settingsDS->getCategorySettings('roles'); |
| 181 | |
| 182 | if (!$rolesSettings['allowConfigureServices']) { |
| 183 | $newUser->setServiceList($oldUser->getServiceList()); |
| 184 | } |
| 185 | |
| 186 | if (!$rolesSettings['allowConfigureSchedule']) { |
| 187 | $newUser->setWeekDayList($oldUser->getWeekDayList()); |
| 188 | } |
| 189 | |
| 190 | if (!$rolesSettings['allowConfigureDaysOff']) { |
| 191 | $newUser->setDayOffList($oldUser->getDayOffList()); |
| 192 | } |
| 193 | |
| 194 | if (!$rolesSettings['allowConfigureSpecialDays']) { |
| 195 | $newUser->setSpecialDayList($oldUser->getSpecialDayList()); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | $providerRepository->beginTransaction(); |
| 200 | |
| 201 | if ( |
| 202 | $providerRepository->getByEmail($newUser->getEmail()->getValue()) && |
| 203 | $oldUser->getEmail()->getValue() !== $newUser->getEmail()->getValue() |
| 204 | ) { |
| 205 | $result->setResult(CommandResult::RESULT_CONFLICT); |
| 206 | $result->setMessage('Email already exist.'); |
| 207 | $result->setData('This email is already in use.'); |
| 208 | |
| 209 | return $result; |
| 210 | } |
| 211 | |
| 212 | if ($command->getField('password')) { |
| 213 | $newPassword = new Password($command->getField('password')); |
| 214 | |
| 215 | $providerRepository->updateFieldById($command->getArg('id'), $newPassword->getValue(), 'password'); |
| 216 | |
| 217 | if ($newUser->getExternalId() && $newUser->getExternalId()->getValue()) { |
| 218 | add_filter('amelia_user_profile_updated', '__return_true'); |
| 219 | wp_set_password($command->getField('password'), $newUser->getExternalId()->getValue()); |
| 220 | remove_filter('amelia_user_profile_updated', '__return_true'); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | do_action('amelia_before_provider_updated', $newUser ? $newUser->toArray() : null, $oldUser ? $oldUser->toArray() : null); |
| 225 | |
| 226 | try { |
| 227 | if (!$providerAS->update($oldUser, $newUser)) { |
| 228 | $providerRepository->rollback(); |
| 229 | return $result; |
| 230 | } |
| 231 | |
| 232 | if ($command->getField('externalId') === 0) { |
| 233 | /** @var UserApplicationService $userAS */ |
| 234 | $userAS = $this->getContainer()->get('application.user.service'); |
| 235 | |
| 236 | $userAS->setWpUserIdForNewUser($userId, $newUser, $command->getField('password')); |
| 237 | } elseif ($newUser->getExternalId() && $newUser->getExternalId()->getValue()) { |
| 238 | add_filter('amelia_user_profile_updated', '__return_true'); |
| 239 | wp_update_user( |
| 240 | [ |
| 241 | 'ID' => $newUser->getExternalId()->getValue(), |
| 242 | 'first_name' => $newUser->getFirstName() ? $newUser->getFirstName()->getValue() : '', |
| 243 | 'last_name' => $newUser->getLastName() ? $newUser->getLastName()->getValue() : '', |
| 244 | 'user_email' => $newUser->getEmail() ? $newUser->getEmail()->getValue() : '' |
| 245 | ] |
| 246 | ); |
| 247 | |
| 248 | if ($uid = get_current_user_id()) { |
| 249 | clean_user_cache($uid); |
| 250 | } |
| 251 | |
| 252 | remove_filter('amelia_user_profile_updated', '__return_true'); |
| 253 | } |
| 254 | } catch (QueryExecutionException $e) { |
| 255 | $providerRepository->rollback(); |
| 256 | throw $e; |
| 257 | } |
| 258 | |
| 259 | $result = $userAS->getAuthenticatedUserResponse( |
| 260 | $newUser, |
| 261 | $oldUser->getEmail()->getValue() !== $newUser->getEmail()->getValue(), |
| 262 | true, |
| 263 | $oldUser->getLoginType(), |
| 264 | 'provider' |
| 265 | ); |
| 266 | |
| 267 | $result->setData( |
| 268 | array_merge( |
| 269 | $result->getData(), |
| 270 | [ |
| 271 | 'sendEmployeePanelAccessEmail' => |
| 272 | $command->getField('password') && $command->getField('sendEmployeePanelAccessEmail'), |
| 273 | 'password' => $command->getField('password') |
| 274 | ] |
| 275 | ) |
| 276 | ); |
| 277 | |
| 278 | $providerRepository->commit(); |
| 279 | |
| 280 | do_action('amelia_after_provider_updated', $newUser ? $newUser->toArray() : null, $oldUser ? $oldUser->toArray() : null); |
| 281 | |
| 282 | return $result; |
| 283 | } |
| 284 | } |
| 285 |