Spaces:
Sleeping
Sleeping
File size: 3,599 Bytes
89ccd89 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | import { NextRequest, NextResponse } from "next/server";
import { createClient } from "@/lib/supabase/server";
// GET — list custom rules
export async function GET() {
const supabase = await createClient();
const { data: { user } } = await supabase.auth.getUser();
if (!user) return NextResponse.json({ error: "Not authenticated" }, { status: 401 });
const { data: profile } = await supabase.from("profiles").select("plan, team_id").eq("id", user.id).single();
if (profile?.plan !== "team") return NextResponse.json({ error: "Custom rules require Team plan" }, { status: 403 });
// Fetch user's own rules + team rules
let query = supabase.from("custom_rules").select("*").order("created_at", { ascending: false });
if (profile?.team_id) {
query = query.or(`user_id.eq.${user.id},team_id.eq.${profile.team_id}`);
} else {
query = query.eq("user_id", user.id);
}
const { data: rules } = await query;
return NextResponse.json({ rules: rules || [] });
}
// POST — create a custom rule
export async function POST(req: NextRequest) {
const supabase = await createClient();
const { data: { user } } = await supabase.auth.getUser();
if (!user) return NextResponse.json({ error: "Not authenticated" }, { status: 401 });
const { data: profile } = await supabase.from("profiles").select("plan, team_id").eq("id", user.id).single();
if (profile?.plan !== "team") return NextResponse.json({ error: "Custom rules require Team plan" }, { status: 403 });
const { name, description, pattern, severity, category } = await req.json();
if (!name || !pattern || !category) {
return NextResponse.json({ error: "name, pattern, and category are required" }, { status: 400 });
}
// Validate regex pattern
try { new RegExp(pattern, "i"); } catch {
return NextResponse.json({ error: "Invalid regex pattern" }, { status: 400 });
}
const { data: rule, error } = await supabase.from("custom_rules").insert({
user_id: user.id,
team_id: profile?.team_id || null,
name,
description: description || null,
pattern,
severity: severity || "MEDIUM",
category,
}).select().single();
if (error) return NextResponse.json({ error: error.message }, { status: 500 });
return NextResponse.json({ rule });
}
// PUT — update a rule
export async function PUT(req: NextRequest) {
const supabase = await createClient();
const { data: { user } } = await supabase.auth.getUser();
if (!user) return NextResponse.json({ error: "Not authenticated" }, { status: 401 });
const { id, ...updates } = await req.json();
if (updates.pattern) {
try { new RegExp(updates.pattern, "i"); } catch {
return NextResponse.json({ error: "Invalid regex pattern" }, { status: 400 });
}
}
const { error } = await supabase.from("custom_rules")
.update({ ...updates, updated_at: new Date().toISOString() })
.eq("id", id)
.eq("user_id", user.id);
if (error) return NextResponse.json({ error: error.message }, { status: 500 });
return NextResponse.json({ success: true });
}
// DELETE — delete a rule
export async function DELETE(req: NextRequest) {
const supabase = await createClient();
const { data: { user } } = await supabase.auth.getUser();
if (!user) return NextResponse.json({ error: "Not authenticated" }, { status: 401 });
const { id } = await req.json();
const { error } = await supabase.from("custom_rules").delete().eq("id", id).eq("user_id", user.id);
if (error) return NextResponse.json({ error: error.message }, { status: 500 });
return NextResponse.json({ success: true });
}
|