diff --git a/lib/Controller/BookingController.php b/lib/Controller/BookingController.php index b23a4d405b..4b955fb8c8 100644 --- a/lib/Controller/BookingController.php +++ b/lib/Controller/BookingController.php @@ -8,7 +8,6 @@ namespace OCA\Calendar\Controller; use DateTime; -use DateTimeImmutable; use DateTimeZone; use InvalidArgumentException; use OCA\Calendar\AppInfo\Application; @@ -87,23 +86,21 @@ public function __construct(string $appName, * * @return JsonResponse */ - public function getBookableSlots(int $appointmentConfigId, - int $startTime, - string $timeZone): JsonResponse { + public function getBookableSlots( + int $appointmentConfigId, + string $dateSelected, + string $timeZone, + ): JsonResponse { try { $tz = new DateTimeZone($timeZone); } catch (Exception $e) { $this->logger->error('Timezone invalid', ['exception' => $e]); return JsonResponse::fail('Invalid time zone', Http::STATUS_UNPROCESSABLE_ENTITY); } - // UI sends epoch start of day adjusted for system users calendar - // E.g "Mon, 18 Nov 2024 05:00:00 +0000" (America/Toronto) - $startDate = (new DateTimeImmutable("@$startTime")); - // Convert start date to requesters selected timezone adjusted start and end of day in epoch - // E.g "Mon, 18 Nov 2024 06:00:00 +0000" (America/Mexico_City) - $startTimeInTz = (new DateTime($startDate->format('Y-m-d'), $tz)) + // Convert selected date to requesters selected timezone adjusted start and end of day in epoch + $startTimeInTz = (new DateTime($dateSelected, $tz)) ->getTimestamp(); - $endTimeInTz = (new DateTime($startDate->format('Y-m-d'), $tz)) + $endTimeInTz = (new DateTime($dateSelected, $tz)) ->modify('+1 day') ->getTimestamp(); diff --git a/src/services/appointmentService.js b/src/services/appointmentService.js index de26d085cf..4ece5d72d5 100644 --- a/src/services/appointmentService.js +++ b/src/services/appointmentService.js @@ -8,13 +8,13 @@ import { generateUrl } from '@nextcloud/router' /** * @param config {object} the appointment config object - * @param start {Number} interval start time as unix timestamp + * @param date {string} selected availability date in yyyy-m-d format * @param timeZone {String} target time zone for the time stamps */ -export async function findSlots(config, start, timeZone) { - const url = generateUrl('/apps/calendar/appointment/{id}/slots?startTime={start}&timeZone={timeZone}', { +export async function findSlots(config, date, timeZone) { + const url = generateUrl('/apps/calendar/appointment/{id}/slots?dateSelected={date}&timeZone={timeZone}', { id: config.id, - start, + date, timeZone, }) diff --git a/src/views/Appointments/Booking.vue b/src/views/Appointments/Booking.vue index 2b19e1de72..84e75fa463 100644 --- a/src/views/Appointments/Booking.vue +++ b/src/views/Appointments/Booking.vue @@ -188,12 +188,14 @@ export default { this.slots = [] this.loadingSlots = true - const startOfDay = new Date(this.selectedDate.getTime()) + const selectedDay = this.selectedDate.getFullYear().toString() + '-' + + (this.selectedDate.getMonth() + 1).toString() + '-' + + this.selectedDate.getDate().toString() try { this.slots = await findSlots( this.config, - Math.round(startOfDay.getTime() / 1000), + selectedDay, this.timeZone, ) } catch (e) { diff --git a/tests/php/unit/Controller/BookingControllerTest.php b/tests/php/unit/Controller/BookingControllerTest.php index aa5f0f16ed..d8abac401f 100644 --- a/tests/php/unit/Controller/BookingControllerTest.php +++ b/tests/php/unit/Controller/BookingControllerTest.php @@ -9,8 +9,6 @@ use ChristophWurst\Nextcloud\Testing\TestCase; use DateTime; -use DateTimeImmutable; -use DateTimeZone; use Exception; use InvalidArgumentException; use OC\URLGenerator; @@ -107,20 +105,18 @@ protected function setUp():void { } public function testGetBookableSlots(): void { - $start = time(); - $tz = new DateTimeZone('Europe/Berlin'); - $startDate = (new DateTimeImmutable("@$start")); - $sDT = (new DateTime($startDate->format('Y-m-d'), $tz)) - ->getTimestamp(); - $eDT = (new DateTime($startDate->format('Y-m-d'), $tz)) - ->modify('+1 day') - ->getTimestamp(); + $currentDate = (new DateTime('2024-6-30'))->getTimestamp(); + $selectedDate = '2024-7-1'; + $selectedTz = 'Europe/Berlin'; + //selected date start and end epoch in selected time zone + $sDT = (new DateTime('2024-6-30 22:00:00'))->getTimestamp(); + $eDT = (new DateTime('2024-7-1 22:00:00'))->getTimestamp(); $apptConfg = new AppointmentConfig(); $apptConfg->setId(1); $this->time->expects(self::once()) ->method('getTime') - ->willReturn($start); + ->willReturn($currentDate); $this->apptService->expects(self::once()) ->method('findById') ->with(1) @@ -129,17 +125,17 @@ public function testGetBookableSlots(): void { ->method('getAvailableSlots') ->with($apptConfg, $sDT, $eDT); - $this->controller->getBookableSlots($apptConfg->getId(), $start, 'Europe/Berlin'); + $this->controller->getBookableSlots($apptConfg->getId(), $selectedDate, $selectedTz); } public function testGetBookableSlotsDatesInPast(): void { - $start = time(); - $fakeFutureTimestamp = time() + (100 * 24 * 60 * 60); + $currentDate = (new DateTime('2024-7-2'))->getTimestamp(); + $selectedDate = '2024-7-1'; $apptConfg = new AppointmentConfig(); $apptConfg->setId(1); $this->time->expects(self::once()) ->method('getTime') - ->willReturn($fakeFutureTimestamp); + ->willReturn($currentDate); $this->apptService->expects(self::never()) ->method('findById') ->with(1); @@ -148,11 +144,11 @@ public function testGetBookableSlotsDatesInPast(): void { $this->logger->expects(self::once()) ->method('warning'); - $this->controller->getBookableSlots($apptConfg->getId(), $start, 'Europe/Berlin'); + $this->controller->getBookableSlots($apptConfg->getId(), $selectedDate, 'Europe/Berlin'); } public function testGetBookableSlotsInvalidTimezone(): void { - $start = time(); + $selectedDate = '2024-7-1'; $apptConfg = new AppointmentConfig(); $apptConfg->setId(1); $this->time->expects(self::never()) @@ -164,13 +160,14 @@ public function testGetBookableSlotsInvalidTimezone(): void { ->method('getAvailableSlots'); $this->expectException(Exception::class); - $this->controller->getBookableSlots($apptConfg->getId(), $start, 'Hook/Neverland'); + $this->controller->getBookableSlots($apptConfg->getId(), $selectedDate, 'Hook/Neverland'); } public function testGetBookableSlotsTimezoneIdentical(): void { - $now = (new DateTime('2024-6-30 8:00:00'))->getTimestamp(); - $start = (new DateTime('2024-7-1 04:00:00'))->getTimestamp(); // Start date with America/Toronto offset - $timezone = 'America/Toronto'; + $currentDate = (new DateTime('2024-6-30'))->getTimestamp(); + $selectedDate = '2024-7-1'; + $selectedTz = 'America/Toronto'; + //selected date start and end epoch in selected time zone $sDT = (new DateTime('2024-7-1 04:00:00'))->getTimestamp(); $eDT = (new DateTime('2024-7-2 04:00:00'))->getTimestamp(); @@ -178,7 +175,7 @@ public function testGetBookableSlotsTimezoneIdentical(): void { $apptConfg->setId(1); $this->time->expects(self::once()) ->method('getTime') - ->willReturn($now); + ->willReturn($currentDate); $this->apptService->expects(self::once()) ->method('findById') ->with(1) @@ -187,13 +184,14 @@ public function testGetBookableSlotsTimezoneIdentical(): void { ->method('getAvailableSlots') ->with($apptConfg, $sDT, $eDT); - $this->controller->getBookableSlots($apptConfg->getId(), $start, $timezone); + $this->controller->getBookableSlots($apptConfg->getId(), $selectedDate, $selectedTz); } public function testGetBookableSlotsTimezoneMinus10(): void { - $now = (new DateTime('2024-6-30 8:00:00'))->getTimestamp(); - $start = (new DateTime('2024-7-1 4:00:00'))->getTimestamp(); // Start date with America/Toronto offset + $currentDate = (new DateTime('2024-6-30'))->getTimestamp(); + $selectedDate = '2024-7-1'; $timezone = 'Pacific/Pago_Pago'; + //selected date start and end epoch in selected time zone $sDT = (new DateTime('2024-7-1 11:00:00'))->getTimestamp(); $eDT = (new DateTime('2024-7-2 11:00:00'))->getTimestamp(); @@ -201,7 +199,7 @@ public function testGetBookableSlotsTimezoneMinus10(): void { $apptConfg->setId(1); $this->time->expects(self::once()) ->method('getTime') - ->willReturn($now); + ->willReturn($currentDate); $this->apptService->expects(self::once()) ->method('findById') ->with(1) @@ -210,13 +208,14 @@ public function testGetBookableSlotsTimezoneMinus10(): void { ->method('getAvailableSlots') ->with($apptConfg, $sDT, $eDT); - $this->controller->getBookableSlots($apptConfg->getId(), $start, $timezone); + $this->controller->getBookableSlots($apptConfg->getId(), $selectedDate, $timezone); } public function testGetBookableSlotsTimezonePlus10(): void { - $now = (new DateTime('2024-6-30 8:00:00'))->getTimestamp(); - $start = (new DateTime('2024-7-1 4:00:00'))->getTimestamp(); // Start date with America/Toronto offset + $currentDate = (new DateTime('2024-6-30'))->getTimestamp(); + $selectedDate = '2024-7-1'; $timezone = 'Australia/Sydney'; + //selected date start and end epoch in selected time zone $sDT = (new DateTime('2024-6-30 14:00:00'))->getTimestamp(); $eDT = (new DateTime('2024-7-1 14:00:00'))->getTimestamp(); @@ -224,7 +223,7 @@ public function testGetBookableSlotsTimezonePlus10(): void { $apptConfg->setId(1); $this->time->expects(self::once()) ->method('getTime') - ->willReturn($now); + ->willReturn($currentDate); $this->apptService->expects(self::once()) ->method('findById') ->with(1) @@ -233,13 +232,14 @@ public function testGetBookableSlotsTimezonePlus10(): void { ->method('getAvailableSlots') ->with($apptConfg, $sDT, $eDT); - $this->controller->getBookableSlots($apptConfg->getId(), $start, $timezone); + $this->controller->getBookableSlots($apptConfg->getId(), $selectedDate, $timezone); } public function testGetBookableSlotsTimezonePlus14(): void { - $now = (new DateTime('2024-6-30 8:00:00'))->getTimestamp(); - $start = (new DateTime('2024-7-1 4:00:00'))->getTimestamp(); // Start date with America/Toronto offset + $currentDate = (new DateTime('2024-6-30'))->getTimestamp(); + $selectedDate = '2024-7-1'; $timezone = 'Pacific/Kiritimati'; + //selected date start and end epoch in selected time zone $sDT = (new DateTime('2024-6-30 10:00:00'))->getTimestamp(); $eDT = (new DateTime('2024-7-1 10:00:00'))->getTimestamp(); @@ -247,7 +247,7 @@ public function testGetBookableSlotsTimezonePlus14(): void { $apptConfg->setId(1); $this->time->expects(self::once()) ->method('getTime') - ->willReturn($now); + ->willReturn($currentDate); $this->apptService->expects(self::once()) ->method('findById') ->with(1) @@ -256,7 +256,7 @@ public function testGetBookableSlotsTimezonePlus14(): void { ->method('getAvailableSlots') ->with($apptConfg, $sDT, $eDT); - $this->controller->getBookableSlots($apptConfg->getId(), $start, $timezone); + $this->controller->getBookableSlots($apptConfg->getId(), $selectedDate, $timezone); } public function testBook(): void {