52 lines
1.6 KiB
TypeScript
52 lines
1.6 KiB
TypeScript
import { fail, handleOptions, json } from "../_shared/http.ts";
|
|
import { createServiceClient, requireUser } from "../_shared/supabase.ts";
|
|
|
|
Deno.serve(async (req) => {
|
|
const preflight = handleOptions(req);
|
|
if (preflight) return preflight;
|
|
|
|
if (req.method !== "POST") return fail("Method not allowed", 405);
|
|
|
|
const { user, error: userError } = await requireUser(req);
|
|
if (!user) return fail(userError ?? "Unauthorized", 401);
|
|
const serviceClient = createServiceClient();
|
|
|
|
let body: {
|
|
channel_id?: string;
|
|
};
|
|
try {
|
|
body = await req.json();
|
|
} catch {
|
|
return fail("Invalid JSON body");
|
|
}
|
|
|
|
const channelId = (body.channel_id ?? "").trim();
|
|
if (!channelId) return fail("channel_id is required");
|
|
|
|
const { data: channel, error: channelError } = await serviceClient
|
|
.from("channels")
|
|
.select("id, organization_id")
|
|
.eq("id", channelId)
|
|
.maybeSingle();
|
|
if (channelError) return fail(channelError.message, 400);
|
|
if (!channel) return fail("channel not found", 404);
|
|
|
|
const { data: member, error: roleError } = await serviceClient
|
|
.from("organization_members")
|
|
.select("role")
|
|
.eq("organization_id", channel.organization_id)
|
|
.eq("user_id", user.id)
|
|
.maybeSingle();
|
|
if (roleError) return fail(roleError.message, 400);
|
|
if (!member || !["owner", "admin"].includes(member.role)) {
|
|
return fail("forbidden", 403);
|
|
}
|
|
|
|
const { error: deleteError } = await serviceClient
|
|
.from("channels")
|
|
.delete()
|
|
.eq("id", channelId);
|
|
if (deleteError) return fail(deleteError.message, 400);
|
|
|
|
return json({ ok: true });
|
|
});
|