Skip to content

Commit c2097c6

Browse files
committed
timetable limit with AI feature still in the works
1 parent a166876 commit c2097c6

File tree

4 files changed

+54
-14
lines changed

4 files changed

+54
-14
lines changed

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export const availableFunctions: AvailableFunctions = {
145145
deleteTimetable: async (args: any, req: Request) => {
146146
try {
147147
const { id } = args;
148-
148+
149149
// Retrieve the authenticated user
150150
const user_id = (req as any).user.id;
151151

@@ -197,13 +197,33 @@ export const availableFunctions: AvailableFunctions = {
197197
try {
198198
// Extract event details and course information from the request
199199
const { name, semester, courses, restrictions } = args;
200+
// Get user id from session authentication to insert in the user_id col
201+
const user_id = (req as any).user.id;
202+
200203
if (name.length > 50) {
201204
return {
202205
status: 400,
203206
error: "timetable title is over 50 characters long",
204207
};
205208
}
206209

210+
// Timetables cannot exceed the size of 25.
211+
const{count: timetable_count, error: timetableCountError} =
212+
await supabase
213+
.schema("timetable")
214+
.from("timetables")
215+
.select('*', { count: 'exact', head: true })
216+
.eq("user_id", user_id);
217+
218+
console.log(timetable_count);
219+
220+
if ((timetable_count ?? 0) >=25){
221+
return {
222+
status: 400,
223+
error: "You have exceeded the limit of 25 timetables",
224+
};
225+
}
226+
207227
const courseOfferingsList: OfferingList[] = [];
208228
const validCourseOfferingsList: GroupedOfferingList[] = [];
209229
const maxdays = getMaxDays(restrictions);
@@ -276,9 +296,6 @@ export const availableFunctions: AvailableFunctions = {
276296

277297
// ------ CREATE FLOW ------
278298

279-
// Get user id from session authentication to insert in the user_id col
280-
const user_id = (req as any).user.id;
281-
282299
// Retrieve timetable title
283300
const schedule = trim(validSchedules)[0];
284301
if (!name || !semester) {
@@ -288,6 +305,8 @@ export const availableFunctions: AvailableFunctions = {
288305
};
289306
}
290307

308+
309+
291310
// Check if a timetable with the same title already exist for this user
292311
const { data: existingTimetable, error: existingTimetableError } =
293312
await supabase

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,9 @@ export const chat = asyncHandler(async (req: Request, res: Response) => {
244244
- Allowing natural language queries about courses, offerings, and academic programs
245245
- Providing personalized recommendations based on degree requirements and course availability
246246
- Creating, reading, updating, and deleting user timetables based on natural language
247-
247+
248+
##!IMPORTANT!: For create timetable requests, even if the timetable limit of 25 has been exceeded, run the tool regardless to get a more up to date data.
249+
248250
## Your Capabilities
249251
- Create new timetables based on provided courses and restrictions
250252
- Update timetable names and semesters

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ export default {
3131
.status(400)
3232
.json({ error: "Timetable Title cannot be over 50 characters long" });
3333
}
34+
// Timetables cannot exceed the size of 25.
35+
const{count: timetable_count, error: timetableCountError} =
36+
await supabase
37+
.schema("timetable")
38+
.from("timetables")
39+
.select('*', { count: 'exact', head: true })
40+
.eq("user_id", user_id);
41+
42+
console.log(timetable_count);
43+
44+
if ((timetable_count ?? 0) >=25){
45+
return res.status(400).json({ error: "You have exceeded the limit of 25 timetables" });
46+
}
3447

3548
// Check if a timetable with the same title already exist for this user
3649
const { data: existingTimetable, error: existingTimetableError } =
@@ -52,6 +65,7 @@ export default {
5265
.json({ error: "A timetable with this title already exists" });
5366
}
5467
//Create query to insert the user_id and timetable_title into the db
68+
5569
let insertTimetable = supabase
5670
.schema("timetable")
5771
.from("timetables")
@@ -176,15 +190,14 @@ export default {
176190
}
177191

178192
// Timetables cannot be longer than 50 characters.
179-
if (timetable_title.length > 50) {
193+
if (timetable_title && timetable_title.length > 50) {
180194
return res
181195
.status(400)
182196
.json({ error: "Timetable Title cannot be over 50 characters long" });
183197
}
184198

185199
//Retrieve the authenticated user
186200
const user_id = (req as any).user.id;
187-
188201
//Retrieve users allowed to access the timetable
189202
const { data: timetableUserData, error: timetableUserError } =
190203
await supabase
@@ -194,15 +207,13 @@ export default {
194207
.eq("user_id", user_id)
195208
.eq("id", id)
196209
.maybeSingle();
197-
198210
if (timetableUserError)
199211
return res.status(400).json({ error: timetableUserError.message });
200212

201213
//Validate timetable validity:
202214
if (!timetableUserData || timetableUserData.length === 0) {
203215
return res.status(404).json({ error: "Calendar id not found" });
204216
}
205-
206217
// Check if another timetable with the same title already exist for this user
207218
const { data: existingTimetable, error: existingTimetableError } =
208219
await supabase
@@ -213,7 +224,6 @@ export default {
213224
.eq("timetable_title", timetable_title)
214225
.neq("id", id)
215226
.maybeSingle();
216-
217227
if (existingTimetableError) {
218228
return res.status(400).json({ error: existingTimetableError.message });
219229
}
@@ -223,7 +233,6 @@ export default {
223233
.status(400)
224234
.json({ error: "Another timetable with this title already exists" });
225235
}
226-
227236
let updateData: any = {};
228237
if (timetable_title) updateData.timetable_title = timetable_title;
229238
if (semester) updateData.semester = semester;
@@ -240,13 +249,11 @@ export default {
240249
.eq("id", id)
241250
.select()
242251
.single();
243-
244252
const { data: timetableData, error: timetableError } =
245253
await updateTimetableQuery;
246254

247255
if (timetableError)
248256
return res.status(400).json({ error: timetableError.message });
249-
250257
// If no records were updated due to non-existence timetable or it doesn't belong to the user.
251258
if (!timetableData || timetableData.length === 0) {
252259
return res.status(404).json({
@@ -255,6 +262,7 @@ export default {
255262
}
256263
return res.status(200).json(timetableData);
257264
} catch (error) {
265+
console.error(error);
258266
return res.status(500).send({ error });
259267
}
260268
}),

course-matrix/frontend/src/pages/Home/Home.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import TimetableCard from "./TimetableCard";
44
import TimetableCompareButton from "./TimetableCompareButton";
55
import TimetableCreateNewButton from "./TimetableCreateNewButton";
66
import { useGetTimetablesQuery } from "../../api/timetableApiSlice";
7-
import { useState } from "react";
7+
import { useState,useEffect } from "react";
88
import TimetableErrorDialog from "../TimetableBuilder/TimetableErrorDialog";
99

1010
export interface Timetable {
@@ -32,15 +32,26 @@ const Home = () => {
3232
isLoading: boolean;
3333
refetch: () => void;
3434
};
35+
3536

3637
const [errorMessage, setErrorMessage] = useState<string | null>(null);
38+
const [count, setCount] = useState<number>(0);
39+
40+
useEffect(() => {
41+
if(data!==undefined)
42+
setCount(data.length);
43+
}, [data]);
3744

3845
return (
3946
<div className="w-full">
4047
<div className="m-8">
4148
<div className="mb-4 flex items-center gap-2 relative group">
4249
<h1 className="text-2xl font-medium tracking-tight">My Timetables</h1>
4350
<Pin size={24} className="text-blue-500" />
51+
52+
<h1 className={`${
53+
count >= 25 ? "font-bold text-red-500" : "font-normal text-black"
54+
}`}> (Timetable limit: {count}/25)</h1>
4455
</div>
4556
<TimetableErrorDialog
4657
errorMessage={errorMessage}

0 commit comments

Comments
 (0)