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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | /** * Room Invitations Manager * Handles invitation logic for room members */ import { and, eq } from 'drizzle-orm' import { db, schema } from '@/db' export interface CreateInvitationParams { roomId: string userId: string userName: string invitedBy: string invitedByName: string invitationType: 'manual' | 'auto-unban' | 'auto-create' message?: string expiresAt?: Date } /** * Create or update an invitation * If a pending invitation exists, it will be replaced */ export async function createInvitation( params: CreateInvitationParams ): Promise<schema.RoomInvitation> { const now = new Date() // Check if there's an existing invitation const existing = await db .select() .from(schema.roomInvitations) .where( and( eq(schema.roomInvitations.roomId, params.roomId), eq(schema.roomInvitations.userId, params.userId) ) ) .limit(1) if (existing.length > 0) { // Update existing invitation const [updated] = await db .update(schema.roomInvitations) .set({ userName: params.userName, invitedBy: params.invitedBy, invitedByName: params.invitedByName, invitationType: params.invitationType, message: params.message, status: 'pending', // Reset to pending createdAt: now, // Update timestamp respondedAt: null, expiresAt: params.expiresAt, }) .where(eq(schema.roomInvitations.id, existing[0].id)) .returning() console.log('[Room Invitations] Updated invitation:', { userId: params.userId, roomId: params.roomId, }) return updated } // Create new invitation const [invitation] = await db .insert(schema.roomInvitations) .values({ roomId: params.roomId, userId: params.userId, userName: params.userName, invitedBy: params.invitedBy, invitedByName: params.invitedByName, invitationType: params.invitationType, message: params.message, status: 'pending', createdAt: now, expiresAt: params.expiresAt, }) .returning() console.log('[Room Invitations] Created invitation:', { userId: params.userId, roomId: params.roomId, }) return invitation } /** * Get all pending invitations for a user */ export async function getUserPendingInvitations(userId: string): Promise<schema.RoomInvitation[]> { return await db .select() .from(schema.roomInvitations) .where( and(eq(schema.roomInvitations.userId, userId), eq(schema.roomInvitations.status, 'pending')) ) .orderBy(schema.roomInvitations.createdAt) } /** * Get all invitations for a room */ export async function getRoomInvitations(roomId: string): Promise<schema.RoomInvitation[]> { return await db .select() .from(schema.roomInvitations) .where(eq(schema.roomInvitations.roomId, roomId)) .orderBy(schema.roomInvitations.createdAt) } /** * Accept an invitation */ export async function acceptInvitation(invitationId: string): Promise<schema.RoomInvitation> { const [invitation] = await db .update(schema.roomInvitations) .set({ status: 'accepted', respondedAt: new Date(), }) .where(eq(schema.roomInvitations.id, invitationId)) .returning() console.log('[Room Invitations] Accepted invitation:', invitationId) return invitation } /** * Decline an invitation */ export async function declineInvitation(invitationId: string): Promise<schema.RoomInvitation> { const [invitation] = await db .update(schema.roomInvitations) .set({ status: 'declined', respondedAt: new Date(), }) .where(eq(schema.roomInvitations.id, invitationId)) .returning() console.log('[Room Invitations] Declined invitation:', invitationId) return invitation } /** * Cancel/delete an invitation */ export async function cancelInvitation(roomId: string, userId: string): Promise<void> { await db .delete(schema.roomInvitations) .where( and(eq(schema.roomInvitations.roomId, roomId), eq(schema.roomInvitations.userId, userId)) ) console.log('[Room Invitations] Cancelled invitation:', { userId, roomId }) } /** * Get a specific invitation */ export async function getInvitation( roomId: string, userId: string ): Promise<schema.RoomInvitation | undefined> { const results = await db .select() .from(schema.roomInvitations) .where( and(eq(schema.roomInvitations.roomId, roomId), eq(schema.roomInvitations.userId, userId)) ) .limit(1) return results[0] } |