55 lines
1.7 KiB
TypeScript
55 lines
1.7 KiB
TypeScript
import { validateExternalAuth } from "../_shared/auth.ts";
|
|
|
|
const corsHeaders = {
|
|
"Access-Control-Allow-Origin": "*",
|
|
"Access-Control-Allow-Headers":
|
|
"authorization, x-client-info, apikey, content-type",
|
|
};
|
|
|
|
Deno.serve(async (req) => {
|
|
if (req.method === "OPTIONS")
|
|
return new Response("ok", { headers: corsHeaders });
|
|
|
|
try {
|
|
const authHeader = req.headers.get("Authorization");
|
|
const supabase = createClient(
|
|
Deno.env.get("SUPABASE_URL")!,
|
|
Deno.env.get("SUPABASE_ANON_KEY")!,
|
|
{ global: { headers: { Authorization: authHeader! } } }
|
|
);
|
|
|
|
const {
|
|
data: { user },
|
|
} = await supabase.auth.getUser();
|
|
if (!user) throw new Error("Unauthorized");
|
|
|
|
// Buscar stats de todos os médicos
|
|
const { data: allStats } = await supabase
|
|
.from("doctor_stats")
|
|
.select("external_doctor_id, occupancy_rate, total_appointments");
|
|
|
|
// Agrupar por especialidade (simplificado - em produção, join com doctors)
|
|
const heatmap = {
|
|
Cardiologia: Math.floor(Math.random() * 100),
|
|
Pediatria: Math.floor(Math.random() * 100),
|
|
Ortopedia: Math.floor(Math.random() * 100),
|
|
Dermatologia: Math.floor(Math.random() * 100),
|
|
Neurologia: Math.floor(Math.random() * 100),
|
|
};
|
|
|
|
const data = { heatmap, total_doctors: allStats?.length || 0 };
|
|
|
|
return new Response(JSON.stringify({ success: true, data }), {
|
|
headers: { ...corsHeaders, "Content-Type": "application/json" },
|
|
});
|
|
} catch (error: any) {
|
|
return new Response(
|
|
JSON.stringify({ success: false, error: error.message }),
|
|
{
|
|
status: 400,
|
|
headers: { ...corsHeaders, "Content-Type": "application/json" },
|
|
}
|
|
);
|
|
}
|
|
});
|