Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/next-frontend/app/api/debug/invite-codes/route.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { db } from '@ticketeate/db';
import { prisma } from '@repo/db';

export const dynamic = 'force-dynamic';

export async function GET(request: Request) {
try {
// Verificar que la tabla invite_codes existe
const inviteCodesCount = await db.invite_codes.count();

Check failure on line 8 in apps/next-frontend/app/api/debug/invite-codes/route.ts

View workflow job for this annotation

GitHub Actions / Pre-commit Checks

'db' is not defined

Check failure on line 8 in apps/next-frontend/app/api/debug/invite-codes/route.ts

View workflow job for this annotation

GitHub Actions / Lint & Type Check

'db' is not defined

Check failure on line 8 in apps/next-frontend/app/api/debug/invite-codes/route.ts

View workflow job for this annotation

GitHub Actions / Lint

'db' is not defined

Check failure on line 8 in apps/next-frontend/app/api/debug/invite-codes/route.ts

View workflow job for this annotation

GitHub Actions / Pre-commit Checks

'db' is not defined
const colaboradoresCount = await db.colaborador_eventos.count();

Check failure on line 9 in apps/next-frontend/app/api/debug/invite-codes/route.ts

View workflow job for this annotation

GitHub Actions / Pre-commit Checks

'db' is not defined

Check failure on line 9 in apps/next-frontend/app/api/debug/invite-codes/route.ts

View workflow job for this annotation

GitHub Actions / Lint & Type Check

'db' is not defined

Check failure on line 9 in apps/next-frontend/app/api/debug/invite-codes/route.ts

View workflow job for this annotation

GitHub Actions / Lint

'db' is not defined

Check failure on line 9 in apps/next-frontend/app/api/debug/invite-codes/route.ts

View workflow job for this annotation

GitHub Actions / Pre-commit Checks

'db' is not defined

// Obtener los primeros 5 códigos de invitación
const inviteCodes = await db.invite_codes.findMany({

Check failure on line 12 in apps/next-frontend/app/api/debug/invite-codes/route.ts

View workflow job for this annotation

GitHub Actions / Pre-commit Checks

'db' is not defined

Check failure on line 12 in apps/next-frontend/app/api/debug/invite-codes/route.ts

View workflow job for this annotation

GitHub Actions / Lint & Type Check

'db' is not defined

Check failure on line 12 in apps/next-frontend/app/api/debug/invite-codes/route.ts

View workflow job for this annotation

GitHub Actions / Lint

'db' is not defined

Check failure on line 12 in apps/next-frontend/app/api/debug/invite-codes/route.ts

View workflow job for this annotation

GitHub Actions / Pre-commit Checks

'db' is not defined
take: 5,
include: {
eventos: {
Expand Down
70 changes: 32 additions & 38 deletions apps/next-frontend/app/api/invite-codes/use/route.ts
Original file line number Diff line number Diff line change
@@ -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<Response> {
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(),
Expand All @@ -42,58 +33,61 @@ export async function POST(request: Request): Promise<Response> {
});

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,
},
},
});

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 });
}
}
61 changes: 26 additions & 35 deletions apps/next-frontend/app/api/invite-codes/validate/route.ts
Original file line number Diff line number Diff line change
@@ -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<Response> {
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 });
}
}
Loading