Files
2026-08-25 10:56:46 +07:00

58 lines
1.7 KiB
TypeScript

import { createAdminClient } from "npm:@insforge/sdk";
const corsHeaders = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
};
export default async function (req: Request): Promise<Response> {
if (req.method === "OPTIONS") {
return new Response(null, { status: 204, headers: corsHeaders });
}
if (req.method !== "POST") {
return new Response(JSON.stringify({ error: "Method not allowed" }), {
status: 405,
headers: { ...corsHeaders, "Content-Type": "application/json" },
});
}
try {
const { id, approved } = await req.json();
if (typeof id !== "string" || id.length === 0) {
return new Response(JSON.stringify({ error: "ID user tidak valid." }), {
status: 400,
headers: { ...corsHeaders, "Content-Type": "application/json" },
});
}
const admin = createAdminClient({
baseUrl: Deno.env.get("INSFORGE_BASE_URL")!,
apiKey: Deno.env.get("INSFORGE_API_KEY")!,
});
const { error } = await admin.database
.from("app_users")
.update({ approved: approved === true })
.eq("id", id);
if (error) {
return new Response(JSON.stringify({ error: error.message }), {
status: 500,
headers: { ...corsHeaders, "Content-Type": "application/json" },
});
}
return new Response(JSON.stringify({ ok: true }), {
status: 200,
headers: { ...corsHeaders, "Content-Type": "application/json" },
});
} catch (err) {
return new Response(
JSON.stringify({ error: err instanceof Error ? err.message : "Unknown error" }),
{ status: 500, headers: { ...corsHeaders, "Content-Type": "application/json" } }
);
}
}