sofia-cloud / src /app /api /automation /route.ts
Gmagl
Fix: All API routes corrected for HF Spaces
acbcb20
raw
history blame
3.45 kB
import { NextRequest, NextResponse } from "next/server";
import { db } from "@/lib/db";
import ZAI from "z-ai-web-dev-sdk";
const AUTOMATION_TYPES: Record<string, { name: string; triggers: string[] }> = {
content_generation: { name: "Generacion de Contenido", triggers: ["schedule", "manual"] },
posting: { name: "Publicacion Automatica", triggers: ["schedule", "manual"] },
trend_tracking: { name: "Seguimiento de Tendencias", triggers: ["schedule", "manual"] }
};
export async function GET(request: NextRequest) {
try {
const { searchParams } = new URL(request.url);
const type = searchParams.get("type");
const where: Record<string, unknown> = {};
if (type) where.type = type;
const automations = await db.automation.findMany({ where, orderBy: { createdAt: "desc" } });
const stats = {
total: await db.automation.count(),
active: await db.automation.count({ where: { isActive: true } })
};
return NextResponse.json({ success: true, automations, automationTypes: AUTOMATION_TYPES, stats });
} catch {
return NextResponse.json({ success: false, error: "Error" }, { status: 500 });
}
}
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const { name, type, trigger, actions } = body;
if (!name || !type || !actions) {
return NextResponse.json({ success: false, error: "Faltan campos" }, { status: 400 });
}
const automation = await db.automation.create({
data: { name, type, trigger: trigger || "manual", actions: JSON.stringify(actions), isActive: true }
});
return NextResponse.json({ success: true, automation });
} catch {
return NextResponse.json({ success: false, error: "Error" }, { status: 500 });
}
}
export async function PUT(request: NextRequest) {
try {
const body = await request.json();
const { id, isActive, action } = body;
if (!id) return NextResponse.json({ success: false, error: "ID requerido" }, { status: 400 });
if (action === "execute") {
const automation = await db.automation.findUnique({ where: { id } });
if (!automation) return NextResponse.json({ success: false, error: "No encontrada" }, { status: 404 });
try {
const zai = await ZAI.create();
await zai.chat.completions.create({ messages: [{ role: "user", content: "test" }] });
} catch {}
await db.automation.update({ where: { id }, data: { lastRunAt: new Date(), runCount: { increment: 1 } } });
await db.automationLog.create({ data: { automationId: id, status: "success", duration: 100 } });
return NextResponse.json({ success: true });
}
const automation = await db.automation.update({ where: { id }, data: { isActive } });
return NextResponse.json({ success: true, automation });
} catch {
return NextResponse.json({ success: false, error: "Error" }, { status: 500 });
}
}
export async function DELETE(request: NextRequest) {
try {
const { searchParams } = new URL(request.url);
const id = searchParams.get("id");
if (!id) return NextResponse.json({ success: false, error: "ID requerido" }, { status: 400 });
await db.automationLog.deleteMany({ where: { automationId: id } });
await db.automation.delete({ where: { id } });
return NextResponse.json({ success: true });
} catch {
return NextResponse.json({ success: false, error: "Error" }, { status: 500 });
}
}