193 lines
4.5 KiB
TypeScript
193 lines
4.5 KiB
TypeScript
|
|
import { adminApiClient } from './client'
|
||
|
|
import type { AxiosError } from 'axios'
|
||
|
|
|
||
|
|
export interface RoomDto {
|
||
|
|
id: string
|
||
|
|
code: string
|
||
|
|
status: 'WAITING' | 'PLAYING' | 'FINISHED'
|
||
|
|
hostId: string
|
||
|
|
createdAt: string
|
||
|
|
expiresAt?: string
|
||
|
|
isAdminRoom: boolean
|
||
|
|
customCode?: string
|
||
|
|
activeFrom?: string
|
||
|
|
activeTo?: string
|
||
|
|
themeId?: string
|
||
|
|
questionPackId?: string
|
||
|
|
uiControls?: {
|
||
|
|
allowThemeChange?: boolean
|
||
|
|
allowPackChange?: boolean
|
||
|
|
allowNameChange?: boolean
|
||
|
|
allowScoreEdit?: boolean
|
||
|
|
}
|
||
|
|
maxPlayers: number
|
||
|
|
allowSpectators: boolean
|
||
|
|
timerEnabled: boolean
|
||
|
|
timerDuration: number
|
||
|
|
host: {
|
||
|
|
id: string
|
||
|
|
name?: string
|
||
|
|
email?: string
|
||
|
|
}
|
||
|
|
theme?: {
|
||
|
|
id: string
|
||
|
|
name: string
|
||
|
|
isPublic: boolean
|
||
|
|
}
|
||
|
|
questionPack?: {
|
||
|
|
id: string
|
||
|
|
name: string
|
||
|
|
}
|
||
|
|
_count?: {
|
||
|
|
participants: number
|
||
|
|
}
|
||
|
|
startedAt?: string
|
||
|
|
finishedAt?: string
|
||
|
|
}
|
||
|
|
|
||
|
|
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
|
||
|
|
)
|
||
|
|
}
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|