PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.2
Booking for Appointments and Events Calendar – Amelia v2.2
2.4.9 2.4.8 2.4.7 2.4.6 2.4.5 2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / src / Application / Commands / User / Provider / GetProviderCommandHandler.php
ameliabooking / src / Application / Commands / User / Provider Last commit date
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 4 months ago UpdateProviderStatusCommand.php 1 year ago UpdateProviderStatusCommandHandler.php 8 months ago
GetProviderCommandHandler.php
195 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\AbstractOutlookCalendarMiddlewareService;
21 use AmeliaBooking\Infrastructure\Services\Outlook\AbstractOutlookCalendarService;
22 use Interop\Container\Exception\ContainerException;
23 use Slim\Exception\ContainerValueNotFoundException;
24
25 /**
26 * Class GetProviderCommandHandler
27 *
28 * @package AmeliaBooking\Application\Commands\User\Provider
29 */
30 class GetProviderCommandHandler extends CommandHandler
31 {
32 /**
33 * @param GetProviderCommand $command
34 *
35 * @return CommandResult
36 * @throws ContainerValueNotFoundException
37 * @throws AccessDeniedException
38 * @throws QueryExecutionException
39 * @throws ContainerException
40 * @throws InvalidArgumentException
41 */
42 public function handle(GetProviderCommand $command)
43 {
44 /** @var int $providerId */
45 $providerId = (int)$command->getField('id');
46
47 /** @var AbstractUser $currentUser */
48 $currentUser = $this->container->get('logged.in.user');
49
50 if (
51 !$command->getPermissionService()->currentUserCanRead(Entities::EMPLOYEES) ||
52 (
53 !$command->getPermissionService()->currentUserCanReadOthers(Entities::EMPLOYEES) &&
54 $currentUser->getId()->getValue() !== $providerId
55 )
56 ) {
57 throw new AccessDeniedException('You are not allowed to read employee.');
58 }
59
60 $result = new CommandResult();
61
62 /** @var AppointmentRepository $appointmentRepository */
63 $appointmentRepository = $this->container->get('domain.booking.appointment.repository');
64 /** @var ProviderApplicationService $providerService */
65 $providerService = $this->container->get('application.user.provider.service');
66 /** @var SettingsService $settingsService */
67 $settingsService = $this->container->get('domain.settings.service');
68 /** @var AbstractGoogleCalendarService $googleCalService */
69 $googleCalService = $this->container->get('infrastructure.google.calendar.service');
70 /** @var AbstractOutlookCalendarService $outlookCalendarService */
71 $outlookCalendarService = $this->container->get('infrastructure.outlook.calendar.service');
72 /** @var ProviderRepository $providerRepository */
73 $providerRepository = $this->container->get('domain.users.providers.repository');
74 /** @var AbstractGoogleCalendarMiddlewareService $googleCalendarMiddlewareService */
75 $googleCalendarMiddlewareService = $this->container->get(
76 'infrastructure.google.calendar.middleware.service'
77 );
78 /** @var AbstractOutlookCalendarMiddlewareService $outlookCalendarMiddlewareService */
79 $outlookCalendarMiddlewareService = $this->container->get(
80 'infrastructure.outlook.calendar.middleware.service'
81 );
82
83 $companyDaysOff = $settingsService->getCategorySettings('daysOff');
84
85 $companyDayOff = $providerService->checkIfTodayIsCompanyDayOff($companyDaysOff);
86
87 /** @var Provider $provider */
88 $provider = $providerService->getProviderWithServicesAndSchedule($providerId, true);
89
90 $providerService->modifyPeriodsWithSingleLocationAfterFetch($provider->getWeekDayList());
91 $providerService->modifyPeriodsWithSingleLocationAfterFetch($provider->getSpecialDayList());
92
93 $futureAppointmentsServicesIds = $appointmentRepository->getFutureAppointmentsServicesIds(
94 [$provider->getId()->getValue()],
95 DateTimeService::getNowDateTime(),
96 null
97 );
98
99 $providerArray = $providerService->manageProvidersActivity(
100 [$provider->toArray()],
101 $companyDayOff
102 )[0];
103
104 $successfulGoogleConnection = true;
105
106 $successfulOutlookConnection = true;
107
108 $providerArray['googleCalendar']['calendarList'] = [];
109 $providerArray['googleCalendar']['calendarId'] = null;
110
111 $providerArray['outlookCalendar']['calendarList'] = [];
112 $providerArray['outlookCalendar']['calendarId'] = null;
113
114 if ($settingsService->isFeatureEnabled('googleCalendar')) {
115 try {
116 $googleCalendar = $settingsService->getCategorySettings('googleCalendar');
117 if (!$googleCalendar['accessToken']) {
118 $providerArray['googleCalendar']['calendarList'] = $googleCalService->listCalendarList($provider);
119 $providerArray['googleCalendar']['calendarId'] = $googleCalService->getProviderGoogleCalendarId($provider);
120 } else {
121 $providerArray['googleCalendar']['calendarList'] = $googleCalendarMiddlewareService->getCalendarList($providerArray['googleCalendar']);
122 $providerArray['googleCalendar']['calendarId'] = $provider->getGoogleCalendar() ?
123 $provider->getGoogleCalendar()->getCalendarId()->getValue() :
124 null;
125 }
126 } catch (\Exception $e) {
127 $providerArray['googleCalendar']['calendarId'] = !empty($providerArray['googleCalendar']['calendarId'])
128 ? $providerArray['googleCalendar']['calendarId']
129 : null;
130
131 $providerArray['googleCalendar']['calendarList'] = [];
132
133 $providerRepository->updateErrorColumn($providerId, $e->getMessage());
134 $successfulGoogleConnection = false;
135 }
136 }
137
138 if ($settingsService->isFeatureEnabled('outlookCalendar')) {
139 try {
140 $outlookCalendar = $settingsService->getCategorySettings('outlookCalendar');
141 if (!$outlookCalendar['accessToken']) {
142 $providerArray['outlookCalendar']['calendarList'] = $outlookCalendarService->listCalendarList($provider);
143 $providerArray['outlookCalendar']['calendarId'] = $outlookCalendarService->getProviderOutlookCalendarId($provider);
144 } else {
145 $providerArray['outlookCalendar']['calendarList'] = $outlookCalendarMiddlewareService->getCalendarList($providerArray['outlookCalendar']);
146 $providerArray['outlookCalendar']['calendarId'] = $provider->getOutlookCalendar() ?
147 $provider->getOutlookCalendar()->getCalendarId()->getValue() :
148 null;
149 }
150 } catch (\Exception $e) {
151 $providerArray['outlookCalendar']['calendarId'] = !empty($providerArray['outlookCalendar']['calendarId'])
152 ? $providerArray['outlookCalendar']['calendarId']
153 : null;
154
155 $providerArray['outlookCalendar']['calendarList'] = [];
156
157 $providerRepository->updateErrorColumn($providerId, $e->getMessage());
158 $successfulOutlookConnection = false;
159 }
160 }
161
162 $providerArray['mandatoryServicesIds'] = $providerService->getMandatoryServicesIds($providerId);
163
164 $providerArray['eventList'] = array_map(
165 function ($event) {
166 return [
167 'name' => $event['name'],
168 'id' => $event['id'],
169 'periods' => $event['periods'],
170 'color' => $event['color'],
171 'organizer' => ['id' => $event['organizerId']]
172 ];
173 },
174 $providerArray['eventList']
175 );
176
177 $providerArray = apply_filters('amelia_get_provider_filter', $providerArray);
178
179 do_action('amelia_get_provider', $providerArray);
180
181 $result->setResult(CommandResult::RESULT_SUCCESS);
182 $result->setMessage('Successfully retrieved user.');
183 $result->setData(
184 [
185 Entities::USER => $providerArray,
186 'successfulGoogleConnection' => $successfulGoogleConnection,
187 'successfulOutlookConnection' => $successfulOutlookConnection,
188 'futureAppointmentsServicesIds' => $futureAppointmentsServicesIds,
189 ]
190 );
191
192 return $result;
193 }
194 }
195