|
1 | 1 | import { supabase } from "../db/setupDb"; |
2 | 2 | import { Request } from "express"; |
3 | 3 |
|
4 | | -export type FunctionNames = "getTimetables"; // Add all possible function names here |
| 4 | +// Add all possible function names here |
| 5 | +export type FunctionNames = "getTimetables" | "updateTimetable" | "deleteTimetable"; |
5 | 6 |
|
6 | 7 | type AvailableFunctions = { |
7 | 8 | [K in FunctionNames]: (args: any, req: Request) => Promise<any>; |
@@ -40,4 +41,124 @@ export const availableFunctions: AvailableFunctions = { |
40 | 41 | return { status: 400, error: error }; |
41 | 42 | } |
42 | 43 | }, |
| 44 | + updateTimetable: async (args: any, req: Request) => { |
| 45 | + try { |
| 46 | + const { id, timetable_title, semester } = args; |
| 47 | + |
| 48 | + if (!timetable_title && !semester) { |
| 49 | + return { |
| 50 | + status: 400, |
| 51 | + error: |
| 52 | + "New timetable title or semester is required when updating a timetable", |
| 53 | + }; |
| 54 | + } |
| 55 | + |
| 56 | + //Retrieve the authenticated user |
| 57 | + const user_id = (req as any).user.id; |
| 58 | + |
| 59 | + //Retrieve users allowed to access the timetable |
| 60 | + const { data: timetableUserData, error: timetableUserError } = |
| 61 | + await supabase |
| 62 | + .schema("timetable") |
| 63 | + .from("timetables") |
| 64 | + .select("*") |
| 65 | + .eq("id", id) |
| 66 | + .eq("user_id", user_id) |
| 67 | + .maybeSingle(); |
| 68 | + |
| 69 | + const timetable_user_id = timetableUserData?.user_id; |
| 70 | + |
| 71 | + if (timetableUserError) |
| 72 | + return {status: 400, error: timetableUserError.message }; |
| 73 | + |
| 74 | + //Validate timetable validity: |
| 75 | + if (!timetableUserData || timetableUserData.length === 0) { |
| 76 | + return {status: 404, error: "Calendar id not found" }; |
| 77 | + } |
| 78 | + |
| 79 | + //Validate user access |
| 80 | + if (user_id !== timetable_user_id) { |
| 81 | + return { status: 401, error: "Unauthorized access to timetable events" }; |
| 82 | + } |
| 83 | + |
| 84 | + let updateData: any = {}; |
| 85 | + if (timetable_title) updateData.timetable_title = timetable_title; |
| 86 | + if (semester) updateData.semester = semester; |
| 87 | + |
| 88 | + //Update timetable title, for authenticated user only |
| 89 | + let updateTimetableQuery = supabase |
| 90 | + .schema("timetable") |
| 91 | + .from("timetables") |
| 92 | + .update(updateData) |
| 93 | + .eq("id", id) |
| 94 | + .eq("user_id", user_id) |
| 95 | + .select(); |
| 96 | + |
| 97 | + const { data: timetableData, error: timetableError } = |
| 98 | + await updateTimetableQuery; |
| 99 | + |
| 100 | + if (timetableError) |
| 101 | + return { status: 400, error: timetableError.message }; |
| 102 | + |
| 103 | + // If no records were updated due to non-existence timetable or it doesn't belong to the user. |
| 104 | + if (!timetableData || timetableData.length === 0) { |
| 105 | + return { |
| 106 | + status: 404, |
| 107 | + error: "Timetable not found or you are not authorized to update it", |
| 108 | + }; |
| 109 | + } |
| 110 | + return { status: 200, data: timetableData }; |
| 111 | + } catch (error) { |
| 112 | + return { status: 500, error: error }; |
| 113 | + } |
| 114 | + }, |
| 115 | + deleteTimetable: async (args: any, req: Request) => { |
| 116 | + try { |
| 117 | + const { id } = args; |
| 118 | + |
| 119 | + // Retrieve the authenticated user |
| 120 | + const user_id = (req as any).user.id; |
| 121 | + |
| 122 | + //Retrieve users allowed to access the timetable |
| 123 | + const { data: timetableUserData, error: timetableUserError } = |
| 124 | + await supabase |
| 125 | + .schema("timetable") |
| 126 | + .from("timetables") |
| 127 | + .select("*") |
| 128 | + .eq("id", id) |
| 129 | + .eq("user_id", user_id) |
| 130 | + .maybeSingle(); |
| 131 | + const timetable_user_id = timetableUserData?.user_id; |
| 132 | + |
| 133 | + if (timetableUserError) |
| 134 | + return { status: 400, error: timetableUserError.message }; |
| 135 | + |
| 136 | + //Validate timetable validity: |
| 137 | + if (!timetableUserData || timetableUserData.length === 0) { |
| 138 | + return { status: 404, error: "Calendar id not found" }; |
| 139 | + } |
| 140 | + |
| 141 | + //Validate user access |
| 142 | + if (user_id !== timetable_user_id) { |
| 143 | + return { status: 401, error: "Unauthorized access to timetable events" }; |
| 144 | + } |
| 145 | + |
| 146 | + // Delete only if the timetable belongs to the authenticated user |
| 147 | + let deleteTimetableQuery = supabase |
| 148 | + .schema("timetable") |
| 149 | + .from("timetables") |
| 150 | + .delete() |
| 151 | + .eq("id", id) |
| 152 | + .eq("user_id", user_id); |
| 153 | + |
| 154 | + const { error: timetableError } = await deleteTimetableQuery; |
| 155 | + |
| 156 | + if (timetableError) |
| 157 | + return { status: 400, error: timetableError.message }; |
| 158 | + |
| 159 | + return { status: 200, data: "Timetable successfully deleted"}; |
| 160 | + } catch (error) { |
| 161 | + return { status: 500, error: error }; |
| 162 | + } |
| 163 | + } |
43 | 164 | }; |
0 commit comments