sofia-cloud / src /app /api /automation /route.ts
Gmagl
feat: complete Telegram AI Girlfriend mechanics & deduplication
b8b98dd
Raw
History Blame Contribute Delete
3.93 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, any> = {};
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 payload = JSON.parse(automation.actions as string);
const webhookUrl = payload.webhookUrl || process.env.N8N_WEBHOOK_URL;
if (webhookUrl) {
// Trigger n8n workflow
await fetch(webhookUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ automationId: automation.id, type: automation.type, payload })
});
} else {
console.warn("No n8n Webhook URL configured for this automation.");
}
} catch (err) {
console.error("Failed to execute n8n webhook", err);
}
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 });
}
}