edit users
This commit is contained in:
56
frontend/lib/api/admin/fetchAll.ts
Normal file
56
frontend/lib/api/admin/fetchAll.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import type { ApiResponse } from '~/shared/types/api';
|
||||
import type { UserWarren, Warren } from '~/shared/types/warrens';
|
||||
import { getApiHeaders } from '..';
|
||||
import type { AdminResources, AuthUserWithWarrens } from '~/shared/types/admin';
|
||||
import type { AuthUser } from '~/shared/types/auth';
|
||||
|
||||
export async function fetchAllAdminResources(): Promise<
|
||||
| {
|
||||
success: true;
|
||||
data: AdminResources;
|
||||
}
|
||||
| { success: false }
|
||||
> {
|
||||
const { data } = await useFetch<
|
||||
ApiResponse<{
|
||||
users: AuthUser[];
|
||||
userWarrens: UserWarren[];
|
||||
warrens: Warren[];
|
||||
}>
|
||||
>(getApiUrl('admin/all'), {
|
||||
method: 'GET',
|
||||
headers: getApiHeaders(),
|
||||
responseType: 'json',
|
||||
deep: false,
|
||||
});
|
||||
|
||||
if (data.value == null) {
|
||||
return {
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
|
||||
const users: Record<string, AuthUserWithWarrens> = data.value.data.users
|
||||
.map((u) => ({
|
||||
...u,
|
||||
warrens: [],
|
||||
}))
|
||||
.reduce((acc, u) => ({ ...acc, [u.id]: u }), {});
|
||||
const warrens: Record<string, Warren> = {};
|
||||
|
||||
for (const warren of data.value.data.warrens) {
|
||||
warrens[warren.id] = warren;
|
||||
}
|
||||
|
||||
for (const userWarren of data.value.data.userWarrens) {
|
||||
users[userWarren.userId].warrens.push(userWarren);
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
users: Object.values(users),
|
||||
warrens,
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user