add stuff table, add stuff remove and add interfaces

This commit is contained in:
smanylov
2026-05-18 16:25:22 +07:00
parent d9720843e7
commit 748c6cb38a
12 changed files with 1193 additions and 19 deletions
+132
View File
@@ -0,0 +1,132 @@
'use server'
import { getSessionData } from '@/app/actions/session';
import { API_BASE_URL } from '@/app/actions/definitions';
export async function fetchStuffList(page?: number, size?: number, sortBy?: string, sortDirection?: 'asc' | 'desc' | string, nameQuery?: string) {
const token = await getSessionData('token');
try {
const response = await fetch(`${API_BASE_URL}/api/admin`, {
method: 'POST',
body: JSON.stringify({
version: 1,
msg_id: 100,
token: token,
message_body: {
/* token: token, */
action: 'get_all',
page: page,
size: size,
sort_by: sortBy || '',
sort_direction: sortDirection || 'asc',
full_name: nameQuery
}
}),
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
});
if (response.ok) {
const parsed = await response.json();
if (parsed.message_code === 0) {
return parsed.message_body;
} else {
return null;
}
} else {
throw new Error(`${response.status}`);
}
} catch (error) {
return null;
}
}
export async function createStuff(fullName: string, email: string, password: string) {
const token = await getSessionData('token');
try {
const response = await fetch(`${API_BASE_URL}/api/admin`, {
method: 'POST',
body: JSON.stringify({
version: 1,
msg_id: 100,
token: token,
message_body: {
action: 'create',
full_name: fullName,
email: email,
password: password,
is_super_admin: false
}
}),
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
});
if (response.ok) {
const parsed = await response.json();
if (parsed.message_code === 0) {
return parsed.message_body;
} else if (parsed.message_code === 3) {
throw new Error('email-already-exists');
} else if (parsed.message_code === 2) {
throw new Error('invalid-email');
} else {
return null;
}
} else {
throw new Error(`${response.status}`);
}
} catch (error) {
throw error;
}
}
export async function removeStuff(id: number) {
const token = await getSessionData('token');
try {
const response = await fetch(`${API_BASE_URL}/api/admin`, {
method: 'POST',
body: JSON.stringify({
version: 1,
msg_id: 100,
token: token,
message_body: {
action: 'delete',
admin_id: id,
}
}),
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
});
if (response.ok) {
const parsed = await response.json();
if (parsed.message_code === 0) {
return parsed.message_body;
} else if (parsed.message_code === 3) {
throw new Error('email-already-exists');
} else if (parsed.message_code === 2) {
throw new Error('invalid-email');
} else {
return null;
}
} else {
throw new Error(`${response.status}`);
}
} catch (error) {
throw error;
}
}