PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / trunk
Booking for Appointments and Events Calendar – Amelia vtrunk
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 / Customer / GetCustomersCommandHandler.php
ameliabooking / src / Application / Commands / User / Customer Last commit date
AddCustomerCommand.php 1 year ago AddCustomerCommandHandler.php 8 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 8 hours ago UpdateCustomerNoteCommand.php 3 months ago UpdateCustomerNoteCommandHandler.php 3 months ago UpdateCustomerStatusCommand.php 1 year ago UpdateCustomerStatusCommandHandler.php 6 months ago
GetCustomersCommandHandler.php
231 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\CustomField\AbstractCustomFieldApplicationService;
9 use AmeliaBooking\Application\Services\User\UserApplicationService;
10 use AmeliaBooking\Domain\Collection\Collection;
11 use AmeliaBooking\Domain\Common\Exceptions\AuthorizationException;
12 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
13 use AmeliaBooking\Domain\Entity\Entities;
14 use AmeliaBooking\Domain\Entity\User\AbstractUser;
15 use AmeliaBooking\Domain\Factory\User\UserFactory;
16 use AmeliaBooking\Domain\Services\Settings\SettingsService;
17 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
18 use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\CustomerBookingRepository;
19 use AmeliaBooking\Infrastructure\Repository\CustomField\CustomFieldRepository;
20 use AmeliaBooking\Infrastructure\Repository\User\CustomerRepository;
21 use AmeliaBooking\Infrastructure\Repository\User\UserRepository;
22 use Exception;
23 use Slim\Exception\ContainerValueNotFoundException;
24
25 /**
26 * Class GetCustomersCommandHandler
27 *
28 * @package AmeliaBooking\Application\Commands\User\Customer
29 */
30 class GetCustomersCommandHandler extends CommandHandler
31 {
32 /**
33 * @param GetCustomersCommand $command
34 *
35 * @return CommandResult
36 * @throws InvalidArgumentException
37 * @throws ContainerValueNotFoundException
38 * @throws QueryExecutionException
39 * @throws Exception
40 * @throws AccessDeniedException
41 */
42 public function handle(GetCustomersCommand $command)
43 {
44 $result = new CommandResult();
45
46 /** @var AbstractUser $currentUser */
47 $currentUser = $this->container->get('logged.in.user');
48
49 if (
50 !$command->getPermissionService()->currentUserCanRead(Entities::CUSTOMERS) &&
51 !($currentUser && $currentUser->getType() === AbstractUser::USER_ROLE_PROVIDER)
52 ) {
53 if ($command->getToken()) {
54 /** @var UserApplicationService $userAS */
55 $userAS = $this->container->get('application.user.service');
56
57 try {
58 $currentUser = $userAS->authorization($command->getToken(), 'provider');
59 } catch (AuthorizationException $e) {
60 $result->setResult(CommandResult::RESULT_ERROR);
61 $result->setData(
62 [
63 'reauthorize' => true
64 ]
65 );
66
67 return $result;
68 }
69 } else {
70 throw new AccessDeniedException('You are not allowed to read customers.');
71 }
72 }
73
74 /** @var CustomerRepository $customerRepository */
75 $customerRepository = $this->getContainer()->get('domain.users.customers.repository');
76
77 /** @var UserRepository $userRepository */
78 $userRepository = $this->container->get('domain.users.repository');
79
80 /** @var CustomFieldRepository $customFieldRepository */
81 $customFieldRepository = $this->container->get('domain.customField.repository');
82
83 /** @var SettingsService $settingsService */
84 $settingsService = $this->container->get('domain.settings.service');
85
86 $rolesSettings = $settingsService->getCategorySettings('roles');
87
88 $params = $command->getField('params');
89
90 $countParams = [];
91
92 $itemsPerPage = !empty($params['limit']) ? (int)$params['limit'] : 10;
93
94 $allowedCustomerIds = null;
95
96 if (
97 $currentUser !== null &&
98 $currentUser->getType() === Entities::PROVIDER &&
99 empty($rolesSettings['allowReadAllCustomers'])
100 ) {
101 /** @var Collection $providerCustomers */
102 $providerCustomers = $userRepository->getProviderAllowedCustomers(
103 $currentUser->getId()->getValue()
104 );
105
106 $allowedCustomerIds = $providerCustomers->keys();
107
108 $params['customers'] = empty($params['customers'])
109 ? $allowedCustomerIds
110 : array_intersect(
111 array_map('intval', $params['customers']),
112 $allowedCustomerIds
113 );
114
115 $countParams['customers'] = $params['customers'];
116 }
117
118 $users = $customerRepository->getFiltered(
119 array_merge($params, ['ignoredBookings' => empty($params['noShow'])]),
120 $itemsPerPage
121 );
122
123 if (!empty($params['includeCustomers'])) {
124 $includeCustomerIds = $params['includeCustomers'];
125
126 // Apply provider allowlist if restricted
127 if ($allowedCustomerIds !== null) {
128 $includeCustomerIds = array_values(array_intersect(
129 array_map('intval', $includeCustomerIds),
130 $allowedCustomerIds
131 ));
132 }
133
134 if (!empty($includeCustomerIds)) {
135 $additionalCustomers = $customerRepository->getFiltered(
136 [
137 'customers' => $includeCustomerIds,
138 'ignoredBookings' => empty($params['noShow'])
139 ]
140 );
141
142 foreach ($additionalCustomers as $customerId => $customerData) {
143 if (!isset($users[$customerId])) {
144 $users[$customerId] = $customerData;
145 }
146 }
147 }
148 }
149
150 if (!empty($users)) {
151 $usersWithBookingsStats = $customerRepository->getFiltered(
152 array_merge($params, ['ignoredBookings' => false, 'customers' => array_keys($users)]),
153 null
154 );
155
156 foreach ($users as $key => $user) {
157 $users[$key] = $usersWithBookingsStats[$key];
158 }
159 }
160
161 $customersNoShowCount = [];
162
163 $noShowTagEnabled = $settingsService->isFeatureEnabled('noShowTag');
164
165 if ($noShowTagEnabled && $users) {
166 /** @var CustomerBookingRepository $bookingRepository */
167 $bookingRepository = $this->container->get('domain.booking.customerBooking.repository');
168
169 $usersIds = array_map(
170 function ($user) {
171 return $user['id'];
172 },
173 $users
174 );
175
176 $customersNoShowCount = $usersIds ? $bookingRepository->countByNoShowStatus($usersIds) : [];
177
178 $customersNoShowCount = $customersNoShowCount ? array_values($customersNoShowCount) : [];
179 }
180
181 /** @var AbstractCustomFieldApplicationService $customFieldService */
182 $customFieldService = $this->container->get('application.customField.service');
183
184 /** @var Collection $customFieldsCollection */
185 $customFieldsCollection = $customFieldRepository->getAll([], false);
186
187 $users = array_values($users);
188
189 foreach ($users as $key => &$user) {
190 $user['wpUserPhotoUrl'] = $this->container->get('user.avatar')->getAvatar($user['externalId']);
191
192 if ($noShowTagEnabled) {
193 $user['noShowCount'] = $customersNoShowCount[$key]['count'];
194 }
195
196 $customFields = [];
197
198 if (!empty($user['customFields'])) {
199 $user['customFields'] = $customFieldService->reformatCustomField(UserFactory::create($user), $customFields, $customFieldsCollection);
200 }
201
202 $user = array_map(
203 function ($v) {
204 return (null === $v) ? '' : $v;
205 },
206 $user
207 );
208 }
209
210 $users = apply_filters('amelia_get_customers_filter', $users);
211
212 do_action('amelia_get_customers', $users);
213
214 $resultData = [
215 Entities::USER . 's' => $users,
216 ];
217
218 if (empty($params['skipCount'])) {
219 $resultData['filteredCount'] = (int)$customerRepository->getCount($params);
220
221 $resultData['totalCount'] = (int)$customerRepository->getCount($countParams);
222 }
223
224 $result->setResult(CommandResult::RESULT_SUCCESS);
225 $result->setMessage('Successfully retrieved users.');
226 $result->setData($resultData);
227
228 return $result;
229 }
230 }
231