source: petify-frontend/src/api/admin.ts@ fa32d0f

Last change on this file since fa32d0f was 92e7c7a, checked in by veronika-ils <ilioskaveronika@…>, 9 hours ago

Petify fullstack project

  • Property mode set to 100644
File size: 4.8 KB
RevLine 
[92e7c7a]1function getBaseUrl(): string {
2 const base = (import.meta.env.VITE_API_BASE_URL as string | undefined) ?? ''
3 return base.replace(/\/$/, '')
4}
5
6function joinUrl(base: string, path: string): string {
7 if (!base) return path
8 return `${base}${path.startsWith('/') ? '' : '/'}${path}`
9}
10
11export async function getAllUsers(userId: number): Promise<any[]> {
12 const url = joinUrl(getBaseUrl(), '/api/users/admin/all')
13 const response = await fetch(url, {
14 method: 'GET',
15 headers: {
16 'Content-Type': 'application/json',
17 'X-User-Id': String(userId),
18 },
19 })
20
21 if (!response.ok) {
22 throw new Error('Failed to fetch all users')
23 }
24
25 return await response.json()
26}
27
28export interface AdminListingsPage {
29 items: any[]
30 page: number
31 size: number
32 totalItems: number
33 totalPages: number
34 hasNext: boolean
35 hasPrevious: boolean
36 activeListings: number
37 soldListings: number
38}
39
40export interface AdminListingsFilters {
41 status?: string
42 minPrice?: string
43 maxPrice?: string
44}
45
46export async function getAllListings(
47 userId: number,
48 page = 0,
49 size = 500,
50 filters: AdminListingsFilters = {}
51): Promise<AdminListingsPage> {
52 const params = new URLSearchParams({
53 page: String(page),
54 size: String(size),
55 })
56 if (filters.status) {
57 params.set('status', filters.status)
58 }
59 if (filters.minPrice) {
60 params.set('minPrice', filters.minPrice)
61 }
62 if (filters.maxPrice) {
63 params.set('maxPrice', filters.maxPrice)
64 }
65 const url = joinUrl(getBaseUrl(), `/api/users/admin/listings?${params.toString()}`)
66 const response = await fetch(url, {
67 method: 'GET',
68 headers: {
69 'Content-Type': 'application/json',
70 'X-User-Id': String(userId),
71 },
72 })
73
74 if (!response.ok) {
75 const text = await response.text()
76 let apiError = ''
77 try {
78 apiError = JSON.parse(text).error || ''
79 } catch {
80 apiError = ''
81 }
82 throw new Error(apiError || `Failed to fetch listings: ${response.status} ${response.statusText}. ${text.slice(0, 300)}`)
83 }
84
85 return await response.json()
86}
87
88export async function blockUser(userId: number, targetUserId: number, isBlocked: boolean, reason?: string): Promise<void> {
89 const url = joinUrl(getBaseUrl(), `/api/users/admin/${targetUserId}/block`)
90 const response = await fetch(url, {
91 method: 'PATCH',
92 headers: {
93 'Content-Type': 'application/json',
94 'X-User-Id': String(userId),
95 },
96 body: JSON.stringify({ isBlocked, blockedReason: reason || '' }),
97 })
98
99 if (!response.ok) {
100 throw new Error('Failed to update user status')
101 }
102}
103
104export async function deleteUser(userId: number, targetUserId: number): Promise<void> {
105 const url = joinUrl(getBaseUrl(), `/api/users/admin/${targetUserId}`)
106 const response = await fetch(url, {
107 method: 'DELETE',
108 headers: {
109 'Content-Type': 'application/json',
110 'X-User-Id': String(userId),
111 },
112 })
113
114 if (!response.ok) {
115 throw new Error('Failed to delete user')
116 }
117}
118
119export async function getClinicApplications(userId: number): Promise<any[]> {
120 const url = joinUrl(getBaseUrl(), '/api/admin/clinic-applications')
121 const response = await fetch(url, {
122 method: 'GET',
123 headers: {
124 'Content-Type': 'application/json',
125 'X-User-Id': String(userId),
126 },
127 })
128
129 if (!response.ok) {
130 throw new Error('Failed to fetch clinic applications')
131 }
132
133 return await response.json()
134}
135
136export async function getClinicsAdmin(userId: number): Promise<any[]> {
137 const url = joinUrl(getBaseUrl(), '/api/clinics')
138 const response = await fetch(url, {
139 method: 'GET',
140 headers: {
141 'Content-Type': 'application/json',
142 'X-User-Id': String(userId),
143 },
144 })
145
146 if (!response.ok) {
147 throw new Error('Failed to fetch clinics')
148 }
149
150 return await response.json()
151}
152
153export async function approveClinicApplication(userId: number, applicationId: number): Promise<any> {
154 const url = joinUrl(getBaseUrl(), `/api/admin/clinic-applications/${applicationId}/approve`)
155 const response = await fetch(url, {
156 method: 'PATCH',
157 headers: {
158 'Content-Type': 'application/json',
159 'X-User-Id': String(userId),
160 },
161 })
162
163 if (!response.ok) {
164 const error = await response.json()
165 throw new Error(error.error || 'Failed to approve clinic application')
166 }
167
168 return await response.json()
169}
170
171export async function denyClinicApplication(userId: number, applicationId: number, denialReason: string): Promise<any> {
172 const url = joinUrl(getBaseUrl(), `/api/admin/clinic-applications/${applicationId}/deny`)
173 const response = await fetch(url, {
174 method: 'PATCH',
175 headers: {
176 'Content-Type': 'application/json',
177 'X-User-Id': String(userId),
178 },
179 body: JSON.stringify({ denialReason }),
180 })
181
182 if (!response.ok) {
183 const error = await response.json()
184 throw new Error(error.error || 'Failed to deny clinic application')
185 }
186
187 return await response.json()
188}
Note: See TracBrowser for help on using the repository browser.