PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.0.2
Booking for Appointments and Events Calendar – Amelia v2.0.2
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 6 months ago GetCustomerCommand.php 1 year ago GetCustomerCommandHandler.php 6 months ago GetCustomersCommand.php 1 year ago GetCustomersCommandHandler.php 6 months ago UpdateCustomerCommand.php 1 year ago UpdateCustomerCommandHandler.php 5 months ago UpdateCustomerStatusCommand.php 1 year ago UpdateCustomerStatusCommandHandler.php 6 months ago
GetCustomersCommandHandler.php
205 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\ProviderApplicationService;
10 use AmeliaBooking\Application\Services\User\UserApplicationService;
11 use AmeliaBooking\Domain\Collection\Collection;
12 use AmeliaBooking\Domain\Common\Exceptions\AuthorizationException;
13 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
14 use AmeliaBooking\Domain\Entity\Entities;
15 use AmeliaBooking\Domain\Entity\User\AbstractUser;
16 use AmeliaBooking\Domain\Factory\User\UserFactory;
17 use AmeliaBooking\Domain\Services\Settings\SettingsService;
18 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
19 use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\CustomerBookingRepository;
20 use AmeliaBooking\Infrastructure\Repository\CustomField\CustomFieldRepository;
21 use AmeliaBooking\Infrastructure\Repository\User\CustomerRepository;
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 CustomFieldRepository $customFieldRepository */
78 $customFieldRepository = $this->container->get('domain.customField.repository');
79
80 /** @var SettingsService $settingsService */
81 $settingsService = $this->container->get('domain.settings.service');
82
83 /** @var ProviderApplicationService $providerAS */
84 $providerAS = $this->container->get('application.user.provider.service');
85
86 $params = $command->getField('params');
87
88 $countParams = [];
89
90 if (
91 empty($params['customers']) &&
92 !$command->getPermissionService()->currentUserCanReadOthers(Entities::CUSTOMERS)
93 ) {
94 /** @var Collection $providerCustomers */
95 $providerCustomers = $providerAS->getAllowedCustomers($currentUser);
96
97 $params['customers'] = array_column($providerCustomers->toArray(), 'id');
98
99 $countParams['customers'] = $params['customers'];
100 }
101
102 $itemsPerPage = !empty($params['limit']) ? $params['limit'] : 10;
103
104 $users = $customerRepository->getFiltered(
105 array_merge($params, ['ignoredBookings' => empty($params['noShow'])]),
106 $itemsPerPage
107 );
108
109 if (!empty($params['includeCustomers'])) {
110 $additionalCustomers = $customerRepository->getFiltered(
111 [
112 'customers' => $params['includeCustomers'],
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 if (!empty($users)) {
125 $usersWithBookingsStats = $customerRepository->getFiltered(
126 array_merge($params, ['ignoredBookings' => false, 'customers' => array_keys($users)]),
127 null
128 );
129
130 foreach ($users as $key => $user) {
131 $users[$key] = $usersWithBookingsStats[$key];
132 }
133 }
134
135 $customersNoShowCount = [];
136
137 $noShowTagEnabled = $settingsService->isFeatureEnabled('noShowTag');
138
139 if ($noShowTagEnabled && $users) {
140 /** @var CustomerBookingRepository $bookingRepository */
141 $bookingRepository = $this->container->get('domain.booking.customerBooking.repository');
142
143 $usersIds = array_map(
144 function ($user) {
145 return $user['id'];
146 },
147 $users
148 );
149
150 $customersNoShowCount = $usersIds ? $bookingRepository->countByNoShowStatus($usersIds) : [];
151
152 $customersNoShowCount = $customersNoShowCount ? array_values($customersNoShowCount) : [];
153 }
154
155 /** @var AbstractCustomFieldApplicationService $customFieldService */
156 $customFieldService = $this->container->get('application.customField.service');
157
158 /** @var Collection $customFieldsCollection */
159 $customFieldsCollection = $customFieldRepository->getAll([], false);
160
161 $users = array_values($users);
162
163 foreach ($users as $key => &$user) {
164 $user['wpUserPhotoUrl'] = $this->container->get('user.avatar')->getAvatar($user['externalId']);
165
166 if ($noShowTagEnabled) {
167 $user['noShowCount'] = $customersNoShowCount[$key]['count'];
168 }
169
170 $customFields = [];
171
172 if (!empty($user['customFields'])) {
173 $user['customFields'] = $customFieldService->reformatCustomField(UserFactory::create($user), $customFields, $customFieldsCollection);
174 }
175
176 $user = array_map(
177 function ($v) {
178 return (null === $v) ? '' : $v;
179 },
180 $user
181 );
182 }
183
184 $users = apply_filters('amelia_get_customers_filter', $users);
185
186 do_action('amelia_get_customers', $users);
187
188 $resultData = [
189 Entities::USER . 's' => $users,
190 ];
191
192 if (empty($params['skipCount'])) {
193 $resultData['filteredCount'] = (int)$customerRepository->getCount($params);
194
195 $resultData['totalCount'] = (int)$customerRepository->getCount($countParams);
196 }
197
198 $result->setResult(CommandResult::RESULT_SUCCESS);
199 $result->setMessage('Successfully retrieved users.');
200 $result->setData($resultData);
201
202 return $result;
203 }
204 }
205