Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | 1x 1x 1x 4x 4x 4x 4x 3x 3x 1x 2x 1x 1x 1x 1x 3x 3x 3x 2x 2x 2x 1x 1x 1x 1x 1x 1x 3x 3x 3x 2x 2x 1x 1x 1x 1x 4x 4x 3x 3x 1x 2x 1x 1x 1x 1x 1x 2x 2x 2x 2x 1x 1x 1x | import { supabase } from "../supabaseClient"; import { EnrollmentStatus, UserActivityLogItem } from "../types"; import { CLASS_ENDPOINT, LOG_ENDPOINT } from "./endpoints"; import { UserClass } from "./types/user"; /** * * Creates a new class in the database. * @param {UserClass} newClass - The class object to be created * @returns {Promise<{ data?: { id: string }; error?: string }>} - The response from the server */ export const createClass = async ( newClass: UserClass ): Promise<{ data?: { id: string }; error?: string }> => { console.log("Creating class with data:", JSON.stringify(newClass)); try { const response = await fetch(`${CLASS_ENDPOINT}/create`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(newClass), }); const data = await response.json(); if (!response.ok) { return { error: data.message || `Failed to create class: ${response.status} ${response.statusText}`, }; } if (!data.data || !data.data.id) { return { error: "Invalid response: expected class ID" }; } return { data: { id: data.data.id } }; } catch (err) { return { error: err instanceof Error ? err.message : "Unknown error occurred", }; } }; /** * Fetches all classes from the database for an instructor. * @param {string} instructorId - The ID of the instructor * @returns {Promise<{ data?: UserClass[]; error?: string }>} - The response from the server */ export const getClassesByInstructor = async ( instructorId: string ): Promise<{ data?: UserClass[]; error?: string }> => { try { const response = await fetch( `${CLASS_ENDPOINT}/instructor/${instructorId}` ); const data = await response.json(); Iif (!response.ok) { return { error: data.message || `Failed to fetch classes: ${response.status} ${response.statusText}`, }; } if (!data.data) { return { error: "Invalid response: expected list of classes" }; } const classes = data.data.map( (classItem: { id: string; class_title: string; class_code: string; instructor_id: string; class_hex_color: string; class_image_cover: string; created_at: string; class_description: string; }): UserClass => ({ id: classItem.id, classTitle: classItem.class_title, classCode: classItem.class_code, instructorId: classItem.instructor_id, classHexColor: classItem.class_hex_color, classImageCover: classItem.class_image_cover, createdAt: classItem.created_at, classDescription: classItem.class_description, }) ); return { data: classes }; } catch (err) { return { error: err instanceof Error ? err.message : "Unknown error occurred", }; } }; /** * Registers a user to a class in the database. * @param {string} classId - The ID of the class * @param {string} studentId - The ID of the student * @returns {Promise<{ data?: UserClass[]; error?: string }>} - The response from the server or an error message. */ export const registerUserToClass = async ( studentId: string, classId: string ): Promise<{ data?: { id: string }; error?: string }> => { try { const response = await fetch(`${CLASS_ENDPOINT}/register`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ studentId, classId }), }); const data = await response.json(); if (!response.ok) { return { error: data.message || `Failed to register: ${response.status} ${response.statusText}`, }; } return { data: data.data }; } catch (err) { return { error: err instanceof Error ? err.message : "Unknown error occurred", }; } }; /** * Fetches all activity logs for a specific class from the database. * @param {string} classId - The ID of the class * @returns {Promise<{ data?: UserActivityLogItem[]; error?: string }>} - The response from the server or an error message. */ export async function getClassActivityByClassId( classId: string ): Promise<{ data?: UserActivityLogItem[] | null; error?: string }> { try { const response = await fetch(`${LOG_ENDPOINT}/class/${classId}`, { method: "GET", headers: { "Content-Type": "application/json" }, }); const data = await response.json(); if (!response.ok) { return { error: data.message || `Failed to get logs by class: ${response.status} ${response.statusText}`, }; } if (!Array.isArray(data.data)) { return { error: "Invalid response: expected an array of activity logs" }; } // eslint-disable-next-line @typescript-eslint/no-explicit-any const activityLogs: UserActivityLogItem[] = data.data.map((item: any) => ({ id: item.id, event: item.event, timestamp: item.timestamp, timeLapse: item.time_lapse, metadata: { userId: item.metadata.user_id, hasBug: item.metadata.has_bug, suggestionId: item.metadata.suggestion_id, userSectionId: item.metadata.user_section_id, userClassId: item.metadata.user_class_id, }, })); return { data: activityLogs }; } catch (err) { return { error: err instanceof Error ? err.message : "Unknown error occurred", }; } } /** * Updates the enrollment status of a student in a class. * @param {string} classId - The ID of the class * @param {string} studentId - The ID of the student * @param {EnrollmentStatus} newStatus - The new enrollment status * @returns {Promise<{ success: boolean; error?: string }>} - The response from the server or an error message. **/ export const updateStudentEnrollmentStatus = async ( classId: string, studentId: string, newStatus: EnrollmentStatus ): Promise<{ success: boolean; error?: string }> => { const updateFields = { enrollment_status: newStatus, user_class_status: newStatus === "ENROLLED" ? "ACTIVE" : null, }; const { error } = await supabase .from("class_users") .update(updateFields) .eq("class_id", classId) .eq("student_id", studentId); if (error) { console.error(`Failed to update enrollment status:`, error.message); return { success: false, error: error.message }; } return { success: true }; }; |