98 lines
2.7 KiB
TypeScript
98 lines
2.7 KiB
TypeScript
/**
|
|
* Netlify Function: Register Patient (Public)
|
|
* POST /register-patient - Registro público de paciente
|
|
* Não requer autenticação - função pública
|
|
* Validações rigorosas (CPF, rate limiting, rollback)
|
|
*/
|
|
|
|
import type { Handler, HandlerEvent } from "@netlify/functions";
|
|
|
|
const SUPABASE_URL = "https://yuanqfswhberkoevtmfr.supabase.co";
|
|
const SUPABASE_ANON_KEY =
|
|
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Inl1YW5xZnN3aGJlcmtvZXZ0bWZyIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTQ5NTQzNjksImV4cCI6MjA3MDUzMDM2OX0.g8Fm4XAvtX46zifBZnYVH4tVuQkqUH6Ia9CXQj4DztQ";
|
|
|
|
export const handler: Handler = async (event: HandlerEvent) => {
|
|
const headers = {
|
|
"Access-Control-Allow-Origin": "*",
|
|
"Access-Control-Allow-Headers": "Content-Type, Authorization",
|
|
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
|
};
|
|
|
|
if (event.httpMethod === "OPTIONS") {
|
|
return {
|
|
statusCode: 200,
|
|
headers,
|
|
body: "",
|
|
};
|
|
}
|
|
|
|
try {
|
|
if (event.httpMethod === "POST") {
|
|
const body = JSON.parse(event.body || "{}");
|
|
|
|
console.log(
|
|
"[register-patient] Recebido body:",
|
|
JSON.stringify(body, null, 2)
|
|
);
|
|
|
|
// Validação dos campos obrigatórios
|
|
if (!body.email || !body.full_name || !body.cpf || !body.phone_mobile) {
|
|
return {
|
|
statusCode: 400,
|
|
headers,
|
|
body: JSON.stringify({
|
|
error: "Campos obrigatórios: email, full_name, cpf, phone_mobile",
|
|
}),
|
|
};
|
|
}
|
|
|
|
// Chama a Edge Function pública do Supabase
|
|
const response = await fetch(
|
|
`${SUPABASE_URL}/functions/v1/register-patient`,
|
|
{
|
|
method: "POST",
|
|
headers: {
|
|
apikey: SUPABASE_ANON_KEY,
|
|
Authorization: `Bearer ${SUPABASE_ANON_KEY}`,
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify(body),
|
|
}
|
|
);
|
|
|
|
const data = await response.json();
|
|
|
|
console.log("[register-patient] Resposta do Supabase:", {
|
|
status: response.status,
|
|
data: JSON.stringify(data, null, 2),
|
|
});
|
|
|
|
return {
|
|
statusCode: response.status,
|
|
headers: {
|
|
...headers,
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify(data),
|
|
};
|
|
}
|
|
|
|
return {
|
|
statusCode: 405,
|
|
headers,
|
|
body: JSON.stringify({ error: "Method Not Allowed" }),
|
|
};
|
|
} catch (error) {
|
|
console.error("[register-patient] Erro na API:", error);
|
|
|
|
return {
|
|
statusCode: 500,
|
|
headers,
|
|
body: JSON.stringify({
|
|
error: "Erro interno do servidor",
|
|
message: error instanceof Error ? error.message : "Erro desconhecido",
|
|
}),
|
|
};
|
|
}
|
|
};
|