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 / Calendar / ManageCalendarBlockTimeCommandHandler.php
ameliabooking / src / Application / Commands / Calendar Last commit date
DeleteBlockTimeCommand.php 4 months ago DeleteBlockTimeCommandHandler.php 2 months ago GetBlockTimeCommand.php 4 months ago GetBlockTimeCommandHandler.php 2 months ago GetCalendarEventsCommand.php 7 months ago GetCalendarEventsCommandHandler.php 4 weeks ago GetCalendarSlotAvailabilityCommand.php 7 months ago GetCalendarSlotAvailabilityHandler.php 1 week ago GetCalendarSlotEntitiesCommand.php 7 months ago GetCalendarSlotEntitiesCommandHandler.php 3 months ago GetCalendarSlotsCommand.php 7 months ago GetCalendarSlotsCommandHandler.php 2 months ago ManageCalendarBlockTimeCommand.php 4 months ago ManageCalendarBlockTimeCommandHandler.php 2 months 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