Add admin panel
This commit is contained in:
@@ -1,10 +1,25 @@
|
||||
import client from './client'
|
||||
import type { AdminUser, AdminMarathon, UserRole, PlatformStats } from '@/types'
|
||||
import type {
|
||||
AdminUser,
|
||||
AdminMarathon,
|
||||
UserRole,
|
||||
PlatformStats,
|
||||
AdminLogsResponse,
|
||||
BroadcastResponse,
|
||||
StaticContent,
|
||||
DashboardStats
|
||||
} from '@/types'
|
||||
|
||||
export const adminApi = {
|
||||
// Dashboard
|
||||
getDashboard: async (): Promise<DashboardStats> => {
|
||||
const response = await client.get<DashboardStats>('/admin/dashboard')
|
||||
return response.data
|
||||
},
|
||||
|
||||
// Users
|
||||
listUsers: async (skip = 0, limit = 50, search?: string): Promise<AdminUser[]> => {
|
||||
const params: Record<string, unknown> = { skip, limit }
|
||||
listUsers: async (skip = 0, limit = 50, search?: string, bannedOnly = false): Promise<AdminUser[]> => {
|
||||
const params: Record<string, unknown> = { skip, limit, banned_only: bannedOnly }
|
||||
if (search) params.search = search
|
||||
const response = await client.get<AdminUser[]>('/admin/users', { params })
|
||||
return response.data
|
||||
@@ -24,6 +39,19 @@ export const adminApi = {
|
||||
await client.delete(`/admin/users/${id}`)
|
||||
},
|
||||
|
||||
banUser: async (id: number, reason: string, bannedUntil?: string): Promise<AdminUser> => {
|
||||
const response = await client.post<AdminUser>(`/admin/users/${id}/ban`, {
|
||||
reason,
|
||||
banned_until: bannedUntil || null,
|
||||
})
|
||||
return response.data
|
||||
},
|
||||
|
||||
unbanUser: async (id: number): Promise<AdminUser> => {
|
||||
const response = await client.post<AdminUser>(`/admin/users/${id}/unban`)
|
||||
return response.data
|
||||
},
|
||||
|
||||
// Marathons
|
||||
listMarathons: async (skip = 0, limit = 50, search?: string): Promise<AdminMarathon[]> => {
|
||||
const params: Record<string, unknown> = { skip, limit }
|
||||
@@ -36,9 +64,62 @@ export const adminApi = {
|
||||
await client.delete(`/admin/marathons/${id}`)
|
||||
},
|
||||
|
||||
forceFinishMarathon: async (id: number): Promise<void> => {
|
||||
await client.post(`/admin/marathons/${id}/force-finish`)
|
||||
},
|
||||
|
||||
// Stats
|
||||
getStats: async (): Promise<PlatformStats> => {
|
||||
const response = await client.get<PlatformStats>('/admin/stats')
|
||||
return response.data
|
||||
},
|
||||
|
||||
// Logs
|
||||
getLogs: async (skip = 0, limit = 50, action?: string, adminId?: number): Promise<AdminLogsResponse> => {
|
||||
const params: Record<string, unknown> = { skip, limit }
|
||||
if (action) params.action = action
|
||||
if (adminId) params.admin_id = adminId
|
||||
const response = await client.get<AdminLogsResponse>('/admin/logs', { params })
|
||||
return response.data
|
||||
},
|
||||
|
||||
// Broadcast
|
||||
broadcastToAll: async (message: string): Promise<BroadcastResponse> => {
|
||||
const response = await client.post<BroadcastResponse>('/admin/broadcast/all', { message })
|
||||
return response.data
|
||||
},
|
||||
|
||||
broadcastToMarathon: async (marathonId: number, message: string): Promise<BroadcastResponse> => {
|
||||
const response = await client.post<BroadcastResponse>(`/admin/broadcast/marathon/${marathonId}`, { message })
|
||||
return response.data
|
||||
},
|
||||
|
||||
// Static Content
|
||||
listContent: async (): Promise<StaticContent[]> => {
|
||||
const response = await client.get<StaticContent[]>('/admin/content')
|
||||
return response.data
|
||||
},
|
||||
|
||||
getContent: async (key: string): Promise<StaticContent> => {
|
||||
const response = await client.get<StaticContent>(`/admin/content/${key}`)
|
||||
return response.data
|
||||
},
|
||||
|
||||
updateContent: async (key: string, title: string, content: string): Promise<StaticContent> => {
|
||||
const response = await client.put<StaticContent>(`/admin/content/${key}`, { title, content })
|
||||
return response.data
|
||||
},
|
||||
|
||||
createContent: async (key: string, title: string, content: string): Promise<StaticContent> => {
|
||||
const response = await client.post<StaticContent>('/admin/content', { key, title, content })
|
||||
return response.data
|
||||
},
|
||||
}
|
||||
|
||||
// Public content API (no auth required)
|
||||
export const contentApi = {
|
||||
getPublicContent: async (key: string): Promise<StaticContent> => {
|
||||
const response = await client.get<StaticContent>(`/content/${key}`)
|
||||
return response.data
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import client from './client'
|
||||
import type { TokenResponse, User } from '@/types'
|
||||
import type { TokenResponse, LoginResponse, User } from '@/types'
|
||||
|
||||
export interface RegisterData {
|
||||
login: string
|
||||
@@ -18,8 +18,15 @@ export const authApi = {
|
||||
return response.data
|
||||
},
|
||||
|
||||
login: async (data: LoginData): Promise<TokenResponse> => {
|
||||
const response = await client.post<TokenResponse>('/auth/login', data)
|
||||
login: async (data: LoginData): Promise<LoginResponse> => {
|
||||
const response = await client.post<LoginResponse>('/auth/login', data)
|
||||
return response.data
|
||||
},
|
||||
|
||||
verify2FA: async (sessionId: number, code: string): Promise<TokenResponse> => {
|
||||
const response = await client.post<TokenResponse>('/auth/2fa/verify', null, {
|
||||
params: { session_id: sessionId, code }
|
||||
})
|
||||
return response.data
|
||||
},
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import axios, { AxiosError } from 'axios'
|
||||
import { useAuthStore, type BanInfo } from '@/store/auth'
|
||||
|
||||
const API_URL = import.meta.env.VITE_API_URL || '/api/v1'
|
||||
|
||||
@@ -18,10 +19,20 @@ client.interceptors.request.use((config) => {
|
||||
return config
|
||||
})
|
||||
|
||||
// Helper to check if detail is ban info object
|
||||
function isBanInfo(detail: unknown): detail is BanInfo {
|
||||
return (
|
||||
typeof detail === 'object' &&
|
||||
detail !== null &&
|
||||
'banned_at' in detail &&
|
||||
'reason' in detail
|
||||
)
|
||||
}
|
||||
|
||||
// Response interceptor to handle errors
|
||||
client.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error: AxiosError<{ detail: string }>) => {
|
||||
(error: AxiosError<{ detail: string | BanInfo }>) => {
|
||||
// Unauthorized - redirect to login
|
||||
if (error.response?.status === 401) {
|
||||
localStorage.removeItem('token')
|
||||
@@ -29,6 +40,15 @@ client.interceptors.response.use(
|
||||
window.location.href = '/login'
|
||||
}
|
||||
|
||||
// Forbidden - check if user is banned
|
||||
if (error.response?.status === 403) {
|
||||
const detail = error.response.data?.detail
|
||||
if (isBanInfo(detail)) {
|
||||
// User is banned - set ban info in store
|
||||
useAuthStore.getState().setBanned(detail)
|
||||
}
|
||||
}
|
||||
|
||||
// Server error or network error - redirect to 500 page
|
||||
if (
|
||||
error.response?.status === 500 ||
|
||||
|
||||
@@ -3,7 +3,7 @@ export { marathonsApi } from './marathons'
|
||||
export { gamesApi } from './games'
|
||||
export { wheelApi } from './wheel'
|
||||
export { feedApi } from './feed'
|
||||
export { adminApi } from './admin'
|
||||
export { adminApi, contentApi } from './admin'
|
||||
export { eventsApi } from './events'
|
||||
export { challengesApi } from './challenges'
|
||||
export { assignmentsApi } from './assignments'
|
||||
|
||||
Reference in New Issue
Block a user