DeleteBlockTimeCommand.php
3 months ago
DeleteBlockTimeCommandHandler.php
1 month ago
GetBlockTimeCommand.php
3 months ago
GetBlockTimeCommandHandler.php
1 month ago
GetCalendarEventsCommand.php
6 months ago
GetCalendarEventsCommandHandler.php
2 weeks ago
GetCalendarSlotAvailabilityCommand.php
6 months ago
GetCalendarSlotAvailabilityHandler.php
4 months ago
GetCalendarSlotEntitiesCommand.php
6 months ago
GetCalendarSlotEntitiesCommandHandler.php
2 months ago
GetCalendarSlotsCommand.php
6 months ago
GetCalendarSlotsCommandHandler.php
1 month ago
ManageCalendarBlockTimeCommand.php
3 months ago
ManageCalendarBlockTimeCommandHandler.php
1 month ago
ManageCalendarBlockTimeCommandHandler.php
119 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @copyright © Melograno Ventures. All rights reserved. |
| 5 | * @licence See LICENCE.md for license details. |
| 6 | */ |
| 7 | |
| 8 | namespace AmeliaBooking\Application\Commands\Calendar; |
| 9 | |
| 10 | use AmeliaBooking\Application\Commands\CommandHandler; |
| 11 | use AmeliaBooking\Application\Commands\CommandResult; |
| 12 | use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException; |
| 13 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 14 | use AmeliaBooking\Domain\Entity\Entities; |
| 15 | use AmeliaBooking\Domain\Entity\Schedule\BlockTime; |
| 16 | use AmeliaBooking\Domain\Entity\User\AbstractUser; |
| 17 | use AmeliaBooking\Domain\Factory\Schedule\BlockTimeFactory; |
| 18 | use AmeliaBooking\Domain\Services\DateTime\DateTimeService; |
| 19 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 20 | use AmeliaBooking\Infrastructure\Repository\Schedule\DayOffRepository; |
| 21 | use AmeliaVendor\Psr\Container\ContainerExceptionInterface; |
| 22 | use Exception; |
| 23 | |
| 24 | class ManageCalendarBlockTimeCommandHandler extends CommandHandler |
| 25 | { |
| 26 | /** |
| 27 | * @var array |
| 28 | */ |
| 29 | public $mandatoryFields = [ |
| 30 | 'name', |
| 31 | 'startDateTime', |
| 32 | 'endDateTime', |
| 33 | ]; |
| 34 | |
| 35 | /** |
| 36 | * @param ManageCalendarBlockTimeCommand $command |
| 37 | * @return CommandResult |
| 38 | * @throws AccessDeniedException |
| 39 | * @throws ContainerExceptionInterface |
| 40 | * @throws InvalidArgumentException |
| 41 | * @throws QueryExecutionException |
| 42 | */ |
| 43 | public function handle(ManageCalendarBlockTimeCommand $command): CommandResult |
| 44 | { |
| 45 | if (!$command->getPermissionService()->currentUserCanWrite(Entities::EMPLOYEES)) { |
| 46 | throw new AccessDeniedException('You are not allowed to delete block time'); |
| 47 | } |
| 48 | |
| 49 | $this->checkMandatoryFields($command); |
| 50 | |
| 51 | $result = new CommandResult(); |
| 52 | $fields = $command->getFields(); |
| 53 | |
| 54 | /** @var DayOffRepository $dayOffRepository */ |
| 55 | $dayOffRepository = $this->container->get('domain.schedule.dayOff.repository'); |
| 56 | |
| 57 | $blockTimeId = !empty($fields['id']) ? $fields['id'] : null; |
| 58 | $isUpdate = $blockTimeId !== null; |
| 59 | $employeeIds = !empty($fields['employeeIds']) ? $fields['employeeIds'] : [null]; |
| 60 | |
| 61 | /** @var AbstractUser $currentUser */ |
| 62 | $currentUser = $this->container->get('logged.in.user'); |
| 63 | |
| 64 | if ($currentUser && $currentUser->getType() === Entities::PROVIDER) { |
| 65 | $providerId = $currentUser->getId()->getValue(); |
| 66 | |
| 67 | if ($isUpdate) { |
| 68 | $blockTime = $dayOffRepository->getBlockTimeById($blockTimeId); |
| 69 | if ($blockTime->getUserId() === null || (int)$blockTime->getUserId()->getValue() !== (int)$providerId) { |
| 70 | throw new AccessDeniedException('You are not allowed to manage this block time.'); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | foreach ($employeeIds as $employeeId) { |
| 75 | if ($employeeId === null || (int)$employeeId !== (int)$providerId) { |
| 76 | throw new AccessDeniedException('You are not allowed to manage this block time.'); |
| 77 | } |
| 78 | } |
| 79 | $employeeIds = [$providerId]; |
| 80 | } |
| 81 | |
| 82 | $dayOffRepository->beginTransaction(); |
| 83 | |
| 84 | try { |
| 85 | foreach ($employeeIds as $employeeId) { |
| 86 | $blockTime = $this->createBlockTime($fields, $blockTimeId, $employeeId); |
| 87 | |
| 88 | if ($isUpdate) { |
| 89 | $dayOffRepository->update($blockTime, $blockTimeId); |
| 90 | } else { |
| 91 | $dayOffRepository->add($blockTime, $employeeId); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | $dayOffRepository->commit(); |
| 96 | } catch (Exception $e) { |
| 97 | $dayOffRepository->rollback(); |
| 98 | throw $e; |
| 99 | } |
| 100 | |
| 101 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 102 | $result->setMessage('Successfully blocked.'); |
| 103 | $result->setData([]); |
| 104 | |
| 105 | return $result; |
| 106 | } |
| 107 | |
| 108 | private function createBlockTime(array $fields, ?int $blockTimeId, ?int $employeeId): BlockTime |
| 109 | { |
| 110 | return BlockTimeFactory::create([ |
| 111 | 'id' => $blockTimeId, |
| 112 | 'name' => $fields['name'], |
| 113 | 'userId' => $employeeId, |
| 114 | 'startDate' => DateTimeService::getCustomDateTimeObjectInUtc($fields['startDateTime'])->format('Y-m-d H:i:s'), |
| 115 | 'endDate' => DateTimeService::getCustomDateTimeObjectInUtc($fields['endDateTime'])->format('Y-m-d H:i:s'), |
| 116 | ]); |
| 117 | } |
| 118 | } |
| 119 |