All files / web/src/app/api/arcade/invitations/pending route.ts

0% Statements 0/56
0% Branches 0/1
0% Functions 0/1
0% Lines 0/56

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57                                                                                                                 
import { eq } from 'drizzle-orm'
import { NextResponse } from 'next/server'
import { db, schema } from '@/db'
import { withAuth } from '@/lib/auth/withAuth'
import { getUserId } from '@/lib/viewer'

/**
 * GET /api/arcade/invitations/pending
 * Get all pending invitations for the current user with room details
 * Excludes invitations for rooms where the user is currently banned
 */
export const GET = withAuth(async () => {
  try {
    const userId = await getUserId()

    // Get pending invitations with room details
    const invitations = await db
      .select({
        id: schema.roomInvitations.id,
        roomId: schema.roomInvitations.roomId,
        roomName: schema.arcadeRooms.name,
        roomGameName: schema.arcadeRooms.gameName,
        userId: schema.roomInvitations.userId,
        userName: schema.roomInvitations.userName,
        invitedBy: schema.roomInvitations.invitedBy,
        invitedByName: schema.roomInvitations.invitedByName,
        status: schema.roomInvitations.status,
        invitationType: schema.roomInvitations.invitationType,
        message: schema.roomInvitations.message,
        createdAt: schema.roomInvitations.createdAt,
        expiresAt: schema.roomInvitations.expiresAt,
      })
      .from(schema.roomInvitations)
      .innerJoin(schema.arcadeRooms, eq(schema.roomInvitations.roomId, schema.arcadeRooms.id))
      .where(eq(schema.roomInvitations.userId, userId))
      .orderBy(schema.roomInvitations.createdAt)

    // Get all active bans for this user (bans are deleted when unbanned, so any existing ban is active)
    const activeBans = await db
      .select({ roomId: schema.roomBans.roomId })
      .from(schema.roomBans)
      .where(eq(schema.roomBans.userId, userId))

    const bannedRoomIds = new Set(activeBans.map((ban) => ban.roomId))

    // Filter to only pending invitations, excluding banned rooms
    const pendingInvitations = invitations.filter(
      (inv) => inv.status === 'pending' && !bannedRoomIds.has(inv.roomId)
    )

    return NextResponse.json({ invitations: pendingInvitations }, { status: 200 })
  } catch (error: any) {
    console.error('Failed to get pending invitations:', error)
    return NextResponse.json({ error: 'Failed to get pending invitations' }, { status: 500 })
  }
})