Skip to content

Commit 43bba66

Browse files
committed
fix: toggle notification settings
1 parent c8f5134 commit 43bba66

File tree

4 files changed

+60
-10
lines changed

4 files changed

+60
-10
lines changed

course-matrix/backend/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ app.use("/api/ai", aiRouter);
4545

4646
// Initialize cron job
4747
// Note: For testing purposes can set first argument to '*/15 * * * * *' to run every 15s
48-
cron.schedule("* * * * *", checkAndNotifyEvents);
48+
cron.schedule("*/15 * * * * *", checkAndNotifyEvents);
4949

5050
/**
5151
* Root route to test the backend server.

course-matrix/backend/src/services/emailNotificationService.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,30 @@ export async function checkAndNotifyEvents() {
119119

120120
// Send email notifications for each event
121121
for (const event of events) {
122+
// get timetable notif enabled
123+
const { data: timetables, error: errorTimetable} =
124+
await supabaseServersideClient
125+
.schema("timetable")
126+
.from("timetables")
127+
.select("email_notifications_enabled")
128+
.eq("id", event.calendar_id)
129+
.eq("user_id", event.user_id)
130+
.limit(1);
131+
132+
if (errorTimetable) {
133+
console.error("Error fetching timetable: ", errorTimetable);
134+
return;
135+
}
136+
137+
if (!timetables || timetables.length === 0) {
138+
console.error("Timetable not found id:", event.offering_id);
139+
return;
140+
}
141+
142+
if (!timetables[0].email_notifications_enabled) {
143+
continue;
144+
}
145+
122146
// Get offering
123147
const { data: offerings, error: errorOffering } =
124148
await supabaseServersideClient

course-matrix/frontend/src/models/models.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,6 @@ export interface CorequisiteModel {
111111
course_code: string;
112112

113113
/** Course code for the corequisite course */
114-
id: number;
115-
created_at: string;
116-
updated_at: string;
117-
course_id: number;
118-
corequisite_id: number;
119-
course_code: string;
120114
corequisite_code: string;
121115
}
122116

@@ -175,3 +169,32 @@ export interface OfferingModel {
175169
/** Additional notes about the course offering */
176170
notes: string;
177171
}
172+
173+
/**
174+
* Represents a timetable including details like its title, semester, and favorite satus
175+
*/
176+
export interface TimetableModel {
177+
/** Unique identifier */
178+
id: number;
179+
180+
/** Creation timestamp */
181+
created_at: string;
182+
183+
/** Last updated at timestamp */
184+
updated_at: string;
185+
186+
/** Name of timetable */
187+
timetable_title: string;
188+
189+
/** ID of user owning this timetable */
190+
user_id: string;
191+
192+
/** Semester that the timetable is for */
193+
semester: string;
194+
195+
/** Is timetable favorited by user */
196+
favorite: boolean;
197+
198+
/** Has user enabled email notifications for this timetable */
199+
email_notifications_enabled: boolean;
200+
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
DialogTrigger,
1515
} from "@/components/ui/dialog";
1616
import { Switch } from "@/components/ui/switch";
17+
import { TimetableModel } from "@/models/models";
1718
import { useEffect, useState } from "react";
1819

1920
interface EmailNotificationSettingsProps {
@@ -28,9 +29,11 @@ export const EmailNotificationSettings = ({
2829
const [toggled, setToggled] = useState<boolean>(false);
2930

3031
useEffect(() => {
31-
if ((data as any)?.email_notifications_enabled !== undefined) {
32-
setToggled((data as any)?.email_notifications_enabled);
33-
console.log((data as any)?.email_notifications_enabled);
32+
if (data) {
33+
const val = (data as TimetableModel[])[0]?.email_notifications_enabled;
34+
if ( val !== undefined) {
35+
setToggled(val);
36+
}
3437
}
3538
}, [data]);
3639

0 commit comments

Comments
 (0)