Files
no-copy-frontend/src/app/actions/action.ts
T

216 lines
4.0 KiB
TypeScript
Raw Normal View History

2025-11-25 17:13:16 +07:00
'use server'
2026-02-03 17:31:27 +07:00
2026-01-20 16:53:11 +07:00
import { getSessionData, deleteSession } from '@/app/actions/session';
2025-12-27 11:54:54 +07:00
import { testUserData, API_BASE_URL } from '@/app/actions/definitions';
2025-11-29 14:40:26 +07:00
2025-12-02 17:11:53 +07:00
export async function getUserData() {
const userEmail = await getSessionData('email');
const token = await getSessionData('token');
2025-12-11 18:28:35 +07:00
/* для теста */
2026-01-28 18:53:08 +07:00
if (userEmail === 'test' && token === '1111') {
2025-12-11 18:28:35 +07:00
return testUserData;
}
/* для теста */
2025-11-25 17:13:16 +07:00
try {
2025-12-07 10:58:53 +07:00
const response = await fetch(`${API_BASE_URL}/v1/api/user?email=${userEmail}`, {
2025-12-02 17:11:53 +07:00
method: 'GET',
2025-11-25 17:13:16 +07:00
headers: {
2026-01-28 18:53:08 +07:00
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': `Bearer ${token}`,
2025-11-25 17:13:16 +07:00
}
});
2025-12-02 17:11:53 +07:00
if (response.ok) {
return await response.json();
2026-02-13 14:16:39 +07:00
} else {
return null
2025-12-02 17:11:53 +07:00
}
2025-11-25 17:13:16 +07:00
} catch (error) {
2026-02-13 13:44:03 +07:00
return null
2025-11-25 17:13:16 +07:00
}
2026-01-07 13:03:50 +07:00
}
export async function getUserFilesInfo() {
const token = await getSessionData('token');
try {
const response = await fetch(`${API_BASE_URL}/api/v1/data`, {
method: 'POST',
body: JSON.stringify({
version: 1,
msg_id: 20005,
message_body: {
2026-01-28 18:53:08 +07:00
action: 'user_files_info',
2026-01-07 13:03:50 +07:00
token: token
}
}),
headers: {
2026-01-28 18:53:08 +07:00
'Content-Type': 'application/json',
'Accept': 'application/json'
2026-01-07 13:03:50 +07:00
}
});
if (response.ok) {
const parsed = await response.json();
if (parsed.message_code === 0) {
return parsed.message_body;
2026-01-20 16:53:11 +07:00
} else if (parsed.message_code === 2) {
await deleteSession();
2026-01-07 13:03:50 +07:00
} else {
throw parsed.message_code;
}
2026-02-13 14:16:39 +07:00
} else {
return null
2026-01-07 13:03:50 +07:00
}
} catch (error) {
2026-02-13 12:50:58 +07:00
return null
2026-01-07 13:03:50 +07:00
}
2026-02-09 17:28:03 +07:00
}
export async function fetchTariffs() {
const token = await getSessionData('token');
try {
const response = await fetch(`${API_BASE_URL}/api/v1/data`, {
method: 'POST',
body: JSON.stringify({
version: 1,
msg_id: 30001,
message_body: {
action: 'get',
user_token: token
}
}),
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?.tariffs;
} else {
return []
}
2026-02-13 14:16:39 +07:00
} else {
return null
2026-02-09 17:28:03 +07:00
}
} catch (error) {
2026-02-13 14:16:39 +07:00
return null
2026-02-09 17:28:03 +07:00
}
2026-02-12 11:16:58 +07:00
}
2026-02-19 17:23:52 +07:00
export async function fetchTokensBundle() {
try {
const response = await fetch(`${API_BASE_URL}/api/v1/data`, {
method: 'POST',
body: JSON.stringify({
version: 1,
msg_id: 30001,
message_body: {
action: 'tariffs_by_type',
type: 'token'
}
}),
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?.token_tariffs;
} else {
return null
}
} else {
return null
}
} catch (error) {
return null
}
}
2026-02-12 11:16:58 +07:00
export async function getBuildData() {
try {
const response = await fetch(`${API_BASE_URL}/check/api/build`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
}
});
if (response.ok) {
return await response.json();
2026-02-13 14:16:39 +07:00
} else {
return null
2026-02-12 11:16:58 +07:00
}
} catch (error) {
2026-02-12 11:17:30 +07:00
return null;
2026-02-12 11:16:58 +07:00
}
}
2026-02-13 19:17:10 +07:00
export async function fetchINN(inn: string) {
try {
const response = await fetch(`${API_BASE_URL}/api/v1/data`, {
method: 'POST',
body: JSON.stringify({
version: 1,
msg_id: 30004,
message_body: {
inn: inn,
}
}),
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 {
return null
}
} catch (error) {
return null
}
2026-02-18 14:21:57 +07:00
}
export async function getHealt() {
try {
const response = await fetch(`${API_BASE_URL}/check/api/healt`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
});
if (response.ok) {
return await response.json();
} else {
return null
}
} catch (error) {
return null
}
2026-02-13 19:17:10 +07:00
}