metaads-pro/src/app/api/engine/rules/route.ts

68 lines
2.2 KiB
TypeScript
Raw Normal View History

import { NextRequest, NextResponse } from 'next/server';
import { DEFAULT_RULES } from '@/lib/engine/rules';
import type { AutomationRule } from '@/lib/engine/rules';
// In-memory store (replace with Supabase when configured)
let rules: AutomationRule[] = DEFAULT_RULES.map((r, i) => ({
...r,
id: `rule-${i + 1}`,
created_at: new Date().toISOString(),
updated_at: new Date().toISOString(),
}));
export async function GET() {
return NextResponse.json({ success: true, data: rules });
}
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const newRule: AutomationRule = {
...body,
id: `rule-${Date.now()}`,
created_at: new Date().toISOString(),
updated_at: new Date().toISOString(),
};
rules.push(newRule);
return NextResponse.json({ success: true, data: newRule }, { status: 201 });
} catch (error) {
return NextResponse.json(
{ success: false, error: error instanceof Error ? error.message : 'Invalid data' },
{ status: 400 }
);
}
}
export async function PATCH(request: NextRequest) {
try {
const body = await request.json();
const { id, ...updates } = body;
const index = rules.findIndex((r) => r.id === id);
if (index === -1) {
return NextResponse.json({ success: false, error: 'Rule not found' }, { status: 404 });
}
rules[index] = { ...rules[index], ...updates, updated_at: new Date().toISOString() };
return NextResponse.json({ success: true, data: rules[index] });
} catch (error) {
return NextResponse.json(
{ success: false, error: error instanceof Error ? error.message : 'Invalid data' },
{ status: 400 }
);
}
}
export async function DELETE(request: NextRequest) {
try {
const { searchParams } = request.nextUrl;
const id = searchParams.get('id');
if (!id) return NextResponse.json({ success: false, error: 'ID required' }, { status: 400 });
rules = rules.filter((r) => r.id !== id);
return NextResponse.json({ success: true });
} catch (error) {
return NextResponse.json(
{ success: false, error: error instanceof Error ? error.message : 'Error' },
{ status: 500 }
);
}
}