PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / trunk
Booking for Appointments and Events Calendar – Amelia vtrunk
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 3 days ago GetProviderCommand.php 7 years ago GetProviderCommandHandler.php 2 months ago GetProvidersCommand.php 1 year ago GetProvidersCommandHandler.php 4 months ago UpdateProviderCommand.php 1 year ago UpdateProviderCommandHandler.php 3 days ago UpdateProviderStatusCommand.php 1 year ago UpdateProviderStatusCommandHandler.php 6 months ago
GetProviderCommandHandler.php
292 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\Bookable\Service\ProviderServiceRepository;
17 use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\AppointmentRepository;
18 use AmeliaBooking\Infrastructure\Repository\User\ProviderRepository;
19 use AmeliaBooking\Infrastructure\Services\Google\AbstractGoogleCalendarService;
20 use AmeliaBooking\Infrastructure\Services\Google\AbstractGoogleCalendarMiddlewareService;
21 use AmeliaBooking\Infrastructure\Services\Outlook\AbstractOutlookCalendarMiddlewareService;
22 use AmeliaBooking\Infrastructure\Services\Outlook\AbstractOutlookCalendarService;
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 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 ProviderServiceRepository $providerServiceRepository */
74 $providerServiceRepository = $this->container->get('domain.bookable.service.providerService.repository');
75 /** @var AbstractGoogleCalendarMiddlewareService $googleCalendarMiddlewareService */
76 $googleCalendarMiddlewareService = $this->container->get(
77 'infrastructure.google.calendar.middleware.service'
78 );
79 /** @var AbstractOutlookCalendarMiddlewareService $outlookCalendarMiddlewareService */
80 $outlookCalendarMiddlewareService = $this->container->get(
81 'infrastructure.outlook.calendar.middleware.service'
82 );
83
84 $companyDaysOff = $settingsService->getCategorySettings('daysOff');
85
86 $companyDayOff = $providerService->checkIfTodayIsCompanyDayOff($companyDaysOff);
87
88 /** @var Provider $provider */
89 $provider = $providerService->getProviderWithServicesAndSchedule($providerId, true);
90
91 $providerService->modifyPeriodsWithSingleLocationAfterFetch($provider->getWeekDayList());
92 $providerService->modifyPeriodsWithSingleLocationAfterFetch($provider->getSpecialDayList());
93
94 $futureAppointmentsServicesIds = $appointmentRepository->getFutureAppointmentsServicesIds(
95 [$provider->getId()->getValue()],
96 DateTimeService::getNowDateTime(),
97 null
98 );
99
100 $providerArray = $providerService->manageProvidersActivity(
101 [$provider->toArray()],
102 $companyDayOff
103 )[0];
104
105 $successfulGoogleConnection = true;
106
107 $successfulOutlookConnection = true;
108
109 $providerArray['googleCalendar']['calendarList'] = [];
110 $providerArray['googleCalendar']['calendarId'] = null;
111
112 $providerArray['outlookCalendar']['calendarList'] = [];
113 $providerArray['outlookCalendar']['calendarId'] = null;
114
115 if ($settingsService->isFeatureEnabled('googleCalendar')) {
116 $googleCalendarAccounts = $providerRepository->getGoogleCalendarAccounts($providerId);
117
118 $providerArray['googleCalendar']['accounts'] = $googleCalendarAccounts;
119
120 $googleCalendarIdFromAccounts = null;
121 foreach ($googleCalendarAccounts as $account) {
122 if (!empty($account['calendarId'])) {
123 $googleCalendarIdFromAccounts = $account['calendarId'];
124 break;
125 }
126 }
127
128 $providerArray['googleCalendar']['insertPendingAppointments'] = $providerArray['googleCalendar']['insertPendingAppointments'] ?? false;
129 $providerArray['googleCalendar']['includeBufferTime'] = $providerArray['googleCalendar']['includeBufferTime'] ?? false;
130
131 $googleCalendarGlobalSettings = $settingsService->getCategorySettings('googleCalendar');
132 $providerArray['googleCalendar']['title'] = $providerArray['googleCalendar']['title'] ??
133 ($googleCalendarGlobalSettings['title'] ?? ['appointment' => '%service_name%', 'event' => '%event_name%']);
134 $providerArray['googleCalendar']['description'] = $providerArray['googleCalendar']['description'] ??
135 ($googleCalendarGlobalSettings['description'] ?? ['appointment' => '', 'event' => '']);
136
137 $blockedCalendars = [];
138 foreach ($googleCalendarAccounts as $account) {
139 if (!empty($account['blockedCalendars'])) {
140 foreach ($account['blockedCalendars'] as $calendarId) {
141 if (!in_array($calendarId, $blockedCalendars)) {
142 $blockedCalendars[] = $calendarId;
143 }
144 }
145 }
146 }
147 $providerArray['googleCalendar']['blockedCalendars'] = $blockedCalendars;
148 try {
149 $googleCalendar = $settingsService->getCategorySettings('googleCalendar');
150 if (!$googleCalendar['accessToken']) {
151 $providerArray['googleCalendar']['calendarList'] = $googleCalService->listCalendarList($provider);
152 $providerArray['googleCalendar']['calendarId'] = $googleCalService->getProviderGoogleCalendarId($provider);
153
154 if (!empty($providerArray['googleCalendar']['accounts'])) {
155 $providerArray['googleCalendar']['accounts'] = $googleCalService->getCalendarListsForAccounts(
156 $providerArray['googleCalendar']['accounts'],
157 $provider
158 );
159 }
160 } else {
161 $providerArray['googleCalendar']['calendarList'] = $googleCalendarMiddlewareService->getCalendarList($providerArray['googleCalendar']);
162 $providerArray['googleCalendar']['calendarId'] = $googleCalendarIdFromAccounts;
163
164 // Fetch calendar lists for all accounts
165 if (!empty($providerArray['googleCalendar']['accounts'])) {
166 $providerArray['googleCalendar']['accounts'] = $googleCalendarMiddlewareService->getCalendarListsForAccounts(
167 $providerArray['googleCalendar']['accounts']
168 );
169 }
170 }
171 } catch (\Exception $e) {
172 $providerArray['googleCalendar']['calendarId'] = !empty($providerArray['googleCalendar']['calendarId'])
173 ? $providerArray['googleCalendar']['calendarId']
174 : null;
175
176 $providerArray['googleCalendar']['calendarList'] = [];
177
178 $providerRepository->updateErrorColumn($providerId, $e->getMessage());
179 $successfulGoogleConnection = false;
180 }
181
182 if ($googleCalendarIdFromAccounts) {
183 $providerArray['googleCalendar']['calendarId'] = $googleCalendarIdFromAccounts;
184 }
185 }
186
187 if ($settingsService->isFeatureEnabled('outlookCalendar')) {
188 $outlookCalendarAccounts = $providerRepository->getOutlookCalendarAccounts($providerId);
189
190 $providerArray['outlookCalendar']['accounts'] = $outlookCalendarAccounts;
191
192 $outlookCalendarIdFromAccounts = null;
193 foreach ($outlookCalendarAccounts as $account) {
194 if (!empty($account['calendarId'])) {
195 $outlookCalendarIdFromAccounts = $account['calendarId'];
196 break;
197 }
198 }
199
200 $providerArray['outlookCalendar']['insertPendingAppointments'] = $providerArray['outlookCalendar']['insertPendingAppointments'] ?? false;
201 $providerArray['outlookCalendar']['includeBufferTime'] = $providerArray['outlookCalendar']['includeBufferTime'] ?? false;
202
203 $outlookCalendarGlobalSettings = $settingsService->getCategorySettings('outlookCalendar');
204 $providerArray['outlookCalendar']['title'] = $providerArray['outlookCalendar']['title'] ??
205 ($outlookCalendarGlobalSettings['title'] ?? ['appointment' => '%service_name%', 'event' => '%event_name%']);
206 $providerArray['outlookCalendar']['description'] = $providerArray['outlookCalendar']['description'] ??
207 ($outlookCalendarGlobalSettings['description'] ?? ['appointment' => '', 'event' => '']);
208
209 $blockedCalendars = [];
210 foreach ($outlookCalendarAccounts as $account) {
211 if (!empty($account['blockedCalendars'])) {
212 foreach ($account['blockedCalendars'] as $calendarId) {
213 if (!in_array($calendarId, $blockedCalendars)) {
214 $blockedCalendars[] = $calendarId;
215 }
216 }
217 }
218 }
219 $providerArray['outlookCalendar']['blockedCalendars'] = $blockedCalendars;
220 try {
221 $outlookCalendar = $settingsService->getCategorySettings('outlookCalendar');
222 if (!$outlookCalendar['accessToken']) {
223 $providerArray['outlookCalendar']['calendarList'] = $outlookCalendarService->listCalendarList($provider);
224 $providerArray['outlookCalendar']['calendarId'] = $outlookCalendarService->getProviderOutlookCalendarId($provider);
225
226 if (!empty($providerArray['outlookCalendar']['accounts'])) {
227 $providerArray['outlookCalendar']['accounts'] = $outlookCalendarService->getCalendarListsForAccounts(
228 $providerArray['outlookCalendar']['accounts'],
229 $provider
230 );
231 }
232 } else {
233 $providerArray['outlookCalendar']['calendarList'] = $outlookCalendarMiddlewareService->getCalendarList($providerArray['outlookCalendar']);
234 $providerArray['outlookCalendar']['calendarId'] = $outlookCalendarIdFromAccounts;
235
236 // Fetch calendar lists for all accounts
237 if (!empty($providerArray['outlookCalendar']['accounts'])) {
238 $providerArray['outlookCalendar']['accounts'] = $outlookCalendarMiddlewareService->getCalendarListsForAccounts(
239 $providerArray['outlookCalendar']['accounts']
240 );
241 }
242 }
243 } catch (\Exception $e) {
244 $providerArray['outlookCalendar']['calendarId'] = !empty($providerArray['outlookCalendar']['calendarId'])
245 ? $providerArray['outlookCalendar']['calendarId']
246 : null;
247
248 $providerArray['outlookCalendar']['calendarList'] = [];
249
250 $providerRepository->updateErrorColumn($providerId, $e->getMessage());
251 $successfulOutlookConnection = false;
252 }
253
254 if ($outlookCalendarIdFromAccounts) {
255 $providerArray['outlookCalendar']['calendarId'] = $outlookCalendarIdFromAccounts;
256 }
257 }
258
259 $providerArray['mandatoryServicesIds'] = $providerServiceRepository->getMandatoryServicesIdsForProvider($providerId);
260
261 $providerArray['eventList'] = array_map(
262 function ($event) {
263 return [
264 'name' => $event['name'],
265 'id' => $event['id'],
266 'periods' => $event['periods'],
267 'color' => $event['color'],
268 'organizer' => ['id' => $event['organizerId']]
269 ];
270 },
271 $providerArray['eventList']
272 );
273
274 $providerArray = apply_filters('amelia_get_provider_filter', $providerArray);
275
276 do_action('amelia_get_provider', $providerArray);
277
278 $result->setResult(CommandResult::RESULT_SUCCESS);
279 $result->setMessage('Successfully retrieved user.');
280 $result->setData(
281 [
282 Entities::USER => $providerArray,
283 'successfulGoogleConnection' => $successfulGoogleConnection,
284 'successfulOutlookConnection' => $successfulOutlookConnection,
285 'futureAppointmentsServicesIds' => $futureAppointmentsServicesIds,
286 ]
287 );
288
289 return $result;
290 }
291 }
292