Spaces:
Build error
Build error
Gmagl commited on
Commit 路
0119dec
1
Parent(s): 613325e
Fix: Simplified automation API
Browse files- src/app/api/automation/route.ts +49 -303
src/app/api/automation/route.ts
CHANGED
|
@@ -2,46 +2,16 @@ import { NextRequest, NextResponse } from "next/server";
|
|
| 2 |
import { db } from "@/lib/db";
|
| 3 |
import ZAI from "z-ai-web-dev-sdk";
|
| 4 |
|
| 5 |
-
// Tipos de automatizaci贸n disponibles
|
| 6 |
const AUTOMATION_TYPES = {
|
| 7 |
-
content_generation: {
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
},
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
description: "Publica contenido en horarios 贸ptimos",
|
| 15 |
-
triggers: ["schedule", "content_ready", "manual"]
|
| 16 |
-
},
|
| 17 |
-
engagement: {
|
| 18 |
-
name: "Engagement Autom谩tico",
|
| 19 |
-
description: "Responde comentarios y mensajes autom谩ticamente",
|
| 20 |
-
triggers: ["new_comment", "new_message", "schedule"]
|
| 21 |
-
},
|
| 22 |
-
monetization: {
|
| 23 |
-
name: "Optimizaci贸n de Monetizaci贸n",
|
| 24 |
-
description: "Ajusta precios y ofertas basado en analytics",
|
| 25 |
-
triggers: ["analytics_update", "subscriber_change", "schedule"]
|
| 26 |
-
},
|
| 27 |
-
storytelling: {
|
| 28 |
-
name: "Storytelling Continuo",
|
| 29 |
-
description: "Contin煤a historias autom谩ticamente con cliffhangers",
|
| 30 |
-
triggers: ["episode_published", "engagement_threshold", "schedule"]
|
| 31 |
-
},
|
| 32 |
-
cross_posting: {
|
| 33 |
-
name: "Cross-Posting",
|
| 34 |
-
description: "Publica el mismo contenido en m煤ltiples plataformas",
|
| 35 |
-
triggers: ["content_published", "schedule", "manual"]
|
| 36 |
-
},
|
| 37 |
-
trend_tracking: {
|
| 38 |
-
name: "Seguimiento de Tendencias",
|
| 39 |
-
description: "Detecta tendencias y sugiere contenido",
|
| 40 |
-
triggers: ["schedule", "manual"]
|
| 41 |
-
}
|
| 42 |
};
|
| 43 |
|
| 44 |
-
// GET - Listar automatizaciones
|
| 45 |
export async function GET(request: NextRequest) {
|
| 46 |
try {
|
| 47 |
const { searchParams } = new URL(request.url);
|
|
@@ -54,83 +24,36 @@ export async function GET(request: NextRequest) {
|
|
| 54 |
|
| 55 |
const automations = await db.automation.findMany({
|
| 56 |
where,
|
| 57 |
-
include: {
|
| 58 |
-
_count: {
|
| 59 |
-
select: { logs: true }
|
| 60 |
-
}
|
| 61 |
-
},
|
| 62 |
orderBy: { createdAt: "desc" }
|
| 63 |
});
|
| 64 |
|
| 65 |
-
// 脷ltimos logs
|
| 66 |
const recentLogs = await db.automationLog.findMany({
|
| 67 |
orderBy: { createdAt: "desc" },
|
| 68 |
take: 20,
|
| 69 |
});
|
| 70 |
|
| 71 |
-
// Estad铆sticas
|
| 72 |
const stats = {
|
| 73 |
total: await db.automation.count(),
|
| 74 |
active: await db.automation.count({ where: { isActive: true } }),
|
| 75 |
runsToday: await db.automationLog.count({
|
| 76 |
-
where: {
|
| 77 |
-
|
| 78 |
-
gte: new Date(new Date().setHours(0, 0, 0, 0))
|
| 79 |
-
}
|
| 80 |
-
}
|
| 81 |
-
}),
|
| 82 |
-
successRate: await calculateSuccessRate()
|
| 83 |
};
|
| 84 |
|
| 85 |
-
return NextResponse.json({
|
| 86 |
-
success: true,
|
| 87 |
-
automations,
|
| 88 |
-
automationTypes: AUTOMATION_TYPES,
|
| 89 |
-
recentLogs,
|
| 90 |
-
stats
|
| 91 |
-
});
|
| 92 |
-
|
| 93 |
} catch (error) {
|
| 94 |
console.error("Error fetching automations:", error);
|
| 95 |
-
return NextResponse.json(
|
| 96 |
-
{ success: false, error: "Error al obtener automatizaciones" },
|
| 97 |
-
{ status: 500 }
|
| 98 |
-
);
|
| 99 |
}
|
| 100 |
}
|
| 101 |
|
| 102 |
-
// POST - Crear automatizaci贸n
|
| 103 |
export async function POST(request: NextRequest) {
|
| 104 |
try {
|
| 105 |
const body = await request.json();
|
| 106 |
-
const {
|
| 107 |
-
name,
|
| 108 |
-
description,
|
| 109 |
-
type,
|
| 110 |
-
trigger,
|
| 111 |
-
triggerConfig,
|
| 112 |
-
actions
|
| 113 |
-
} = body;
|
| 114 |
|
| 115 |
if (!name || !type || !trigger || !actions) {
|
| 116 |
-
return NextResponse.json(
|
| 117 |
-
{ success: false, error: "Faltan campos requeridos: name, type, trigger, actions" },
|
| 118 |
-
{ status: 400 }
|
| 119 |
-
);
|
| 120 |
-
}
|
| 121 |
-
|
| 122 |
-
// Validar tipo de automatizaci贸n
|
| 123 |
-
if (!AUTOMATION_TYPES[type as keyof typeof AUTOMATION_TYPES]) {
|
| 124 |
-
return NextResponse.json(
|
| 125 |
-
{ success: false, error: "Tipo de automatizaci贸n no v谩lido" },
|
| 126 |
-
{ status: 400 }
|
| 127 |
-
);
|
| 128 |
-
}
|
| 129 |
-
|
| 130 |
-
// Calcular pr贸xima ejecuci贸n si es programada
|
| 131 |
-
let nextRunAt = null;
|
| 132 |
-
if (trigger === "schedule" && triggerConfig?.schedule) {
|
| 133 |
-
nextRunAt = calculateNextRun(triggerConfig.schedule);
|
| 134 |
}
|
| 135 |
|
| 136 |
const automation = await db.automation.create({
|
|
@@ -141,256 +64,79 @@ export async function POST(request: NextRequest) {
|
|
| 141 |
trigger,
|
| 142 |
triggerConfig: triggerConfig ? JSON.stringify(triggerConfig) : null,
|
| 143 |
actions: JSON.stringify(actions),
|
| 144 |
-
nextRunAt,
|
| 145 |
isActive: true,
|
| 146 |
}
|
| 147 |
});
|
| 148 |
|
| 149 |
-
return NextResponse.json({
|
| 150 |
-
success: true,
|
| 151 |
-
automation,
|
| 152 |
-
message: `Automatizaci贸n "${name}" creada`
|
| 153 |
-
});
|
| 154 |
-
|
| 155 |
} catch (error) {
|
| 156 |
console.error("Error creating automation:", error);
|
| 157 |
-
return NextResponse.json(
|
| 158 |
-
{ success: false, error: "Error al crear automatizaci贸n" },
|
| 159 |
-
{ status: 500 }
|
| 160 |
-
);
|
| 161 |
}
|
| 162 |
}
|
| 163 |
|
| 164 |
-
// PUT - Ejecutar o actualizar automatizaci贸n
|
| 165 |
export async function PUT(request: NextRequest) {
|
| 166 |
try {
|
| 167 |
const body = await request.json();
|
| 168 |
const { id, action, isActive, triggerConfig, actions } = body;
|
| 169 |
|
| 170 |
if (!id) {
|
| 171 |
-
return NextResponse.json(
|
| 172 |
-
{ success: false, error: "ID requerido" },
|
| 173 |
-
{ status: 400 }
|
| 174 |
-
);
|
| 175 |
}
|
| 176 |
|
| 177 |
-
// Si es una acci贸n de ejecuci贸n
|
| 178 |
if (action === "execute") {
|
| 179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
}
|
| 181 |
|
| 182 |
-
// Actualizaci贸n normal
|
| 183 |
const updateData: Record<string, unknown> = {};
|
| 184 |
if (isActive !== undefined) updateData.isActive = isActive;
|
| 185 |
if (triggerConfig) updateData.triggerConfig = JSON.stringify(triggerConfig);
|
| 186 |
if (actions) updateData.actions = JSON.stringify(actions);
|
| 187 |
|
| 188 |
-
const automation = await db.automation.update({
|
| 189 |
-
|
| 190 |
-
data: updateData
|
| 191 |
-
});
|
| 192 |
-
|
| 193 |
-
return NextResponse.json({
|
| 194 |
-
success: true,
|
| 195 |
-
automation
|
| 196 |
-
});
|
| 197 |
-
|
| 198 |
} catch (error) {
|
| 199 |
console.error("Error updating automation:", error);
|
| 200 |
-
return NextResponse.json(
|
| 201 |
-
{ success: false, error: "Error al actualizar automatizaci贸n" },
|
| 202 |
-
{ status: 500 }
|
| 203 |
-
);
|
| 204 |
}
|
| 205 |
}
|
| 206 |
|
| 207 |
-
// DELETE - Eliminar automatizaci贸n
|
| 208 |
export async function DELETE(request: NextRequest) {
|
| 209 |
try {
|
| 210 |
const { searchParams } = new URL(request.url);
|
| 211 |
const id = searchParams.get("id");
|
| 212 |
|
| 213 |
-
if (!id) {
|
| 214 |
-
return NextResponse.json(
|
| 215 |
-
{ success: false, error: "ID requerido" },
|
| 216 |
-
{ status: 400 }
|
| 217 |
-
);
|
| 218 |
-
}
|
| 219 |
|
| 220 |
-
|
| 221 |
-
await db.
|
| 222 |
-
where: { automationId: id }
|
| 223 |
-
});
|
| 224 |
-
|
| 225 |
-
await db.automation.delete({
|
| 226 |
-
where: { id }
|
| 227 |
-
});
|
| 228 |
-
|
| 229 |
-
return NextResponse.json({
|
| 230 |
-
success: true,
|
| 231 |
-
message: "Automatizaci贸n eliminada"
|
| 232 |
-
});
|
| 233 |
|
|
|
|
| 234 |
} catch (error) {
|
| 235 |
console.error("Error deleting automation:", error);
|
| 236 |
-
return NextResponse.json(
|
| 237 |
-
{ success: false, error: "Error al eliminar automatizaci贸n" },
|
| 238 |
-
{ status: 500 }
|
| 239 |
-
);
|
| 240 |
-
}
|
| 241 |
-
}
|
| 242 |
-
|
| 243 |
-
// Funci贸n para ejecutar una automatizaci贸n
|
| 244 |
-
async function executeAutomation(automationId: string) {
|
| 245 |
-
const startTime = Date.now();
|
| 246 |
-
|
| 247 |
-
try {
|
| 248 |
-
const automation = await db.automation.findUnique({
|
| 249 |
-
where: { id: automationId }
|
| 250 |
-
});
|
| 251 |
-
|
| 252 |
-
if (!automation) {
|
| 253 |
-
return NextResponse.json(
|
| 254 |
-
{ success: false, error: "Automatizaci贸n no encontrada" },
|
| 255 |
-
{ status: 404 }
|
| 256 |
-
);
|
| 257 |
-
}
|
| 258 |
-
|
| 259 |
-
const actions = JSON.parse(automation.actions);
|
| 260 |
-
const results: unknown[] = [];
|
| 261 |
-
|
| 262 |
-
// Ejecutar cada acci贸n
|
| 263 |
-
for (const action of actions) {
|
| 264 |
-
const result = await executeAction(action);
|
| 265 |
-
results.push(result);
|
| 266 |
-
}
|
| 267 |
-
|
| 268 |
-
// Actualizar 煤ltima ejecuci贸n
|
| 269 |
-
await db.automation.update({
|
| 270 |
-
where: { id: automationId },
|
| 271 |
-
data: {
|
| 272 |
-
lastRunAt: new Date(),
|
| 273 |
-
runCount: { increment: 1 },
|
| 274 |
-
nextRunAt: automation.trigger === "schedule"
|
| 275 |
-
? calculateNextRun(JSON.parse(automation.triggerConfig || "{}").schedule)
|
| 276 |
-
: null
|
| 277 |
-
}
|
| 278 |
-
});
|
| 279 |
-
|
| 280 |
-
// Crear log de 茅xito
|
| 281 |
-
await db.automationLog.create({
|
| 282 |
-
data: {
|
| 283 |
-
automationId,
|
| 284 |
-
status: "success",
|
| 285 |
-
input: automation.actions,
|
| 286 |
-
output: JSON.stringify(results),
|
| 287 |
-
duration: Date.now() - startTime
|
| 288 |
-
}
|
| 289 |
-
});
|
| 290 |
-
|
| 291 |
-
return NextResponse.json({
|
| 292 |
-
success: true,
|
| 293 |
-
results,
|
| 294 |
-
duration: Date.now() - startTime
|
| 295 |
-
});
|
| 296 |
-
|
| 297 |
-
} catch (error) {
|
| 298 |
-
// Crear log de error
|
| 299 |
-
await db.automationLog.create({
|
| 300 |
-
data: {
|
| 301 |
-
automationId,
|
| 302 |
-
status: "failed",
|
| 303 |
-
error: String(error),
|
| 304 |
-
duration: Date.now() - startTime
|
| 305 |
-
}
|
| 306 |
-
});
|
| 307 |
-
|
| 308 |
-
return NextResponse.json(
|
| 309 |
-
{ success: false, error: "Error al ejecutar automatizaci贸n" },
|
| 310 |
-
{ status: 500 }
|
| 311 |
-
);
|
| 312 |
}
|
| 313 |
-
}
|
| 314 |
-
|
| 315 |
-
// Ejecutar acci贸n individual
|
| 316 |
-
async function executeAction(action: Record<string, unknown>): Promise<unknown> {
|
| 317 |
-
const zai = await ZAI.create();
|
| 318 |
-
|
| 319 |
-
switch (action.type) {
|
| 320 |
-
case "generate_content":
|
| 321 |
-
// Generar contenido con IA
|
| 322 |
-
const completion = await zai.chat.completions.create({
|
| 323 |
-
messages: [
|
| 324 |
-
{ role: "system", content: "Eres un generador de contenido para redes sociales." },
|
| 325 |
-
{ role: "user", content: String(action.prompt) }
|
| 326 |
-
]
|
| 327 |
-
});
|
| 328 |
-
return { type: "content", result: completion.choices[0]?.message?.content };
|
| 329 |
-
|
| 330 |
-
case "create_post":
|
| 331 |
-
// Crear post programado
|
| 332 |
-
const post = await db.post.create({
|
| 333 |
-
data: {
|
| 334 |
-
title: String(action.title) || "Auto-generated",
|
| 335 |
-
caption: String(action.caption) || "",
|
| 336 |
-
type: String(action.postType) || "post",
|
| 337 |
-
status: "draft"
|
| 338 |
-
}
|
| 339 |
-
});
|
| 340 |
-
return { type: "post", postId: post.id };
|
| 341 |
-
|
| 342 |
-
case "check_trends":
|
| 343 |
-
// Simular detecci贸n de tendencias
|
| 344 |
-
return { type: "trends", trends: ["trending1", "trending2"] };
|
| 345 |
-
|
| 346 |
-
case "send_notification":
|
| 347 |
-
return { type: "notification", sent: true };
|
| 348 |
-
|
| 349 |
-
default:
|
| 350 |
-
return { type: action.type, status: "executed" };
|
| 351 |
-
}
|
| 352 |
-
}
|
| 353 |
-
|
| 354 |
-
// Calcular pr贸xima ejecuci贸n
|
| 355 |
-
function calculateNextRun(schedule: string): Date {
|
| 356 |
-
const now = new Date();
|
| 357 |
-
|
| 358 |
-
// Parsear schedule simple (ej: "daily:09:00", "hourly", "weekly:monday:10:00")
|
| 359 |
-
if (schedule.startsWith("daily:")) {
|
| 360 |
-
const time = schedule.split(":")[1];
|
| 361 |
-
const [hours, minutes] = time.split("-").length > 1
|
| 362 |
-
? time.split("-")[0].split(":").map(Number)
|
| 363 |
-
: time.split(":").map(Number);
|
| 364 |
-
|
| 365 |
-
const next = new Date(now);
|
| 366 |
-
next.setHours(hours || 9, minutes || 0, 0, 0);
|
| 367 |
-
if (next <= now) next.setDate(next.getDate() + 1);
|
| 368 |
-
return next;
|
| 369 |
-
}
|
| 370 |
-
|
| 371 |
-
if (schedule === "hourly") {
|
| 372 |
-
return new Date(now.getTime() + 60 * 60 * 1000);
|
| 373 |
-
}
|
| 374 |
-
|
| 375 |
-
if (schedule === "daily") {
|
| 376 |
-
const next = new Date(now);
|
| 377 |
-
next.setDate(next.getDate() + 1);
|
| 378 |
-
next.setHours(9, 0, 0, 0);
|
| 379 |
-
return next;
|
| 380 |
-
}
|
| 381 |
-
|
| 382 |
-
// Default: 1 hora
|
| 383 |
-
return new Date(now.getTime() + 60 * 60 * 1000);
|
| 384 |
-
}
|
| 385 |
-
|
| 386 |
-
// Calcular tasa de 茅xito
|
| 387 |
-
async function calculateSuccessRate(): Promise<number> {
|
| 388 |
-
const total = await db.automationLog.count();
|
| 389 |
-
if (total === 0) return 100;
|
| 390 |
-
|
| 391 |
-
const success = await db.automationLog.count({
|
| 392 |
-
where: { status: "success" }
|
| 393 |
-
});
|
| 394 |
-
|
| 395 |
-
return Math.round((success / total) * 100);
|
| 396 |
-
}
|
|
|
|
| 2 |
import { db } from "@/lib/db";
|
| 3 |
import ZAI from "z-ai-web-dev-sdk";
|
| 4 |
|
|
|
|
| 5 |
const AUTOMATION_TYPES = {
|
| 6 |
+
content_generation: { name: "Generacion de Contenido", triggers: ["schedule", "manual"] },
|
| 7 |
+
posting: { name: "Publicacion Automatica", triggers: ["schedule", "manual"] },
|
| 8 |
+
engagement: { name: "Engagement Automatico", triggers: ["schedule", "manual"] },
|
| 9 |
+
monetization: { name: "Optimizacion de Monetizacion", triggers: ["schedule", "manual"] },
|
| 10 |
+
storytelling: { name: "Storytelling Continuo", triggers: ["schedule", "manual"] },
|
| 11 |
+
cross_posting: { name: "Cross-Posting", triggers: ["schedule", "manual"] },
|
| 12 |
+
trend_tracking: { name: "Seguimiento de Tendencias", triggers: ["schedule", "manual"] }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
};
|
| 14 |
|
|
|
|
| 15 |
export async function GET(request: NextRequest) {
|
| 16 |
try {
|
| 17 |
const { searchParams } = new URL(request.url);
|
|
|
|
| 24 |
|
| 25 |
const automations = await db.automation.findMany({
|
| 26 |
where,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
orderBy: { createdAt: "desc" }
|
| 28 |
});
|
| 29 |
|
|
|
|
| 30 |
const recentLogs = await db.automationLog.findMany({
|
| 31 |
orderBy: { createdAt: "desc" },
|
| 32 |
take: 20,
|
| 33 |
});
|
| 34 |
|
|
|
|
| 35 |
const stats = {
|
| 36 |
total: await db.automation.count(),
|
| 37 |
active: await db.automation.count({ where: { isActive: true } }),
|
| 38 |
runsToday: await db.automationLog.count({
|
| 39 |
+
where: { createdAt: { gte: new Date(new Date().setHours(0, 0, 0, 0)) } }
|
| 40 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
};
|
| 42 |
|
| 43 |
+
return NextResponse.json({ success: true, automations, automationTypes: AUTOMATION_TYPES, recentLogs, stats });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
} catch (error) {
|
| 45 |
console.error("Error fetching automations:", error);
|
| 46 |
+
return NextResponse.json({ success: false, error: "Error al obtener automatizaciones" }, { status: 500 });
|
|
|
|
|
|
|
|
|
|
| 47 |
}
|
| 48 |
}
|
| 49 |
|
|
|
|
| 50 |
export async function POST(request: NextRequest) {
|
| 51 |
try {
|
| 52 |
const body = await request.json();
|
| 53 |
+
const { name, description, type, trigger, triggerConfig, actions } = body;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
if (!name || !type || !trigger || !actions) {
|
| 56 |
+
return NextResponse.json({ success: false, error: "Faltan campos requeridos" }, { status: 400 });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
}
|
| 58 |
|
| 59 |
const automation = await db.automation.create({
|
|
|
|
| 64 |
trigger,
|
| 65 |
triggerConfig: triggerConfig ? JSON.stringify(triggerConfig) : null,
|
| 66 |
actions: JSON.stringify(actions),
|
|
|
|
| 67 |
isActive: true,
|
| 68 |
}
|
| 69 |
});
|
| 70 |
|
| 71 |
+
return NextResponse.json({ success: true, automation });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
} catch (error) {
|
| 73 |
console.error("Error creating automation:", error);
|
| 74 |
+
return NextResponse.json({ success: false, error: "Error al crear automatizacion" }, { status: 500 });
|
|
|
|
|
|
|
|
|
|
| 75 |
}
|
| 76 |
}
|
| 77 |
|
|
|
|
| 78 |
export async function PUT(request: NextRequest) {
|
| 79 |
try {
|
| 80 |
const body = await request.json();
|
| 81 |
const { id, action, isActive, triggerConfig, actions } = body;
|
| 82 |
|
| 83 |
if (!id) {
|
| 84 |
+
return NextResponse.json({ success: false, error: "ID requerido" }, { status: 400 });
|
|
|
|
|
|
|
|
|
|
| 85 |
}
|
| 86 |
|
|
|
|
| 87 |
if (action === "execute") {
|
| 88 |
+
const automation = await db.automation.findUnique({ where: { id } });
|
| 89 |
+
if (!automation) return NextResponse.json({ success: false, error: "No encontrada" }, { status: 404 });
|
| 90 |
+
|
| 91 |
+
const actionsList = JSON.parse(automation.actions);
|
| 92 |
+
const zai = await ZAI.create();
|
| 93 |
+
|
| 94 |
+
for (const act of actionsList) {
|
| 95 |
+
if (act.type === "generate_content") {
|
| 96 |
+
await zai.chat.completions.create({
|
| 97 |
+
messages: [{ role: "user", content: String(act.prompt) }]
|
| 98 |
+
});
|
| 99 |
+
}
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
await db.automation.update({
|
| 103 |
+
where: { id },
|
| 104 |
+
data: { lastRunAt: new Date(), runCount: { increment: 1 } }
|
| 105 |
+
});
|
| 106 |
+
|
| 107 |
+
await db.automationLog.create({
|
| 108 |
+
data: { automationId: id, status: "success", duration: 100 }
|
| 109 |
+
});
|
| 110 |
+
|
| 111 |
+
return NextResponse.json({ success: true });
|
| 112 |
}
|
| 113 |
|
|
|
|
| 114 |
const updateData: Record<string, unknown> = {};
|
| 115 |
if (isActive !== undefined) updateData.isActive = isActive;
|
| 116 |
if (triggerConfig) updateData.triggerConfig = JSON.stringify(triggerConfig);
|
| 117 |
if (actions) updateData.actions = JSON.stringify(actions);
|
| 118 |
|
| 119 |
+
const automation = await db.automation.update({ where: { id }, data: updateData });
|
| 120 |
+
return NextResponse.json({ success: true, automation });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
} catch (error) {
|
| 122 |
console.error("Error updating automation:", error);
|
| 123 |
+
return NextResponse.json({ success: false, error: "Error al actualizar" }, { status: 500 });
|
|
|
|
|
|
|
|
|
|
| 124 |
}
|
| 125 |
}
|
| 126 |
|
|
|
|
| 127 |
export async function DELETE(request: NextRequest) {
|
| 128 |
try {
|
| 129 |
const { searchParams } = new URL(request.url);
|
| 130 |
const id = searchParams.get("id");
|
| 131 |
|
| 132 |
+
if (!id) return NextResponse.json({ success: false, error: "ID requerido" }, { status: 400 });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
+
await db.automationLog.deleteMany({ where: { automationId: id } });
|
| 135 |
+
await db.automation.delete({ where: { id } });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
+
return NextResponse.json({ success: true });
|
| 138 |
} catch (error) {
|
| 139 |
console.error("Error deleting automation:", error);
|
| 140 |
+
return NextResponse.json({ success: false, error: "Error al eliminar" }, { status: 500 });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
}
|
| 142 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|