-
Notifications
You must be signed in to change notification settings - Fork 334
Expand file tree
/
Copy pathBookingController.php
More file actions
283 lines (251 loc) Β· 8.79 KB
/
Copy pathBookingController.php
File metadata and controls
283 lines (251 loc) Β· 8.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Calendar\Controller;
use DateTime;
use DateTimeZone;
use InvalidArgumentException;
use OCA\Calendar\AppInfo\Application;
use OCA\Calendar\Exception\ClientException;
use OCA\Calendar\Exception\NoSlotFoundException;
use OCA\Calendar\Exception\ServiceException;
use OCA\Calendar\Http\JsonResponse;
use OCA\Calendar\Service\Appointments\AppointmentConfigService;
use OCA\Calendar\Service\Appointments\BookingService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\AnonRateLimit;
use OCP\AppFramework\Http\Attribute\UserRateLimit;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IConfig;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\Mail\IMailer;
use Psr\Log\LoggerInterface;
class BookingController extends Controller {
/** @var BookingService */
private $bookingService;
/** @var ITimeFactory */
private $timeFactory;
/** @var AppointmentConfigService */
private $appointmentConfigService;
/** @var IInitialState */
private $initialState;
/** @var IURLGenerator */
private $urlGenerator;
/** @var LoggerInterface */
private $logger;
/** @var IMailer */
private $mailer;
private IConfig $systemConfig;
public function __construct(string $appName,
IRequest $request,
ITimeFactory $timeFactory,
IInitialState $initialState,
BookingService $bookingService,
AppointmentConfigService $appointmentConfigService,
IURLGenerator $urlGenerator,
LoggerInterface $logger,
IMailer $mailer,
IConfig $systemConfig) {
parent::__construct($appName, $request);
$this->bookingService = $bookingService;
$this->timeFactory = $timeFactory;
$this->appointmentConfigService = $appointmentConfigService;
$this->initialState = $initialState;
$this->urlGenerator = $urlGenerator;
$this->logger = $logger;
$this->mailer = $mailer;
$this->systemConfig = $systemConfig;
}
/**
* @NoAdminRequired
* @PublicPage
*
* @param string $appointmentConfigToken
* @param int $startTime UNIX time stamp for the start time in UTC
* @param string $timeZone
*
* @return JsonResponse
*/
public function getBookableSlots(
string $appointmentConfigToken,
string $dateSelected,
string $timeZone,
): JsonResponse {
try {
$tz = new DateTimeZone($timeZone);
} catch (\Exception $e) {
$this->logger->error('Timezone invalid', ['exception' => $e]);
return JsonResponse::fail('Invalid timezone', Http::STATUS_UNPROCESSABLE_ENTITY);
}
// Convert selected date to requesters selected timezone adjusted start and end of day in epoch
$startTimeInTz = (new DateTime($dateSelected, $tz))
->getTimestamp();
$endTimeInTz = (new DateTime($dateSelected, $tz))
->modify('+1 day')
->getTimestamp();
if ($startTimeInTz > $endTimeInTz) {
$this->logger->warning('Invalid time range - end time ' . $endTimeInTz . ' before start time ' . $startTimeInTz);
return JsonResponse::fail('Invalid time range', Http::STATUS_UNPROCESSABLE_ENTITY);
}
// rate limit this to only allow ranges between 0 and 7 days
if (ceil(($endTimeInTz - $startTimeInTz) / 86400) > 7) {
$this->logger->warning('Date range too large for start ' . $startTimeInTz . ' end ' . $endTimeInTz);
return JsonResponse::fail('Date Range too large.', Http::STATUS_UNPROCESSABLE_ENTITY);
}
$now = $this->timeFactory->getTime();
if ($now > $endTimeInTz) {
$this->logger->warning('Slot time must be in the future - now ' . $now . ' end ' . $endTimeInTz);
return JsonResponse::fail('Slot time range must be in the future', Http::STATUS_UNPROCESSABLE_ENTITY);
}
try {
$config = $this->appointmentConfigService->findByToken($appointmentConfigToken);
} catch (ServiceException $e) {
$this->logger->error('No appointment config found for ' . $appointmentConfigToken, ['exception' => $e]);
return JsonResponse::fail(null, Http::STATUS_NOT_FOUND);
}
return JsonResponse::success(
$this->bookingService->getAvailableSlots($config, $startTimeInTz, $endTimeInTz)
);
}
/**
* @NoAdminRequired
* @PublicPage
*
* @param string $appointmentConfigToken
* @param int $start
* @param int $end
* @param string $displayName
* @param string $email
* @param string $description
* @param string $timeZone
* @return JsonResponse
*
* @AnonRateThrottle(limit=10, period=1200)
* @UserRateThrottle(limit=10, period=300)
*/
#[AnonRateLimit(limit: 10, period: 1200)]
#[UserRateLimit(limit: 10, period: 300)]
public function bookSlot(string $appointmentConfigToken,
int $start,
int $end,
string $displayName,
string $email,
string $description,
string $timeZone): JsonResponse {
if (!$this->mailer->validateMailAddress($email)) {
return JsonResponse::fail('Invalid email address', Http::STATUS_UNPROCESSABLE_ENTITY);
}
if ($start > $end) {
return JsonResponse::fail('Invalid time range', Http::STATUS_UNPROCESSABLE_ENTITY);
}
try {
$config = $this->appointmentConfigService->findByToken($appointmentConfigToken);
} catch (ServiceException $e) {
$this->logger->error('No appointment config found for token ' . $appointmentConfigToken, ['exception' => $e]);
return JsonResponse::fail(null, Http::STATUS_NOT_FOUND);
}
try {
$booking = $this->bookingService->book($config, $start, $end, $timeZone, $displayName, $email, $description);
} catch (NoSlotFoundException $e) {
$this->logger->warning('No slot available for start: ' . $start . ', end: ' . $end . ', config token: ' . $appointmentConfigToken, ['exception' => $e]);
return JsonResponse::fail(null, Http::STATUS_NOT_FOUND);
} catch (InvalidArgumentException $e) {
$this->logger->warning($e->getMessage(), ['exception' => $e]);
return JsonResponse::fail(null, Http::STATUS_UNPROCESSABLE_ENTITY);
} catch (ServiceException $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
if ($this->systemConfig->getSystemValue('debug', false)) {
return JsonResponse::errorFromThrowable($e, $e->getHttpCode() ?? Http::STATUS_INTERNAL_SERVER_ERROR,
['debug' => true,]
);
}
return JsonResponse::error(
'Server error',
$e->getHttpCode() ?? Http::STATUS_INTERNAL_SERVER_ERROR
);
}
return JsonResponse::success();
}
/**
* @PublicPage
* @NoCSRFRequired
*
* @param string $token
* @return TemplateResponse
*/
public function showConfirmBooking(string $token): TemplateResponse {
try {
$booking = $this->bookingService->findByToken($token);
} catch (ClientException $e) {
$this->logger->warning($e->getMessage(), ['exception' => $e]);
return new TemplateResponse(
Application::APP_ID,
'appointments/404-booking',
[],
TemplateResponse::RENDER_AS_GUEST
);
}
try {
$config = $this->appointmentConfigService->findById($booking->getApptConfigId());
} catch (ClientException $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
return new TemplateResponse(
Application::APP_ID,
'appointments/404-booking',
[],
TemplateResponse::RENDER_AS_GUEST
);
}
$link = $this->urlGenerator->linkToRouteAbsolute('calendar.appointment.show', ['token' => $config->getToken()]);
$this->initialState->provideInitialState('appointment-link', $link);
$this->initialState->provideInitialState('booking', $booking);
$this->initialState->provideInitialState('booking-token', $token);
return new TemplateResponse(
Application::APP_ID,
'appointments/confirmation',
[],
TemplateResponse::RENDER_AS_GUEST
);
}
/**
* @PublicPage
* @NoCSRFRequired
*
* @param string $token
* @return JsonResponse
*/
public function confirmBooking(string $token): JsonResponse {
try {
$booking = $this->bookingService->findByToken($token);
} catch (ClientException $e) {
$this->logger->warning($e->getMessage(), ['exception' => $e]);
return JsonResponse::fail(null, Http::STATUS_NOT_FOUND);
}
if ($booking->isConfirmed()) {
return JsonResponse::success(['confirmed' => true]);
}
try {
$config = $this->appointmentConfigService->findById($booking->getApptConfigId());
} catch (ClientException $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
return JsonResponse::fail(null, Http::STATUS_NOT_FOUND);
}
try {
$booking = $this->bookingService->confirmBooking($booking, $config);
} catch (NoSlotFoundException $e) {
$this->logger->warning($e->getMessage(), ['exception' => $e]);
return JsonResponse::fail('slot_unavailable', Http::STATUS_CONFLICT);
} catch (ClientException $e) {
$this->logger->warning($e->getMessage(), ['exception' => $e]);
return JsonResponse::fail(null, Http::STATUS_UNPROCESSABLE_ENTITY);
}
return JsonResponse::success(['confirmed' => $booking->isConfirmed()]);
}
}