sto-k-odnomu/admin/src/api/rooms.ts

220 lines
5.2 KiB
TypeScript
Raw Normal View History

2026-01-09 21:36:49 +00:00
import { adminApiClient } from './client'
import type { AxiosError } from 'axios'
2026-01-10 00:18:08 +00:00
export interface ParticipantDto {
id: string
userId: string
name: string
role: 'HOST' | 'PLAYER' | 'SPECTATOR'
score: number
joinedAt: string
isActive: boolean
user: {
id: string
name?: string
email?: string
}
}
2026-01-09 21:36:49 +00:00
export interface RoomDto {
id: string
code: string
status: 'WAITING' | 'PLAYING' | 'FINISHED'
hostId: string
createdAt: string
2026-01-10 00:18:08 +00:00
expiresAt?: string | null
2026-01-09 21:36:49 +00:00
isAdminRoom: boolean
2026-01-10 00:18:08 +00:00
customCode?: string | null
activeFrom?: string | null
activeTo?: string | null
themeId?: string | null
questionPackId?: string | null
2026-01-09 21:36:49 +00:00
uiControls?: {
allowThemeChange?: boolean
allowPackChange?: boolean
allowNameChange?: boolean
allowScoreEdit?: boolean
2026-01-10 00:18:08 +00:00
} | null
2026-01-09 21:36:49 +00:00
maxPlayers: number
allowSpectators: boolean
timerEnabled: boolean
timerDuration: number
2026-01-10 00:18:08 +00:00
autoAdvance?: boolean
voiceMode?: boolean
currentQuestionIndex?: number
totalQuestions?: number
answeredQuestions?: number
currentQuestionId?: string | null
currentPlayerId?: string | null
isGameOver?: boolean
revealedAnswers?: Record<string, string[]> | null
2026-01-09 21:36:49 +00:00
host: {
id: string
2026-01-10 00:18:08 +00:00
name?: string | null
email?: string | null
2026-01-09 21:36:49 +00:00
}
theme?: {
id: string
name: string
isPublic: boolean
2026-01-10 00:18:08 +00:00
} | null
2026-01-09 21:36:49 +00:00
questionPack?: {
id: string
name: string
2026-01-10 00:18:08 +00:00
description?: string | null
questionCount?: number
} | null
participants?: ParticipantDto[]
2026-01-09 21:36:49 +00:00
_count?: {
participants: number
}
2026-01-10 00:18:08 +00:00
startedAt?: string | null
finishedAt?: string | null
2026-01-09 21:36:49 +00:00
}
export interface CreateAdminRoomDto {
hostId: string
hostName?: string
customCode?: string
activeFrom?: string
activeTo?: string
themeId?: string
questionPackId?: string
uiControls?: {
allowThemeChange?: boolean
allowPackChange?: boolean
allowNameChange?: boolean
allowScoreEdit?: boolean
}
settings?: {
maxPlayers?: number
allowSpectators?: boolean
timerEnabled?: boolean
timerDuration?: number
}
}
export interface RoomsResponse {
rooms: RoomDto[]
total: number
page: number
limit: number
totalPages: number
}
export interface RoomsApiError {
message: string
statusCode?: number
originalError?: unknown
name: 'RoomsApiError'
}
export function createRoomsApiError(
message: string,
statusCode?: number,
originalError?: unknown
): RoomsApiError {
return {
message,
statusCode,
originalError,
name: 'RoomsApiError',
}
}
export function isRoomsApiError(error: unknown): error is RoomsApiError {
return (
typeof error === 'object' &&
error !== null &&
'name' in error &&
error.name === 'RoomsApiError'
)
}
export const roomsApi = {
getRooms: async (params?: {
page?: number
limit?: number
status?: 'WAITING' | 'PLAYING' | 'FINISHED'
dateFrom?: string
dateTo?: string
}): Promise<RoomsResponse> => {
try {
const response = await adminApiClient.get('/api/admin/rooms', {
params: {
page: params?.page || 1,
limit: params?.limit || 20,
status: params?.status,
dateFrom: params?.dateFrom,
dateTo: params?.dateTo,
},
})
return response.data
} catch (error) {
const axiosError = error as AxiosError<{ message?: string }>
throw createRoomsApiError(
axiosError.response?.data?.message || 'Failed to load rooms',
axiosError.response?.status,
error
)
}
},
getRoom: async (roomId: string): Promise<RoomDto> => {
try {
const response = await adminApiClient.get(`/api/admin/rooms/${roomId}`)
return response.data
} catch (error) {
const axiosError = error as AxiosError<{ message?: string }>
if (axiosError.response?.status === 404) {
throw createRoomsApiError(
`Room not found: The room with ID "${roomId}" does not exist.`,
axiosError.response.status,
error
)
}
throw createRoomsApiError(
axiosError.response?.data?.message || `Failed to load room ${roomId}`,
axiosError.response?.status,
error
)
}
},
createAdminRoom: async (room: CreateAdminRoomDto): Promise<RoomDto> => {
try {
const response = await adminApiClient.post('/api/admin/rooms', room)
return response.data
} catch (error) {
const axiosError = error as AxiosError<{ message?: string }>
throw createRoomsApiError(
axiosError.response?.data?.message || 'Failed to create room',
axiosError.response?.status,
error
)
}
},
deleteRoom: async (roomId: string): Promise<{ message: string }> => {
try {
const response = await adminApiClient.delete(`/api/admin/rooms/${roomId}`)
return response.data
} catch (error) {
const axiosError = error as AxiosError<{ message?: string }>
if (axiosError.response?.status === 404) {
throw createRoomsApiError(
`Room not found: The room with ID "${roomId}" does not exist.`,
axiosError.response.status,
error
)
}
throw createRoomsApiError(
axiosError.response?.data?.message || `Failed to delete room ${roomId}`,
axiosError.response?.status,
error
)
}
},
}