Roadbound-BRR/supabase/functions/org-list/index.ts

32 lines
940 B
TypeScript

import { fail, handleOptions, json } from "../_shared/http.ts";
import { requireUser } from "../_shared/supabase.ts";
Deno.serve(async (req) => {
const preflight = handleOptions(req);
if (preflight) return preflight;
if (req.method !== "GET" && req.method !== "POST") {
return fail("Method not allowed", 405);
}
const { client, user, error: userError } = await requireUser(req);
if (!user) return fail(userError ?? "Unauthorized", 401);
const { data, error } = await client
.from("organization_members")
.select(
"role, joined_at, organizations(id, name, slug, icon_url, owner_user_id, created_at)",
)
.eq("user_id", user.id)
.order("joined_at", { ascending: true });
if (error) return fail(error.message, 400);
return json({
organizations: (data ?? []).map((row) => ({
role: row.role,
joined_at: row.joined_at,
organization: row.organizations,
})),
});
});