Spaces:
Build error
Build error
| import { NextRequest, NextResponse } from "next/server"; | |
| import { db } from "@/lib/db"; | |
| import { cancelStripeSubscription } from "@/lib/stripe"; | |
| // Endpoint para cancelar suscripci贸n | |
| // DELETE /api/influencers/subscription/cancel?id=<subscription-id> | |
| export async function POST(request: NextRequest) { | |
| try { | |
| const body = await request.json(); | |
| const { subscriptionId } = body; | |
| if (!subscriptionId) { | |
| return NextResponse.json({ success: false, error: "subscriptionId es requerido" }, { status: 400 }); | |
| } | |
| const sub = await db.influencerSubscription.findUnique({ where: { id: subscriptionId } }); | |
| if (!sub) return NextResponse.json({ success: false, error: "Suscripci贸n no encontrada" }, { status: 404 }); | |
| // Si tiene stripeSubscriptionId, cancelar en Stripe tambi茅n | |
| if (sub.stripeSubscriptionId) { | |
| try { | |
| await cancelStripeSubscription(sub.stripeSubscriptionId); | |
| } catch (stripeError) { | |
| console.error("Error cancelando en Stripe:", stripeError); | |
| // Continuar de todas formas para permitir cancelar localmente | |
| } | |
| } | |
| // Marcar como cancelada en BD | |
| const updated = await db.influencerSubscription.update({ | |
| where: { id: subscriptionId }, | |
| data: { | |
| status: "cancelled", | |
| autoRenew: false, | |
| expiresAt: new Date(), // Expirar inmediatamente | |
| }, | |
| }); | |
| try { | |
| await db.agentTask.create({ | |
| data: { | |
| type: "cancel_subscription", | |
| status: "completed", | |
| input: subscriptionId, | |
| output: "cancelled", | |
| completedAt: new Date(), | |
| }, | |
| }); | |
| } catch { } | |
| return NextResponse.json({ | |
| success: true, | |
| message: "Suscripci贸n cancelada correctamente", | |
| subscription: updated, | |
| }); | |
| } catch (error: unknown) { | |
| const message = error instanceof Error ? error.message : "Error desconocido"; | |
| return NextResponse.json({ success: false, error: message }, { status: 500 }); | |
| } | |
| } | |