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
GetProviderCommandHandler.php
180 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\User\ProviderApplicationService; |
| 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\Provider; |
| 13 | use AmeliaBooking\Domain\Services\DateTime\DateTimeService; |
| 14 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 15 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 16 | use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\AppointmentRepository; |
| 17 | use AmeliaBooking\Infrastructure\Repository\User\ProviderRepository; |
| 18 | use AmeliaBooking\Infrastructure\Services\Google\AbstractGoogleCalendarService; |
| 19 | use AmeliaBooking\Infrastructure\Services\Google\AbstractGoogleCalendarMiddlewareService; |
| 20 | use AmeliaBooking\Infrastructure\Services\Outlook\AbstractOutlookCalendarService; |
| 21 | use Interop\Container\Exception\ContainerException; |
| 22 | use Slim\Exception\ContainerValueNotFoundException; |
| 23 | |
| 24 | /** |
| 25 | * Class GetProviderCommandHandler |
| 26 | * |
| 27 | * @package AmeliaBooking\Application\Commands\User\Provider |
| 28 | */ |
| 29 | class GetProviderCommandHandler extends CommandHandler |
| 30 | { |
| 31 | /** |
| 32 | * @param GetProviderCommand $command |
| 33 | * |
| 34 | * @return CommandResult |
| 35 | * @throws ContainerValueNotFoundException |
| 36 | * @throws AccessDeniedException |
| 37 | * @throws QueryExecutionException |
| 38 | * @throws ContainerException |
| 39 | * @throws InvalidArgumentException |
| 40 | */ |
| 41 | public function handle(GetProviderCommand $command) |
| 42 | { |
| 43 | /** @var int $providerId */ |
| 44 | $providerId = (int)$command->getField('id'); |
| 45 | |
| 46 | /** @var AbstractUser $currentUser */ |
| 47 | $currentUser = $this->container->get('logged.in.user'); |
| 48 | |
| 49 | if ( |
| 50 | !$command->getPermissionService()->currentUserCanRead(Entities::EMPLOYEES) || |
| 51 | ( |
| 52 | !$command->getPermissionService()->currentUserCanReadOthers(Entities::EMPLOYEES) && |
| 53 | $currentUser->getId()->getValue() !== $providerId |
| 54 | ) |
| 55 | ) { |
| 56 | throw new AccessDeniedException('You are not allowed to read employee.'); |
| 57 | } |
| 58 | |
| 59 | $result = new CommandResult(); |
| 60 | |
| 61 | /** @var AppointmentRepository $appointmentRepository */ |
| 62 | $appointmentRepository = $this->container->get('domain.booking.appointment.repository'); |
| 63 | /** @var ProviderApplicationService $providerService */ |
| 64 | $providerService = $this->container->get('application.user.provider.service'); |
| 65 | /** @var SettingsService $settingsService */ |
| 66 | $settingsService = $this->container->get('domain.settings.service'); |
| 67 | /** @var AbstractGoogleCalendarService $googleCalService */ |
| 68 | $googleCalService = $this->container->get('infrastructure.google.calendar.service'); |
| 69 | /** @var AbstractOutlookCalendarService $outlookCalendarService */ |
| 70 | $outlookCalendarService = $this->container->get('infrastructure.outlook.calendar.service'); |
| 71 | /** @var ProviderRepository $providerRepository */ |
| 72 | $providerRepository = $this->container->get('domain.users.providers.repository'); |
| 73 | /** @var AbstractGoogleCalendarMiddlewareService $googleCalendarMiddlewareService */ |
| 74 | $googleCalendarMiddlewareService = $this->container->get( |
| 75 | 'infrastructure.google.calendar.middleware.service' |
| 76 | ); |
| 77 | |
| 78 | $companyDaysOff = $settingsService->getCategorySettings('daysOff'); |
| 79 | |
| 80 | $companyDayOff = $providerService->checkIfTodayIsCompanyDayOff($companyDaysOff); |
| 81 | |
| 82 | /** @var Provider $provider */ |
| 83 | $provider = $providerService->getProviderWithServicesAndSchedule($providerId, true); |
| 84 | |
| 85 | $providerService->modifyPeriodsWithSingleLocationAfterFetch($provider->getWeekDayList()); |
| 86 | $providerService->modifyPeriodsWithSingleLocationAfterFetch($provider->getSpecialDayList()); |
| 87 | |
| 88 | $futureAppointmentsServicesIds = $appointmentRepository->getFutureAppointmentsServicesIds( |
| 89 | [$provider->getId()->getValue()], |
| 90 | DateTimeService::getNowDateTime(), |
| 91 | null |
| 92 | ); |
| 93 | |
| 94 | $providerArray = $providerService->manageProvidersActivity( |
| 95 | [$provider->toArray()], |
| 96 | $companyDayOff |
| 97 | )[0]; |
| 98 | |
| 99 | $successfulGoogleConnection = true; |
| 100 | |
| 101 | $successfulOutlookConnection = true; |
| 102 | |
| 103 | $providerArray['googleCalendar']['calendarList'] = []; |
| 104 | $providerArray['googleCalendar']['calendarId'] = null; |
| 105 | |
| 106 | if ($settingsService->isFeatureEnabled('googleCalendar')) { |
| 107 | try { |
| 108 | $googleCalendar = $settingsService->getCategorySettings('googleCalendar'); |
| 109 | if (!$googleCalendar['accessToken']) { |
| 110 | $providerArray['googleCalendar']['calendarList'] = $googleCalService->listCalendarList($provider); |
| 111 | $providerArray['googleCalendar']['calendarId'] = $googleCalService->getProviderGoogleCalendarId($provider); |
| 112 | } else { |
| 113 | $providerArray['googleCalendar']['calendarList'] = $googleCalendarMiddlewareService->getCalendarList($providerArray['googleCalendar']); |
| 114 | $providerArray['googleCalendar']['calendarId'] = $provider->getGoogleCalendar() ? |
| 115 | $provider->getGoogleCalendar()->getCalendarId()->getValue() : |
| 116 | null; |
| 117 | } |
| 118 | } catch (\Exception $e) { |
| 119 | $providerArray['googleCalendar']['calendarId'] = !empty($providerArray['googleCalendar']['calendarId']) |
| 120 | ? $providerArray['googleCalendar']['calendarId'] |
| 121 | : null; |
| 122 | |
| 123 | $providerArray['googleCalendar']['calendarList'] = []; |
| 124 | |
| 125 | $providerRepository->updateErrorColumn($providerId, $e->getMessage()); |
| 126 | $successfulGoogleConnection = false; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | try { |
| 131 | $providerArray['outlookCalendar']['calendarList'] = $outlookCalendarService->listCalendarList($provider); |
| 132 | |
| 133 | $providerArray['outlookCalendar']['calendarId'] = $outlookCalendarService->getProviderOutlookCalendarId( |
| 134 | $provider |
| 135 | ); |
| 136 | } catch (\Exception $e) { |
| 137 | $providerArray['outlookCalendar']['calendarId'] = !empty($providerArray['outlookCalendar']['calendarId']) |
| 138 | ? $providerArray['outlookCalendar']['calendarId'] |
| 139 | : null; |
| 140 | |
| 141 | $providerArray['outlookCalendar']['calendarList'] = []; |
| 142 | |
| 143 | $providerRepository->updateErrorColumn($providerId, $e->getMessage()); |
| 144 | $successfulOutlookConnection = false; |
| 145 | } |
| 146 | |
| 147 | $providerArray['mandatoryServicesIds'] = $providerService->getMandatoryServicesIds($providerId); |
| 148 | |
| 149 | $providerArray['eventList'] = array_map( |
| 150 | function ($event) { |
| 151 | return [ |
| 152 | 'name' => $event['name'], |
| 153 | 'id' => $event['id'], |
| 154 | 'periods' => $event['periods'], |
| 155 | 'color' => $event['color'], |
| 156 | 'organizer' => ['id' => $event['organizerId']] |
| 157 | ]; |
| 158 | }, |
| 159 | $providerArray['eventList'] |
| 160 | ); |
| 161 | |
| 162 | $providerArray = apply_filters('amelia_get_provider_filter', $providerArray); |
| 163 | |
| 164 | do_action('amelia_get_provider', $providerArray); |
| 165 | |
| 166 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 167 | $result->setMessage('Successfully retrieved user.'); |
| 168 | $result->setData( |
| 169 | [ |
| 170 | Entities::USER => $providerArray, |
| 171 | 'successfulGoogleConnection' => $successfulGoogleConnection, |
| 172 | 'successfulOutlookConnection' => $successfulOutlookConnection, |
| 173 | 'futureAppointmentsServicesIds' => $futureAppointmentsServicesIds, |
| 174 | ] |
| 175 | ); |
| 176 | |
| 177 | return $result; |
| 178 | } |
| 179 | } |
| 180 |