Skip to content

Commit

Permalink
Fixing luxor update errors
Browse files Browse the repository at this point in the history
  • Loading branch information
villepynttari committed Jun 17, 2024
1 parent d746d0f commit d46f49d
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 14 deletions.
6 changes: 3 additions & 3 deletions backend/src/authMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import express from 'express';
import unless from 'express-unless';
import unless, { Params } from 'express-unless';

Check failure on line 2 in backend/src/authMiddleware.ts

View workflow job for this annotation

GitHub Actions / Build backend

'Params' is defined but never used
import { getOAuthClient } from './utils/oAuthClient';
import * as responses from './utils/responses';
import { readToken, updateToken } from './controllers/auth/token';
Expand Down Expand Up @@ -60,7 +60,7 @@ export const parseToken = () => {
}
};

middleware.unless = unless;
middleware.unless = unless.unless;

return middleware;
};
Expand Down Expand Up @@ -114,7 +114,7 @@ export const validateAccessToken = () => {
}
};

middleware.unless = unless;
middleware.unless = unless.unless;

return middleware;
};
6 changes: 5 additions & 1 deletion backend/src/controllers/booking/currentBookingsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ export const addNextCalendarEventMiddleware = () => {
const endDt = DateTime.fromISO(
req.query.until as string
).toUTC();
end = endDt.toISO();
end = endDt.toISO() || '';

if (end === '') {
throw new Error('End date not found');
}

if (endDt <= startDt) {
return responses.badRequest(req, res);
Expand Down
10 changes: 6 additions & 4 deletions backend/src/controllers/booking/makeBookingController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as calendar from '../googleAPI/calendarAPI';
import * as responses from '../../utils/responses';
import { OAuth2Client } from 'google-auth-library';
import _ from 'lodash';
import { getISOTime } from '../../utils/timeUtils';

/**
* Add res.locals.roomAccepted boolean that tells if the room has accepted the event
Expand Down Expand Up @@ -85,8 +86,8 @@ export const checkRoomIsFree = () => {
await calendar.freeBusyQuery(
client,
[{ id: roomId }],
startTime.toISO(),
endTime.toISO()
getISOTime(startTime),
getISOTime(endTime)
)
)[roomId];

Expand Down Expand Up @@ -128,13 +129,14 @@ export const makeBooking = () => {
.plus({ minutes: res.locals.duration })
.toUTC();
const client: OAuth2Client = res.locals.oAuthClient;

const response = await calendar.createEvent(
client,
res.locals.roomId,
res.locals.email,
res.locals.title,
startTime.toISO(),
endTime.toISO()
getISOTime(startTime),
getISOTime(endTime)
);

if (!response.id) {
Expand Down
3 changes: 2 additions & 1 deletion backend/src/controllers/booking/notifyBookingController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
removeScheduleDataArray
} from '../userController';
import { DateTime } from 'luxon';
import { getISOTime } from '../../utils/timeUtils';

const notificationOptions = {
vapidDetails: {
Expand Down Expand Up @@ -124,7 +125,7 @@ export const scheduleNotification = () => {
const endTime = DateTime.fromISO(
eventData.end?.dateTime as string
).toUTC();
const endTimeISO = endTime.toISO();
const endTimeISO = getISOTime(endTime);
const scheduleData: ScheduleData = {
endTime: endTimeISO,
roomId: roomId
Expand Down
14 changes: 10 additions & 4 deletions backend/src/controllers/booking/updateBookingController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { OAuth2Client } from 'google-auth-library';
import * as calendar from '../googleAPI/calendarAPI';
import * as responses from '../../utils/responses';
import * as schema from '../../utils/googleSchema';
import { getISOTime } from '../../utils/timeUtils';

/**
* Add time to current booking
Expand All @@ -26,10 +27,11 @@ export const addTimeToBooking = () => {
}

// New end time
const endTime = DateTime.fromISO(eventData.end?.dateTime as string)
.plus({ minutes: timeToAdd })
.toUTC()
.toISO();
const endTime = getISOTime(
DateTime.fromISO(eventData.end?.dateTime as string)
.plus({ minutes: timeToAdd })
.toUTC()
);

// Keep responseStatus as accepted, because in case of
// conflicts in Google calendar, adding time to the existing
Expand Down Expand Up @@ -198,6 +200,10 @@ export const rollBackDeclinedUpdate = () => {
},
{ email: res.locals.email, responseStatus: 'accepted' }
];
if (!endTime) {
console.error('Time parse error', endTime);
throw new Error('Time parse error');
}

await calendar.updateEndTime(
client,
Expand Down
7 changes: 6 additions & 1 deletion backend/src/controllers/roomController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ export const fetchAvailability = () => {
req.query.until as string
).toUTC();
start = startDt.toISO();
end = endDt.toISO();

end = endDt.toISO() || '';

if (end === '') {
throw new Error('End date not found');
}

if (endDt <= startDt) {
return responses.badRequest(req, res);
Expand Down
11 changes: 11 additions & 0 deletions backend/src/utils/timeUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { DateTime } from 'luxon';

export const getISOTime = (time: DateTime): string => {
const timeISO: string = time.toISO() || '';

if (timeISO === '') {
console.error(time);
throw new Error('Time parse error');
}
return timeISO;
};

0 comments on commit d46f49d

Please sign in to comment.