AddCustomerCommand.php
7 years ago
AddCustomerCommandHandler.php
1 year ago
GetCustomerCommand.php
7 years ago
GetCustomerCommandHandler.php
1 year ago
GetCustomersCommand.php
7 years ago
GetCustomersCommandHandler.php
1 year ago
UpdateCustomerCommand.php
7 years ago
UpdateCustomerCommandHandler.php
1 year ago
UpdateCustomerStatusCommand.php
1 year ago
UpdateCustomerStatusCommandHandler.php
1 year ago
GetCustomersCommandHandler.php
164 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\User\ProviderApplicationService; |
| 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\Services\Settings\SettingsService; |
| 16 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 17 | use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\CustomerBookingRepository; |
| 18 | use AmeliaBooking\Infrastructure\Repository\User\CustomerRepository; |
| 19 | use Exception; |
| 20 | use Interop\Container\Exception\ContainerException; |
| 21 | use Slim\Exception\ContainerValueNotFoundException; |
| 22 | |
| 23 | /** |
| 24 | * Class GetCustomersCommandHandler |
| 25 | * |
| 26 | * @package AmeliaBooking\Application\Commands\User\Customer |
| 27 | */ |
| 28 | class GetCustomersCommandHandler extends CommandHandler |
| 29 | { |
| 30 | /** |
| 31 | * @param GetCustomersCommand $command |
| 32 | * |
| 33 | * @return CommandResult |
| 34 | * @throws InvalidArgumentException |
| 35 | * @throws ContainerValueNotFoundException |
| 36 | * @throws QueryExecutionException |
| 37 | * @throws ContainerException |
| 38 | * @throws Exception |
| 39 | * @throws AccessDeniedException |
| 40 | */ |
| 41 | public function handle(GetCustomersCommand $command) |
| 42 | { |
| 43 | $result = new CommandResult(); |
| 44 | |
| 45 | /** @var AbstractUser $currentUser */ |
| 46 | $currentUser = $this->container->get('logged.in.user'); |
| 47 | |
| 48 | if (!$command->getPermissionService()->currentUserCanRead(Entities::CUSTOMERS) && |
| 49 | !($currentUser && $currentUser->getType() === AbstractUser::USER_ROLE_PROVIDER) |
| 50 | ) { |
| 51 | if ($command->getToken()) { |
| 52 | /** @var UserApplicationService $userAS */ |
| 53 | $userAS = $this->container->get('application.user.service'); |
| 54 | |
| 55 | try { |
| 56 | $currentUser = $userAS->authorization($command->getToken(), 'provider'); |
| 57 | } catch (AuthorizationException $e) { |
| 58 | $result->setResult(CommandResult::RESULT_ERROR); |
| 59 | $result->setData( |
| 60 | [ |
| 61 | 'reauthorize' => true |
| 62 | ] |
| 63 | ); |
| 64 | |
| 65 | return $result; |
| 66 | } |
| 67 | } else { |
| 68 | throw new AccessDeniedException('You are not allowed to read customers.'); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** @var CustomerRepository $customerRepository */ |
| 73 | $customerRepository = $this->getContainer()->get('domain.users.customers.repository'); |
| 74 | |
| 75 | /** @var SettingsService $settingsService */ |
| 76 | $settingsService = $this->container->get('domain.settings.service'); |
| 77 | |
| 78 | /** @var ProviderApplicationService $providerAS */ |
| 79 | $providerAS = $this->container->get('application.user.provider.service'); |
| 80 | |
| 81 | $params = $command->getField('params'); |
| 82 | |
| 83 | $countParams = []; |
| 84 | |
| 85 | if (!$command->getPermissionService()->currentUserCanReadOthers(Entities::CUSTOMERS)) { |
| 86 | /** @var Collection $providerCustomers */ |
| 87 | $providerCustomers = $providerAS->getAllowedCustomers($currentUser); |
| 88 | |
| 89 | $params['customers'] = array_column($providerCustomers->toArray(), 'id'); |
| 90 | |
| 91 | $countParams['customers'] = $params['customers']; |
| 92 | } |
| 93 | |
| 94 | $itemsPerPage = !empty($params['limit']) ? |
| 95 | $params['limit'] : $settingsService->getSetting('general', 'itemsPerPageBackEnd'); |
| 96 | |
| 97 | $users = $customerRepository->getFiltered( |
| 98 | array_merge($params, ['ignoredBookings' => empty($params['noShow'])]), |
| 99 | $itemsPerPage |
| 100 | ); |
| 101 | |
| 102 | if (!empty($users)) { |
| 103 | $usersWithBookingsStats = $customerRepository->getFiltered( |
| 104 | array_merge($params, ['ignoredBookings' => false, 'customers' => array_keys($users)]), |
| 105 | null |
| 106 | ); |
| 107 | |
| 108 | foreach ($users as $key => $user) { |
| 109 | $users[$key] = $usersWithBookingsStats[$key]; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | $customersNoShowCount = []; |
| 114 | |
| 115 | $noShowTagEnabled = $settingsService->getSetting('roles', 'enableNoShowTag'); |
| 116 | |
| 117 | if ($noShowTagEnabled && $users) { |
| 118 | /** @var CustomerBookingRepository $bookingRepository */ |
| 119 | $bookingRepository = $this->container->get('domain.booking.customerBooking.repository'); |
| 120 | |
| 121 | $usersIds = array_map(function ($user) { return $user['id']; }, $users); |
| 122 | |
| 123 | $customersNoShowCount = $usersIds ? $bookingRepository->countByNoShowStatus($usersIds) : []; |
| 124 | } |
| 125 | |
| 126 | $users = array_values($users); |
| 127 | |
| 128 | foreach ($users as $key => &$user) { |
| 129 | $user['wpUserPhotoUrl'] = $this->container->get('user.avatar')->getAvatar($user['externalId']); |
| 130 | |
| 131 | if ($noShowTagEnabled) { |
| 132 | $user['noShowCount'] = $customersNoShowCount[$key]['count']; |
| 133 | } |
| 134 | |
| 135 | $user = array_map( |
| 136 | function ($v) { |
| 137 | return (null === $v) ? '' : $v; |
| 138 | }, |
| 139 | $user |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | $users = apply_filters('amelia_get_customers_filter', $users); |
| 144 | |
| 145 | do_action('amelia_get_customers', $users); |
| 146 | |
| 147 | $resultData = [ |
| 148 | Entities::USER . 's' => $users, |
| 149 | ]; |
| 150 | |
| 151 | if (empty($params['skipCount'])) { |
| 152 | $resultData['filteredCount'] = (int)$customerRepository->getCount($params); |
| 153 | |
| 154 | $resultData['totalCount'] = (int)$customerRepository->getCount($countParams); |
| 155 | } |
| 156 | |
| 157 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 158 | $result->setMessage('Successfully retrieved users.'); |
| 159 | $result->setData($resultData); |
| 160 | |
| 161 | return $result; |
| 162 | } |
| 163 | } |
| 164 |