interface Env { DB: D1Database; } export const onRequest = async (context: { request: Request; env: Env }) => { const { request, env } = context; const url = new URL(request.url); const path = url.pathname; if (request.method === "OPTIONS") { return new Response(null, { headers: { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "GET, POST, DELETE, OPTIONS", "Access-Control-Allow-Headers": "Content-Type", }, }); } try { if (path === "/api/db/professionals") { const type = url.searchParams.get("type"); if (request.method === "GET") { let query = "SELECT * FROM professionals"; const params: any[] = []; if (type) { query += " WHERE type = ?"; params.push(type); } const { results } = await env.DB.prepare(query).bind(...params).all(); return new Response(JSON.stringify(results), { headers: { "Content-Type": "application/json" }, }); } if (request.method === "POST") { const { data, type: bodyType } = await request.json(); const professionalsArray = Array.isArray(data) ? data : [data]; const statements = professionalsArray.map((p: any) => env.DB.prepare( "INSERT INTO professionals (name, specialty, address, contact, website, summary, type) VALUES (?, ?, ?, ?, ?, ?, ?)" ).bind( p.name, p.specialty, p.address, p.contact, p.website, p.summary, bodyType || type || 'lawyer' ) ); await env.DB.batch(statements); return new Response(JSON.stringify({ success: true }), { headers: { "Content-Type": "application/json" }, }); } if (request.method === "DELETE") { if (type) { await env.DB.prepare("DELETE FROM professionals WHERE type = ?").bind(type).run(); } else { await env.DB.prepare("DELETE FROM professionals").run(); } return new Response(JSON.stringify({ success: true }), { headers: { "Content-Type": "application/json" }, }); } } if (path === "/api/db/chats") { if (request.method === "GET") { const { results } = await env.DB.prepare("SELECT * FROM chat_sessions ORDER BY timestamp DESC").all(); const chats = results.map((r: any) => ({ ...r, messages: JSON.parse(r.messages) })); return new Response(JSON.stringify(chats), { headers: { "Content-Type": "application/json" }, }); } if (request.method === "POST") { const chat = await request.json(); await env.DB.prepare( "INSERT OR REPLACE INTO chat_sessions (id, title, docType, timestamp, messages) VALUES (?, ?, ?, ?, ?)" ).bind( chat.id, chat.title, chat.docType, chat.timestamp, JSON.stringify(chat.messages) ).run(); return new Response(JSON.stringify({ success: true }), { headers: { "Content-Type": "application/json" }, }); } if (request.method === "DELETE") { const id = url.searchParams.get("id"); if (id) { await env.DB.prepare("DELETE FROM chat_sessions WHERE id = ?").bind(id).run(); } else { await env.DB.prepare("DELETE FROM chat_sessions").run(); } return new Response(JSON.stringify({ success: true }), { headers: { "Content-Type": "application/json" }, }); } } if (path === "/api/db/checkpoints") { if (request.method === "GET") { const { results } = await env.DB.prepare("SELECT * FROM checkpoints ORDER BY timestamp DESC").all(); const checkpoints = results.map((r: any) => ({ ...r, state: JSON.parse(r.state) })); return new Response(JSON.stringify(checkpoints), { headers: { "Content-Type": "application/json" }, }); } if (request.method === "POST") { const ckpt = await request.json(); await env.DB.prepare( "INSERT OR REPLACE INTO checkpoints (id, name, timestamp, state) VALUES (?, ?, ?, ?)" ).bind( ckpt.id, ckpt.name, ckpt.timestamp, JSON.stringify(ckpt.state) ).run(); return new Response(JSON.stringify({ success: true }), { headers: { "Content-Type": "application/json" }, }); } if (request.method === "DELETE") { const id = url.searchParams.get("id"); if (id) { await env.DB.prepare("DELETE FROM checkpoints WHERE id = ?").bind(id).run(); } else { await env.DB.prepare("DELETE FROM checkpoints").run(); } return new Response(JSON.stringify({ success: true }), { headers: { "Content-Type": "application/json" }, }); } } return new Response("Not Found", { status: 404 }); } catch (error: any) { return new Response(JSON.stringify({ error: error.message }), { status: 500, headers: { "Content-Type": "application/json" }, }); } };