// routes/api/brain/reset-session.ts // API endpoint for resetting all Brain study progress for a user import { define, State } from "../../../utils.ts"; import { SessionCookie } from "../../auth/callback.ts"; import { brainDataClient } from "../../../utils/BrainDataClient.ts"; export const handler = define.handlers({ // DELETE: Reset all quiz progress for the current user async DELETE(ctx) { const state = ctx.state as State; const stateData: SessionCookie = state.data ?? { started_on: 0, user_name: "", user_id: "", role: "", user_encrypted: false, }; if (!state.stateId || !stateData.user_id) { return new Response( JSON.stringify({ error: "Unauthorized" }), { status: 401, headers: { "Content-Type": "application/json" } }, ); } try { // Reset all progress for this user await brainDataClient.resetAllProgress(stateData.user_id); return new Response( JSON.stringify({ success: true, message: "All quiz progress has been reset", }), { status: 200, headers: { "Content-Type": "application/json" } }, ); } catch (error) { console.error("Error resetting brain session:", error); return new Response( JSON.stringify({ error: "Failed to reset session" }), { status: 500, headers: { "Content-Type": "application/json" } }, ); } }, });