From 6cad1a6607dacf5b33262c021bc84669be1098f2 Mon Sep 17 00:00:00 2001 From: Santserrano Date: Mon, 17 Nov 2025 10:52:02 -0300 Subject: [PATCH] updates --- .../app/api/debug/invite-codes/route.ts | 2 +- .../app/api/invite-codes/use/route.ts | 70 +++++++++---------- .../app/api/invite-codes/validate/route.ts | 61 +++++++--------- 3 files changed, 59 insertions(+), 74 deletions(-) diff --git a/apps/next-frontend/app/api/debug/invite-codes/route.ts b/apps/next-frontend/app/api/debug/invite-codes/route.ts index 2d394c6..8ae96b2 100644 --- a/apps/next-frontend/app/api/debug/invite-codes/route.ts +++ b/apps/next-frontend/app/api/debug/invite-codes/route.ts @@ -1,4 +1,4 @@ -import { db } from '@ticketeate/db'; +import { prisma } from '@repo/db'; export const dynamic = 'force-dynamic'; diff --git a/apps/next-frontend/app/api/invite-codes/use/route.ts b/apps/next-frontend/app/api/invite-codes/use/route.ts index 8bc1c29..c3e6ba1 100644 --- a/apps/next-frontend/app/api/invite-codes/use/route.ts +++ b/apps/next-frontend/app/api/invite-codes/use/route.ts @@ -1,39 +1,30 @@ +import { NextRequest, NextResponse } from 'next/server'; +import { prisma } from '@repo/db'; import { auth } from '@/lib/auth'; -import { db } from '@ticketeate/db'; export const dynamic = 'force-dynamic'; -interface UseInviteCodeResponse { - success: boolean; - message: string; - eventoid?: string; - colaborador_evento_id?: string; - error?: string; -} - -export async function POST(request: Request): Promise { +export async function POST(request: NextRequest) { try { // Obtener la sesión del usuario const session = await auth.api.getSession({ headers: request.headers, }); - if (!session || !session.user) { - return Response.json({ error: 'Usuario no autenticado' }, { status: 401 }); + if (!session) { + return NextResponse.json({ error: 'No autenticado' }, { status: 401 }); } const { codigo } = await request.json(); if (!codigo || typeof codigo !== 'string') { - return Response.json({ error: 'Código de invitación requerido' }, { status: 400 }); + return NextResponse.json({ error: 'Código de invitación requerido' }, { status: 400 }); } - const codigoTrimmed = codigo.trim().toUpperCase(); - - // Buscar el código en la base de datos - const inviteCode = await db.invite_codes.findFirst({ + // Buscar el código de invitación + const inviteCode = await prisma.invite_codes.findFirst({ where: { - codigo: codigoTrimmed, + codigo: codigo.trim().toUpperCase(), estado: 'ACTIVO', fecha_expiracion: { gt: new Date(), @@ -42,43 +33,46 @@ export async function POST(request: Request): Promise { }); if (!inviteCode) { - return Response.json({ error: 'Código de invitación no válido o expirado' }, { status: 401 }); + return NextResponse.json( + { error: 'Código de invitación no válido o expirado' }, + { status: 404 }, + ); } // Verificar que no ha excedido el máximo de usos if (inviteCode.usos_totales >= inviteCode.usos_max) { - return Response.json( + return NextResponse.json( { error: 'Código de invitación ya ha alcanzado el máximo de usos' }, - { status: 401 }, + { status: 400 }, ); } // Verificar si el usuario ya es colaborador de este evento - const existingColaborador = await db.colaborador_eventos.findFirst({ + const existingColaborador = await prisma.colaborador_eventos.findUnique({ where: { - eventoid: inviteCode.eventoid, - usuarioid: session.user.id, + eventoid_usuarioid: { + eventoid: inviteCode.eventoid, + usuarioid: session.user.id, + }, }, }); if (existingColaborador) { - return Response.json({ error: 'Ya eres colaborador de este evento' }, { status: 400 }); + return NextResponse.json({ error: 'Ya eres colaborador de este evento' }, { status: 400 }); } - // Crear el registro de colaborador_eventos - const colaborador = await db.colaborador_eventos.create({ + // Crear el registro de colaborador + const newColaborador = await prisma.colaborador_eventos.create({ data: { eventoid: inviteCode.eventoid, usuarioid: session.user.id, - invite_code_used: inviteCode.codigoid, + invite_code_used: codigo.trim().toUpperCase(), }, }); - // Incrementar el contador de usos del código - await db.invite_codes.update({ - where: { - codigoid: inviteCode.codigoid, - }, + // Incrementar el contador de usos del código de invitación + await prisma.invite_codes.update({ + where: { codigoid: inviteCode.codigoid }, data: { usos_totales: { increment: 1, @@ -86,14 +80,14 @@ export async function POST(request: Request): Promise { }, }); - return Response.json({ + return NextResponse.json({ success: true, - message: 'Te has registrado como colaborador correctamente', + message: '¡Código validado! Ahora eres colaborador del evento', eventoid: inviteCode.eventoid, - colaborador_evento_id: colaborador.colaborador_evento_id, - } as UseInviteCodeResponse); + colaborador_evento_id: newColaborador.colaborador_evento_id, + }); } catch (error) { console.error('Error using invite code:', error); - return Response.json({ error: 'Error al usar código de invitación' }, { status: 500 }); + return NextResponse.json({ error: 'Error al validar código de invitación' }, { status: 500 }); } } diff --git a/apps/next-frontend/app/api/invite-codes/validate/route.ts b/apps/next-frontend/app/api/invite-codes/validate/route.ts index a96ff2f..6239c07 100644 --- a/apps/next-frontend/app/api/invite-codes/validate/route.ts +++ b/apps/next-frontend/app/api/invite-codes/validate/route.ts @@ -1,68 +1,59 @@ -import { auth } from '@/lib/auth'; -import { db } from '@ticketeate/db'; +import { NextRequest, NextResponse } from 'next/server'; +import { prisma } from '@repo/db'; export const dynamic = 'force-dynamic'; -interface ValidateInviteCodeResponse { - valid: boolean; - eventoid?: string; - evento?: { - eventoid: string; - nombre: string; - descripcion: string; - fecha_inicio: string; - }; - error?: string; -} - -export async function POST(request: Request): Promise { +export async function POST(request: NextRequest) { try { const { codigo } = await request.json(); if (!codigo || typeof codigo !== 'string') { - return Response.json({ error: 'Código de invitación requerido' }, { status: 400 }); + return NextResponse.json({ error: 'Código de invitación requerido' }, { status: 400 }); } // Buscar el código en la base de datos - const inviteCode = await db.invite_codes.findFirst({ + const inviteCode = await prisma.invite_codes.findFirst({ where: { codigo: codigo.trim().toUpperCase(), estado: 'ACTIVO', fecha_expiracion: { - gt: new Date(), // Mayor que la fecha actual - }, - }, - include: { - eventos: { - select: { - eventoid: true, - nombre: true, - descripcion: true, - fecha_inicio: true, - }, + gt: new Date(), }, }, }); if (!inviteCode) { - return Response.json({ error: 'Código de invitación no válido o expirado' }, { status: 401 }); + return NextResponse.json( + { error: 'Código de invitación no válido o expirado' }, + { status: 404 }, + ); } // Verificar que no ha excedido el máximo de usos if (inviteCode.usos_totales >= inviteCode.usos_max) { - return Response.json( + return NextResponse.json( { error: 'Código de invitación ya ha alcanzado el máximo de usos' }, - { status: 401 }, + { status: 400 }, ); } - return Response.json({ + // Obtener datos del evento + const evento = await prisma.eventos.findUnique({ + where: { eventoid: inviteCode.eventoid }, + select: { + eventoid: true, + titulo: true, + descripcion: true, + }, + }); + + return NextResponse.json({ valid: true, eventoid: inviteCode.eventoid, - evento: inviteCode.eventos, - } as ValidateInviteCodeResponse); + titulo: evento?.titulo, + }); } catch (error) { console.error('Error validating invite code:', error); - return Response.json({ error: 'Error validando código de invitación' }, { status: 500 }); + return NextResponse.json({ error: 'Error validando código de invitación' }, { status: 500 }); } }