Skip to content

Commit 048ba31

Browse files
committed
Fix: Restrictions not saving
1 parent 224095b commit 048ba31

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
lines changed

course-matrix/backend/src/constants/availableFunctions.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import {
1515
} from "../utils/generatorHelpers";
1616
import getOfferings from "../services/getOfferings";
1717
import { getValidSchedules } from "../services/getValidSchedules";
18+
import { RestrictionForm } from "../models/timetable-form";
19+
import { convertTimeStringToDate } from "../utils/convert-time-string";
1820

1921
// Add all possible function names here
2022
export type FunctionNames =
@@ -385,6 +387,42 @@ export const availableFunctions: AvailableFunctions = {
385387
}
386388
}
387389

390+
// Save restrictions
391+
for (const restriction of (restrictions as RestrictionForm[])) {
392+
let startTime: String | null = null;
393+
let endTime: String | null = null;
394+
395+
if (restriction.startTime) {
396+
let restriction_start_time = convertTimeStringToDate(restriction.startTime);
397+
startTime = restriction_start_time.toISOString().split("T")[1];
398+
}
399+
400+
if (restriction.endTime) {
401+
let restriction_end_time = convertTimeStringToDate(restriction.endTime);
402+
endTime = restriction_end_time.toISOString().split("T")[1];
403+
}
404+
const { data: restrictionData, error: restrictionError } = await supabase
405+
.schema("timetable")
406+
.from("restriction")
407+
.insert([
408+
{
409+
user_id,
410+
type: restriction?.type,
411+
days: restriction?.days,
412+
start_time: startTime,
413+
end_time: endTime,
414+
disabled: restriction?.disabled,
415+
num_days: restriction?.numDays,
416+
calendar_id: timetableData?.id,
417+
},
418+
])
419+
.select();
420+
421+
if (restrictionError) {
422+
return { status: 400, error: restrictionError.message };
423+
}
424+
}
425+
388426
return { status: 201, data: timetableData };
389427
} catch (error) {
390428
// Catch any error and return the error message

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,12 @@ export const chat = asyncHandler(async (req: Request, res: Response) => {
293293
parameters: TimetableFormSchema,
294294
execute: async (args) => {
295295
// console.log("Args for generate: ", args)
296-
// console.log("restrictions :", JSON.stringify(args.restrictions))
296+
console.log("restrictions :", JSON.stringify(args.restrictions))
297297
const data = await availableFunctions.generateTimetable(
298298
args,
299299
req,
300300
);
301-
// console.log("Generated timetable: ", data)
301+
console.log("Generated timetable: ", data)
302302
return data;
303303
},
304304
}),

course-matrix/backend/src/models/timetable-form.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ export type RestrictionForm = {
1717
type: string;
1818
days?: string[];
1919
numDays?: number;
20-
startTime?: Date;
21-
endTime?: Date;
20+
startTime?: string;
21+
endTime?: string;
2222
disabled?: boolean;
2323
};
2424

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export function convertTimeStringToDate(timeString: string): Date {
2+
// Validate the timeString format (HH:mm:ss)
3+
const isValidFormat = /^([0-1]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/.test(timeString);
4+
if (!isValidFormat) {
5+
throw new Error("Invalid time format. Expected HH:mm:ss");
6+
}
7+
8+
const date = new Date();
9+
10+
const [hours, minutes, seconds] = timeString.split(':').map(Number);
11+
12+
date.setHours(hours);
13+
date.setMinutes(minutes);
14+
date.setSeconds(seconds);
15+
date.setMilliseconds(0);
16+
17+
return date;
18+
}

0 commit comments

Comments
 (0)