154 lines
3.9 KiB
TypeScript
154 lines
3.9 KiB
TypeScript
/**
|
|
* Netlify Function: Listar Atribuições
|
|
* GET /rest/v1/patient_assignments
|
|
*/
|
|
|
|
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": "GET, POST, OPTIONS",
|
|
};
|
|
|
|
if (event.httpMethod === "OPTIONS") {
|
|
return {
|
|
statusCode: 200,
|
|
headers,
|
|
body: "",
|
|
};
|
|
}
|
|
|
|
// GET - Listar atribuições
|
|
if (event.httpMethod === "GET") {
|
|
try {
|
|
const authHeader =
|
|
event.headers.authorization || event.headers.Authorization;
|
|
|
|
if (!authHeader) {
|
|
return {
|
|
statusCode: 401,
|
|
headers,
|
|
body: JSON.stringify({ error: "Token não fornecido" }),
|
|
};
|
|
}
|
|
|
|
// Monta URL com query params (se houver)
|
|
const queryString = event.queryStringParameters
|
|
? "?" +
|
|
new URLSearchParams(
|
|
event.queryStringParameters as Record<string, string>
|
|
).toString()
|
|
: "";
|
|
|
|
const response = await fetch(
|
|
`${SUPABASE_URL}/rest/v1/patient_assignments${queryString}`,
|
|
{
|
|
method: "GET",
|
|
headers: {
|
|
apikey: SUPABASE_ANON_KEY,
|
|
Authorization: authHeader,
|
|
},
|
|
}
|
|
);
|
|
|
|
const data = await response.json();
|
|
|
|
return {
|
|
statusCode: response.status,
|
|
headers: {
|
|
...headers,
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify(data),
|
|
};
|
|
} catch (error) {
|
|
console.error("Erro ao listar atribuições:", error);
|
|
|
|
return {
|
|
statusCode: 500,
|
|
headers,
|
|
body: JSON.stringify({
|
|
error: "Erro interno no servidor",
|
|
message: error instanceof Error ? error.message : "Erro desconhecido",
|
|
}),
|
|
};
|
|
}
|
|
}
|
|
|
|
// POST - Criar atribuição
|
|
if (event.httpMethod === "POST") {
|
|
try {
|
|
const authHeader =
|
|
event.headers.authorization || event.headers.Authorization;
|
|
|
|
if (!authHeader) {
|
|
return {
|
|
statusCode: 401,
|
|
headers,
|
|
body: JSON.stringify({ error: "Token não fornecido" }),
|
|
};
|
|
}
|
|
|
|
const body = JSON.parse(event.body || "{}");
|
|
|
|
if (!body.patient_id || !body.user_id || !body.role) {
|
|
return {
|
|
statusCode: 400,
|
|
headers,
|
|
body: JSON.stringify({
|
|
error: "patient_id, user_id e role são obrigatórios",
|
|
}),
|
|
};
|
|
}
|
|
|
|
const response = await fetch(
|
|
`${SUPABASE_URL}/rest/v1/patient_assignments`,
|
|
{
|
|
method: "POST",
|
|
headers: {
|
|
apikey: SUPABASE_ANON_KEY,
|
|
Authorization: authHeader,
|
|
"Content-Type": "application/json",
|
|
Prefer: "return=representation",
|
|
},
|
|
body: JSON.stringify(body),
|
|
}
|
|
);
|
|
|
|
const data = await response.json();
|
|
|
|
return {
|
|
statusCode: response.status,
|
|
headers: {
|
|
...headers,
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify(data),
|
|
};
|
|
} catch (error) {
|
|
console.error("Erro ao criar atribuição:", error);
|
|
|
|
return {
|
|
statusCode: 500,
|
|
headers,
|
|
body: JSON.stringify({
|
|
error: "Erro interno no servidor",
|
|
message: error instanceof Error ? error.message : "Erro desconhecido",
|
|
}),
|
|
};
|
|
}
|
|
}
|
|
|
|
return {
|
|
statusCode: 405,
|
|
headers,
|
|
body: JSON.stringify({ error: "Method Not Allowed" }),
|
|
};
|
|
};
|