Spaces:
Build error
Build error
| import { NextRequest, NextResponse } from "next/server"; | |
| import { db } from "@/lib/db"; | |
| export async function POST(request: NextRequest) { | |
| try { | |
| const body = await request.json(); | |
| const { userId, influencerId, tier, price, currency = "USD", expiresAt, stripeSubscriptionId, stripeCustomerId, autoRenew = true } = body; | |
| if (!userId || !influencerId || !tier) { | |
| return NextResponse.json({ success: false, error: "userId, influencerId y tier son requeridos" }, { status: 400 }); | |
| } | |
| // Verificar que influencer existe | |
| const influencer = await db.aIInfluencer.findUnique({ where: { id: influencerId } }); | |
| if (!influencer) return NextResponse.json({ success: false, error: "Influencer no encontrado" }, { status: 404 }); | |
| // Verificar que el usuario existe | |
| const user = await db.user.findUnique({ where: { id: userId } }); | |
| if (!user) return NextResponse.json({ success: false, error: "Usuario no encontrado" }, { status: 404 }); | |
| // Pol铆tica ACTUALIZADA: permitir cualquier tier pagando (sin restricci贸n) | |
| const nextRenewalDate = autoRenew ? new Date(Date.now() + 30 * 24 * 60 * 60 * 1000) : null; // 30 d铆as | |
| const subscription = await db.influencerSubscription.create({ | |
| data: { | |
| influencerId, | |
| userId, | |
| tier, | |
| price: price ?? null, | |
| currency: currency, | |
| expiresAt: expiresAt ? new Date(expiresAt) : null, | |
| nextRenewalDate, | |
| autoRenew, | |
| stripeSubscriptionId: stripeSubscriptionId || null, | |
| stripeCustomerId: stripeCustomerId || null, | |
| } | |
| }); | |
| // Registrar tarea para auditor铆a | |
| try { | |
| await db.agentTask.create({ data: { type: "subscribe_influencer", status: "completed", input: JSON.stringify({ userId, influencerId, tier }), output: subscription.id, completedAt: new Date() } }); | |
| } catch { } | |
| return NextResponse.json({ success: true, subscription }); | |
| } catch (error: unknown) { | |
| const message = error instanceof Error ? error.message : "Error desconocido"; | |
| return NextResponse.json({ success: false, error: message }, { status: 500 }); | |
| } | |
| } | |
| export async function GET(request: NextRequest) { | |
| try { | |
| const { searchParams } = new URL(request.url); | |
| const userId = searchParams.get("userId"); | |
| const influencerId = searchParams.get("influencerId"); | |
| const where: any = {}; | |
| if (userId) where.userId = userId; | |
| if (influencerId) where.influencerId = influencerId; | |
| const subs = await db.influencerSubscription.findMany({ where, orderBy: { createdAt: "desc" } }); | |
| return NextResponse.json({ success: true, subscriptions: subs }); | |
| } 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 }); | |
| const sub = await db.influencerSubscription.findUnique({ where: { id } }); | |
| if (!sub) return NextResponse.json({ success: false, error: "Suscripci贸n no encontrada" }, { status: 404 }); | |
| // Marcar como cancelada (soft delete) | |
| await db.influencerSubscription.update({ | |
| where: { id }, | |
| data: { status: "cancelled", autoRenew: false } | |
| }); | |
| try { await db.agentTask.create({ data: { type: "unsubscribe_influencer", status: "completed", input: id, output: "cancelled", completedAt: new Date() } }); } catch { } | |
| return NextResponse.json({ success: true, message: "Suscripci贸n cancelada" }); | |
| } catch (error) { | |
| const message = error instanceof Error ? error.message : "Error"; | |
| return NextResponse.json({ success: false, error: message }, { status: 500 }); | |
| } | |
| } | |