Add rules engine with CRUD API, condition/action evaluation, and apply-all endpoint. UI: rule builder form with field/operator/value conditions, tag multi-select, apply button with result stats.
45 lines
1.7 KiB
TypeScript
45 lines
1.7 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { getCurrentUser } from "@/lib/auth";
|
|
import { queryRaw, prisma } from "@/lib/db";
|
|
|
|
export async function PATCH(req: NextRequest, { params }: { params: Promise<{ id: string }> }) {
|
|
const user = await getCurrentUser(req);
|
|
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 403 });
|
|
|
|
const { id } = await params;
|
|
const body = await req.json();
|
|
|
|
const existing = await queryRaw<{ id: number }>(
|
|
`SELECT id FROM rules WHERE id = $1 AND owner_id = $2`,
|
|
[Number(id), user.id]
|
|
);
|
|
if (!existing.length) return NextResponse.json({ error: "Not found" }, { status: 404 });
|
|
|
|
const updated = await prisma.rules.update({
|
|
where: { id: Number(id) },
|
|
data: {
|
|
...(body.name !== undefined && { name: body.name }),
|
|
...(body.conditions !== undefined && { conditions: body.conditions }),
|
|
...(body.actions !== undefined && { actions: body.actions }),
|
|
...(body.enabled !== undefined && { enabled: body.enabled }),
|
|
...(body.priority !== undefined && { priority: body.priority }),
|
|
},
|
|
});
|
|
return NextResponse.json(updated);
|
|
}
|
|
|
|
export async function DELETE(req: NextRequest, { params }: { params: Promise<{ id: string }> }) {
|
|
const user = await getCurrentUser(req);
|
|
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 403 });
|
|
|
|
const { id } = await params;
|
|
const existing = await queryRaw<{ id: number }>(
|
|
`SELECT id FROM rules WHERE id = $1 AND owner_id = $2`,
|
|
[Number(id), user.id]
|
|
);
|
|
if (!existing.length) return NextResponse.json({ error: "Not found" }, { status: 404 });
|
|
|
|
await prisma.rules.delete({ where: { id: Number(id) } });
|
|
return NextResponse.json({ ok: true });
|
|
}
|