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