add / edit / delete user warrens
This commit is contained in:
48
frontend/lib/api/admin/createUserWarren.ts
Normal file
48
frontend/lib/api/admin/createUserWarren.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import type { ApiResponse } from '~/shared/types/api';
|
||||
import type { UserWarren } from '~/shared/types/warrens';
|
||||
import { getApiHeaders } from '..';
|
||||
import { toast } from 'vue-sonner';
|
||||
|
||||
export async function createUserWarren(
|
||||
userWarren: UserWarren
|
||||
): Promise<{ success: true; data: UserWarren } | { success: false }> {
|
||||
const { data, error } = await useFetch<ApiResponse<UserWarren>>(
|
||||
getApiUrl('admin/user-warrens'),
|
||||
{
|
||||
method: 'POST',
|
||||
headers: getApiHeaders(),
|
||||
body: JSON.stringify(userWarren),
|
||||
responseType: 'json',
|
||||
}
|
||||
);
|
||||
|
||||
if (data.value == null) {
|
||||
toast.error('Create user warren', {
|
||||
description: error.value?.data ?? 'Failed to create user warren',
|
||||
});
|
||||
|
||||
return {
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
|
||||
const keys = ['admin-resources'];
|
||||
|
||||
const session = useAuthSession();
|
||||
if (
|
||||
session.value != null &&
|
||||
data.value.data.userId === session.value.user.id
|
||||
) {
|
||||
keys.push('warrens');
|
||||
}
|
||||
await refreshNuxtData(keys);
|
||||
|
||||
toast.success('Create user warren', {
|
||||
description: 'Successfully created user warren',
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: data.value.data,
|
||||
};
|
||||
}
|
||||
@@ -32,7 +32,7 @@ export async function deleteUser(
|
||||
await refreshNuxtData('admin-resources');
|
||||
|
||||
toast.success('Delete user', {
|
||||
description: 'Successfully delete user',
|
||||
description: 'Successfully deleted user',
|
||||
});
|
||||
|
||||
return {
|
||||
|
||||
52
frontend/lib/api/admin/deleteUserWarren.ts
Normal file
52
frontend/lib/api/admin/deleteUserWarren.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import type { UserWarren } from '~/shared/types/warrens';
|
||||
import { getApiHeaders } from '..';
|
||||
import { toast } from 'vue-sonner';
|
||||
import type { ApiResponse } from '~/shared/types/api';
|
||||
|
||||
export async function deleteUserWarren(
|
||||
userId: string,
|
||||
warrenId: string
|
||||
): Promise<{ success: true; data: UserWarren } | { success: false }> {
|
||||
const { data, error } = await useFetch<ApiResponse<UserWarren>>(
|
||||
getApiUrl('admin/user-warrens'),
|
||||
{
|
||||
method: 'DELETE',
|
||||
headers: getApiHeaders(),
|
||||
body: JSON.stringify({
|
||||
userId: userId,
|
||||
warrenId: warrenId,
|
||||
}),
|
||||
responseType: 'json',
|
||||
}
|
||||
);
|
||||
|
||||
if (data.value == null) {
|
||||
toast.error('Delete user warren', {
|
||||
description: error.value?.data ?? 'Failed to delete user warren',
|
||||
});
|
||||
|
||||
return {
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
|
||||
const keys = ['admin-resources'];
|
||||
const session = useAuthSession();
|
||||
if (
|
||||
session.value != null &&
|
||||
data.value.data.userId === session.value.user.id
|
||||
) {
|
||||
keys.push('warrens');
|
||||
}
|
||||
|
||||
await refreshNuxtData(keys);
|
||||
|
||||
toast.success('Delete user warren', {
|
||||
description: 'Successfully deleted user warren',
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: data.value.data,
|
||||
};
|
||||
}
|
||||
@@ -33,7 +33,14 @@ export async function editUser(
|
||||
};
|
||||
}
|
||||
|
||||
await refreshNuxtData('admin-resources');
|
||||
const session = useAuthSession();
|
||||
if (session.value != null && data.value.data.id === session.value.user.id) {
|
||||
reloadNuxtApp({
|
||||
ttl: 2000,
|
||||
});
|
||||
} else {
|
||||
await refreshNuxtData('admin-resources');
|
||||
}
|
||||
|
||||
toast.success('Edit user', {
|
||||
description: 'Successfully edited user',
|
||||
|
||||
30
frontend/lib/api/admin/editUserWarren.ts
Normal file
30
frontend/lib/api/admin/editUserWarren.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import type { UserWarren } from '~/shared/types/warrens';
|
||||
import { getApiHeaders } from '..';
|
||||
import type { ApiResponse } from '~/shared/types/api';
|
||||
|
||||
export async function editUserWarren(
|
||||
userWarren: UserWarren
|
||||
): Promise<{ success: true; data: UserWarren } | { success: false }> {
|
||||
const { data } = await useFetch<ApiResponse<UserWarren>>(
|
||||
getApiUrl('admin/user-warrens'),
|
||||
{
|
||||
method: 'PATCH',
|
||||
headers: getApiHeaders(),
|
||||
body: JSON.stringify(userWarren),
|
||||
responseType: 'json',
|
||||
}
|
||||
);
|
||||
|
||||
if (data.value == null) {
|
||||
return {
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
|
||||
await refreshNuxtData('admin-resources');
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: data.value.data,
|
||||
};
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { ApiResponse } from '~/shared/types/api';
|
||||
import type { UserWarren, Warren } from '~/shared/types/warrens';
|
||||
import type { AdminWarrenData, UserWarren } from '~/shared/types/warrens';
|
||||
import { getApiHeaders } from '..';
|
||||
import type { AdminResources, AuthUserWithWarrens } from '~/shared/types/admin';
|
||||
import type { AuthUser } from '~/shared/types/auth';
|
||||
@@ -15,7 +15,7 @@ export async function fetchAllAdminResources(): Promise<
|
||||
ApiResponse<{
|
||||
users: AuthUser[];
|
||||
userWarrens: UserWarren[];
|
||||
warrens: Warren[];
|
||||
warrens: AdminWarrenData[];
|
||||
}>
|
||||
>(getApiUrl('admin/all'), {
|
||||
method: 'GET',
|
||||
@@ -36,7 +36,7 @@ export async function fetchAllAdminResources(): Promise<
|
||||
warrens: [],
|
||||
}))
|
||||
.reduce((acc, u) => ({ ...acc, [u.id]: u }), {});
|
||||
const warrens: Record<string, Warren> = {};
|
||||
const warrens: Record<string, AdminWarrenData> = {};
|
||||
|
||||
for (const warren of data.value.data.warrens) {
|
||||
warrens[warren.id] = warren;
|
||||
|
||||
@@ -5,9 +5,10 @@ export async function getAuthSessionData(params: {
|
||||
sessionType: AuthSessionType;
|
||||
sessionId: string;
|
||||
}): Promise<
|
||||
{ success: true; user: AuthUser; expiresAt: number } | { success: false }
|
||||
| { success: true; user: AuthUser; expiresAt: number }
|
||||
| { success: false; code: number }
|
||||
> {
|
||||
const { data, status } = await useFetch<
|
||||
const { data, error, status } = await useFetch<
|
||||
ApiResponse<{
|
||||
user: AuthUser;
|
||||
expiresAt: number;
|
||||
@@ -23,6 +24,7 @@ export async function getAuthSessionData(params: {
|
||||
if (status.value !== 'success' || data.value == null) {
|
||||
return {
|
||||
success: false,
|
||||
code: error.value?.statusCode ?? 400,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,23 +1,22 @@
|
||||
import { toast } from 'vue-sonner';
|
||||
import type { DirectoryEntry } from '#shared/types';
|
||||
import type { ApiResponse } from '#shared/types/api';
|
||||
import type { Warren } from '#shared/types/warrens';
|
||||
import type { WarrenData } from '#shared/types/warrens';
|
||||
import { getApiHeaders, getAuthHeader } from '.';
|
||||
|
||||
export async function getWarrens(): Promise<Record<string, Warren>> {
|
||||
const { data, error } = await useFetch<ApiResponse<{ warrens: Warren[] }>>(
|
||||
getApiUrl('warrens'),
|
||||
{
|
||||
method: 'GET',
|
||||
headers: getApiHeaders(),
|
||||
}
|
||||
);
|
||||
export async function getWarrens(): Promise<Record<string, WarrenData>> {
|
||||
const { data, error } = await useFetch<
|
||||
ApiResponse<{ warrens: WarrenData[] }>
|
||||
>(getApiUrl('warrens'), {
|
||||
method: 'GET',
|
||||
headers: getApiHeaders(),
|
||||
});
|
||||
|
||||
if (data.value == null) {
|
||||
throw error.value?.name;
|
||||
}
|
||||
|
||||
const warrens: Record<string, Warren> = {};
|
||||
const warrens: Record<string, WarrenData> = {};
|
||||
|
||||
for (const warren of data.value.data.warrens) {
|
||||
warrens[warren.id] = warren;
|
||||
|
||||
@@ -17,3 +17,12 @@ export const editUserSchema = object({
|
||||
.optional(),
|
||||
admin: boolean().required('required'),
|
||||
});
|
||||
|
||||
export const userWarrenSchema = object({
|
||||
userId: string().uuid().required(),
|
||||
warrenId: string().uuid().required('required'),
|
||||
canListFiles: boolean().required(),
|
||||
canReadFiles: boolean().required(),
|
||||
canModifyFiles: boolean().required(),
|
||||
canDeleteFiles: boolean().required(),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user