Skip to content

Commit c580b38

Browse files
committed
Update start time and end time handling
1 parent ba874af commit c580b38

File tree

1 file changed

+33
-49
lines changed

1 file changed

+33
-49
lines changed

course-matrix/backend/src/controllers/restrictionsController.ts

Lines changed: 33 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Request, Response } from "express";
22
import asyncHandler from "../middleware/asyncHandler";
33
import { supabase } from "../db/setupDb";
4+
import { start } from "repl";
45

56
export default {
67
/**
@@ -32,29 +33,10 @@ export default {
3233
}
3334

3435
// Function to construct date in local time
35-
const restriction_start = new Date(`2025-08-08T${start_time}-00:00`);
36-
const restriction_end = new Date(`2025-08-08T${end_time}-00:00`);
37-
const valid_minutes = [0, 15, 30, 45];
38-
39-
if (
40-
!valid_minutes.includes(restriction_start.getMinutes()) ||
41-
!valid_minutes.includes(restriction_end.getMinutes())
42-
) {
43-
return res.status(400).json({
44-
error: "Event start / end minutes must be 00, 15, 30, 45",
45-
});
46-
}
47-
48-
if (restriction_start.getTime() === restriction_end.getTime()) {
36+
if (!start_time && !end_time) {
4937
return res
5038
.status(400)
51-
.json({ error: "Event start and end time must not be the same" });
52-
}
53-
54-
if (restriction_start.getTime() - restriction_end.getTime() > 0) {
55-
return res
56-
.status(400)
57-
.json({ error: "Event start time cannot be after event end time" });
39+
.json({ error: "Start time or end time must be provided" });
5840
}
5941

6042
//Retrieve users allowed to access the timetable
@@ -83,6 +65,25 @@ export default {
8365
.json({ error: "Unauthorized access to timetable restriction" });
8466
}
8567

68+
let startTime: String | null = null;
69+
let endTime: String | null = null;
70+
71+
if (start_time) {
72+
let restriction_start_time = new Date(start_time);
73+
startTime = restriction_start_time.toISOString().split("T")[1];
74+
}
75+
76+
if (end_time) {
77+
let restriction_end_time = new Date(end_time);
78+
endTime = restriction_end_time.toISOString().split("T")[1];
79+
}
80+
81+
if (!start_time && !end_time) {
82+
return res
83+
.status(400)
84+
.json({ error: "Start time or end time must be provided" });
85+
}
86+
8687
const { data: restrictionData, error: restrictionError } = await supabase
8788
.schema("timetable")
8889
.from("restriction")
@@ -91,8 +92,8 @@ export default {
9192
user_id,
9293
type,
9394
days,
94-
start_time,
95-
end_time,
95+
start_time: startTime,
96+
end_time: endTime,
9697
disabled,
9798
num_days,
9899
calendar_id,
@@ -203,34 +204,17 @@ export default {
203204
return res.status(404).json({ error: "Restriction id does not exist" });
204205
}
205206

206-
// Function to construct date in local time
207-
const restriction_start = new Date(
208-
`2025-08-08T${updateData.start_time}-00:00`,
209-
);
210-
const restriction_end = new Date(
211-
`2025-08-08T${updateData.end_time}-00:00`,
212-
);
213-
const valid_minutes = [0, 15, 30, 45];
214-
215-
if (
216-
!valid_minutes.includes(restriction_start.getMinutes()) ||
217-
!valid_minutes.includes(restriction_end.getMinutes())
218-
) {
219-
return res.status(400).json({
220-
error: "Event start / end minutes must be 00, 15, 30, 45",
221-
});
207+
//Ensure start_time and end_time only contain time value
208+
if (updateData.start_time) {
209+
const restriction_start_time = new Date(updateData.start_time);
210+
updateData.start_time = restriction_start_time
211+
.toISOString()
212+
.split("T")[1];
222213
}
223214

224-
if (restriction_start.getTime() === restriction_end.getTime()) {
225-
return res
226-
.status(400)
227-
.json({ error: "Event start and end time must not be the same" });
228-
}
229-
230-
if (restriction_start.getTime() - restriction_end.getTime() > 0) {
231-
return res
232-
.status(400)
233-
.json({ error: "Event start time cannot be after event end time" });
215+
if (updateData.end_time) {
216+
const restriction_end_time = new Date(updateData.end_time);
217+
updateData.end_time = restriction_end_time.toISOString().split("T")[1];
234218
}
235219

236220
//Retrieve users allowed to access the timetable

0 commit comments

Comments
 (0)