Skip to content

Commit

Permalink
fix: send date as sting instead of epoch
Browse files Browse the repository at this point in the history
Signed-off-by: SebastianKrupinski <[email protected]>
  • Loading branch information
SebastianKrupinski committed Dec 10, 2024
1 parent c1b7f8c commit 91d787c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 52 deletions.
19 changes: 8 additions & 11 deletions lib/Controller/BookingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace OCA\Calendar\Controller;

use DateTime;
use DateTimeImmutable;
use DateTimeZone;
use InvalidArgumentException;
use OCA\Calendar\AppInfo\Application;
Expand Down Expand Up @@ -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();

Expand Down
8 changes: 4 additions & 4 deletions src/services/appointmentService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}', {

Check warning on line 15 in src/services/appointmentService.js

View check run for this annotation

Codecov / codecov/patch

src/services/appointmentService.js#L14-L15

Added lines #L14 - L15 were not covered by tests
id: config.id,
start,
date,
timeZone,
})

Expand Down
6 changes: 4 additions & 2 deletions src/views/Appointments/Booking.vue
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,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) {
Expand Down
70 changes: 35 additions & 35 deletions tests/php/unit/Controller/BookingControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

use ChristophWurst\Nextcloud\Testing\TestCase;
use DateTime;
use DateTimeImmutable;
use DateTimeZone;
use Exception;
use InvalidArgumentException;
use OC\URLGenerator;
Expand Down Expand Up @@ -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)
Expand All @@ -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);
Expand All @@ -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())
Expand All @@ -164,21 +160,22 @@ 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();

$apptConfg = new AppointmentConfig();
$apptConfg->setId(1);
$this->time->expects(self::once())
->method('getTime')
->willReturn($now);
->willReturn($currentDate);
$this->apptService->expects(self::once())
->method('findById')
->with(1)
Expand All @@ -187,21 +184,22 @@ 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();

$apptConfg = new AppointmentConfig();
$apptConfg->setId(1);
$this->time->expects(self::once())
->method('getTime')
->willReturn($now);
->willReturn($currentDate);
$this->apptService->expects(self::once())
->method('findById')
->with(1)
Expand All @@ -210,21 +208,22 @@ 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();

$apptConfg = new AppointmentConfig();
$apptConfg->setId(1);
$this->time->expects(self::once())
->method('getTime')
->willReturn($now);
->willReturn($currentDate);
$this->apptService->expects(self::once())
->method('findById')
->with(1)
Expand All @@ -233,21 +232,22 @@ 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();

$apptConfg = new AppointmentConfig();
$apptConfg->setId(1);
$this->time->expects(self::once())
->method('getTime')
->willReturn($now);
->willReturn($currentDate);
$this->apptService->expects(self::once())
->method('findById')
->with(1)
Expand All @@ -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 {
Expand Down

0 comments on commit 91d787c

Please sign in to comment.