All files / web/src/lib/arcade room-join-requests.ts

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

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153                                                                                                                                                                                                                                                                                                                 
/**
 * Room Join Requests Manager
 * Handles join request logic for approval-only rooms
 */

import { and, eq } from 'drizzle-orm'
import { db, schema } from '@/db'

export interface CreateJoinRequestParams {
  roomId: string
  userId: string
  userName: string
}

/**
 * Create a join request
 */
export async function createJoinRequest(
  params: CreateJoinRequestParams
): Promise<schema.RoomJoinRequest> {
  const now = new Date()

  // Check if there's an existing request
  const existing = await db
    .select()
    .from(schema.roomJoinRequests)
    .where(
      and(
        eq(schema.roomJoinRequests.roomId, params.roomId),
        eq(schema.roomJoinRequests.userId, params.userId)
      )
    )
    .limit(1)

  if (existing.length > 0) {
    // Update existing request (reset to pending)
    const [updated] = await db
      .update(schema.roomJoinRequests)
      .set({
        userName: params.userName,
        status: 'pending',
        requestedAt: now,
        reviewedAt: null,
        reviewedBy: null,
        reviewedByName: null,
      })
      .where(eq(schema.roomJoinRequests.id, existing[0].id))
      .returning()

    return updated
  }

  // Create new request
  const [request] = await db
    .insert(schema.roomJoinRequests)
    .values({
      roomId: params.roomId,
      userId: params.userId,
      userName: params.userName,
      status: 'pending',
      requestedAt: now,
    })
    .returning()

  return request
}

/**
 * Get all pending join requests for a room
 */
export async function getPendingJoinRequests(roomId: string): Promise<schema.RoomJoinRequest[]> {
  return await db
    .select()
    .from(schema.roomJoinRequests)
    .where(
      and(eq(schema.roomJoinRequests.roomId, roomId), eq(schema.roomJoinRequests.status, 'pending'))
    )
    .orderBy(schema.roomJoinRequests.requestedAt)
}

/**
 * Get all join requests for a room (any status)
 */
export async function getAllJoinRequests(roomId: string): Promise<schema.RoomJoinRequest[]> {
  return await db
    .select()
    .from(schema.roomJoinRequests)
    .where(eq(schema.roomJoinRequests.roomId, roomId))
    .orderBy(schema.roomJoinRequests.requestedAt)
}

/**
 * Approve a join request
 */
export async function approveJoinRequest(
  requestId: string,
  reviewedBy: string,
  reviewedByName: string
): Promise<schema.RoomJoinRequest> {
  const [request] = await db
    .update(schema.roomJoinRequests)
    .set({
      status: 'approved',
      reviewedAt: new Date(),
      reviewedBy,
      reviewedByName,
    })
    .where(eq(schema.roomJoinRequests.id, requestId))
    .returning()

  return request
}

/**
 * Deny a join request
 */
export async function denyJoinRequest(
  requestId: string,
  reviewedBy: string,
  reviewedByName: string
): Promise<schema.RoomJoinRequest> {
  const [request] = await db
    .update(schema.roomJoinRequests)
    .set({
      status: 'denied',
      reviewedAt: new Date(),
      reviewedBy,
      reviewedByName,
    })
    .where(eq(schema.roomJoinRequests.id, requestId))
    .returning()

  return request
}

/**
 * Get a specific join request
 */
export async function getJoinRequest(
  roomId: string,
  userId: string
): Promise<schema.RoomJoinRequest | undefined> {
  const results = await db
    .select()
    .from(schema.roomJoinRequests)
    .where(
      and(eq(schema.roomJoinRequests.roomId, roomId), eq(schema.roomJoinRequests.userId, userId))
    )
    .limit(1)

  return results[0]
}