issue/6-generacin-de-materias #16
@@ -1,5 +1,8 @@
|
||||
import "jsr:@supabase/functions-js/edge-runtime.d.ts";
|
||||
import { corsHeaders } from "../_shared/cors.ts";
|
||||
import { HttpError, sendError, sendSuccess } from "../_shared/utils.ts";
|
||||
import { createClient } from "npm:@supabase/supabase-js@2";
|
||||
import type { Database, Json } from "../_shared/database.types.ts";
|
||||
|
||||
console.log("Hello from Functions!");
|
||||
|
||||
@@ -13,13 +16,92 @@ Deno.serve(async (req) => {
|
||||
if (req.method === "OPTIONS") {
|
||||
return new Response(null, { status: 204, headers: corsHeaders });
|
||||
}
|
||||
const { name } = await req.json();
|
||||
const data = {
|
||||
message: `Hello ${name}!`,
|
||||
};
|
||||
|
||||
return new Response(
|
||||
JSON.stringify(data),
|
||||
{ headers: { "Content-Type": "application/json" } },
|
||||
);
|
||||
try {
|
||||
const SUPABASE_URL = Deno.env.get("SUPABASE_URL");
|
||||
const SUPABASE_ANON_KEY = Deno.env.get("SUPABASE_ANON_KEY");
|
||||
if (!SUPABASE_URL || !SUPABASE_ANON_KEY) {
|
||||
throw new HttpError(
|
||||
500,
|
||||
"Configuración del servidor incompleta.",
|
||||
"MISSING_ENV",
|
||||
{
|
||||
missing: [
|
||||
!SUPABASE_URL ? "SUPABASE_URL" : null,
|
||||
!SUPABASE_ANON_KEY ? "SUPABASE_ANON_KEY" : null,
|
||||
].filter(Boolean),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// If needed for RLS-protected reads, create an anon client with user's JWT
|
||||
// Currently not used; kept here for future expansion.
|
||||
// const supabaseAnon = createClient(SUPABASE_URL, SUPABASE_ANON_KEY, {
|
||||
// global: {
|
||||
// headers: {
|
||||
// Authorization: authHeaderRaw,
|
||||
// },
|
||||
// },
|
||||
// });
|
||||
|
||||
const SERVICE_ROLE_KEY = Deno.env.get("SUPABASE_SERVICE_ROLE_KEY");
|
||||
if (!SERVICE_ROLE_KEY) {
|
||||
throw new HttpError(
|
||||
500,
|
||||
"Configuración del servidor incompleta.",
|
||||
"MISSING_ENV",
|
||||
{ missing: ["SUPABASE_SERVICE_ROLE_KEY"] },
|
||||
);
|
||||
}
|
||||
|
||||
const supabaseService = createClient<Database>(
|
||||
SUPABASE_URL,
|
||||
SERVICE_ROLE_KEY,
|
||||
);
|
||||
|
||||
// INICIO DE CODIGO PARA DEBBUGGING
|
||||
console.log(
|
||||
`[${
|
||||
new Date().toISOString()
|
||||
}][${functionName}]: Request processed successfully`,
|
||||
);
|
||||
const { data: asignatura_debug, error: asignatura_error } =
|
||||
await supabaseService
|
||||
.from("asignaturas")
|
||||
.select("*")
|
||||
.eq("id", "9d4dda6a-488f-428a-8a07-38081592a641")
|
||||
.single();
|
||||
return sendSuccess(asignatura_debug);
|
||||
// FIN DE CODIGO PARA DEBBUGGING
|
||||
} catch (error) {
|
||||
if (error instanceof HttpError) {
|
||||
console.error(
|
||||
`[${new Date().toISOString()}][${functionName}] ⚠️ Handled Error:`,
|
||||
{
|
||||
message: error.message,
|
||||
code: error.code,
|
||||
internalDetails: error.internalDetails || "N/A",
|
||||
},
|
||||
);
|
||||
|
||||
return sendError(error.status, error.message, error.code);
|
||||
}
|
||||
|
||||
const unexpectedError = error instanceof Error
|
||||
? error
|
||||
: new Error(String(error));
|
||||
|
||||
console.error(
|
||||
`[${
|
||||
new Date().toISOString()
|
||||
}][${functionName}] 💥 CRITICAL UNHANDLED ERROR:`,
|
||||
unexpectedError.stack || unexpectedError.message,
|
||||
);
|
||||
|
||||
return sendError(
|
||||
500,
|
||||
"Ocurrió un error inesperado en el servidor.",
|
||||
"INTERNAL_SERVER_ERROR",
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user