Spaces:
Build error
Build error
File size: 3,445 Bytes
333c51a acbcb20 0119dec 333c51a acbcb20 333c51a acbcb20 333c51a acbcb20 333c51a acbcb20 333c51a acbcb20 333c51a acbcb20 333c51a 0119dec acbcb20 333c51a acbcb20 333c51a acbcb20 333c51a 0119dec acbcb20 0119dec acbcb20 0119dec 333c51a acbcb20 0119dec acbcb20 333c51a 0119dec 333c51a 0119dec acbcb20 333c51a 0119dec | 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 | 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 });
}
} |