48 lines
1.2 KiB
TypeScript

import { serve } from "https://deno.land/std@0.168.0/http/server.ts";
import { mydb } from "../../lib/mySupabase.ts";
import { corsHeaders, jsonResponse, errorResponse } from "../../lib/utils.ts";
import { validateAuth, hasPermission } from "../../lib/auth.ts";
serve(async (req) => {
if (req.method === "OPTIONS") {
return new Response("ok", { headers: corsHeaders() });
}
try {
const auth = await validateAuth(req);
if (!auth) {
return errorResponse("Não autorizado", 401);
}
if (req.method === "GET") {
const res = await mydb
.from("access_log")
.select("*")
.eq("user_id", auth.userId)
.order("created_at", { ascending: false });
return jsonResponse({ logs: res.data || [] });
}
if (req.method === "POST") {
const body = await req.json();
const res = await mydb
.from("consent_requests")
.insert({
user_id: auth.userId,
type: body.type,
status: "pending",
})
.select();
return jsonResponse({ request_id: res.data?.[0]?.id });
}
return errorResponse("Method not allowed", 405);
} catch (error: unknown) {
const err = error as Error;
return errorResponse(err.message, 500);
}
});