'use server' import { getSessionData } from '@/app/actions/session'; import { API_BASE_URL } from '@/app/actions/definitions'; export async function getViolationSearchStatus() { const token = await getSessionData('token'); try { const response = await fetch(`${API_BASE_URL}/api/v1/global-search/actual-task`, { method: 'GET', headers: { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': `Bearer ${token}`, } }); if (response.ok) { const text = await response.text(); if (text) { try { const parsed = JSON.parse(text); return parsed; } catch (e) { return { status: text === 'Task not found' ? 'task-not-found' : text }; } } else { return null; } } else { return null } } catch (error) { return null } } export async function startGlobalMonitoring(monitoringType: 'monitoring' | 'all_files') { const token = await getSessionData('token'); console.log('startGlobalMonitoring'); try { const response = await fetch(`${API_BASE_URL}/api/v1/global-search/start`, { method: 'POST', body: JSON.stringify({ searchType: monitoringType, ...(monitoringType === 'all_files' && { fileIds: [] }) }), headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}`, } }); if (response.ok) { const parsed: { taskId: string, status: string, totalFiles: number } = await response.json(); console.log(parsed); if (parsed?.status === 'ACCEPTED') { return parsed } else { return null } } else { return null } } catch (error) { return null } } export async function getViolationFilesArray() { const token = await getSessionData('token'); try { const response = await fetch(`${API_BASE_URL}/api/v1/global-search/violation-summary`, { method: 'GET', headers: { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': `Bearer ${token}`, } }); if (response.ok) { let parsed = await response.json(); if (parsed.length) { return parsed; } else { throw parsed; } } else { throw (`${response.status}`); } } catch (error) { return [] } } /* статистика нурешений */ export async function fetchViolationStats() { 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: 30010, message_body: { token: token } }), headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' } }); if (response.ok) { let parsed = await response.json(); if (parsed) { return parsed; } else { throw null; } } else { throw (`${response.status}`); } } catch (error) { return null } } export async function getFileViolations(fileId: string, page: number) { 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: 30009, message_body: { file_id: fileId, page: page, size: 5, sort_direction: "desc", token: token } }), headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' } }); if (response.ok) { let parsed = await response.json(); if (parsed.message_body) { return parsed.message_body; } else { throw null; } } else { throw (`${response.status}`); } } catch (error) { return null } } export async function fetchViolationAnalyticStatistic(group: 'domain' | 'tld') { 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: 30009, message_body: { group_by: group, action: "group", token: token, sort_direction: 'desc' } }), headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' } }); if (response.ok) { let parsed = await response.json(); if (parsed?.message_body) { return parsed?.message_body; } else { throw null; } } else { throw (`${response.status}`); } } catch (error) { return null } } export async function fetchViolationStatistic() { 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: 30010, message_body: { token: token, } }), headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' } }); if (response.ok) { let parsed = await response.json(); if (parsed?.message_body) { return parsed?.message_body; } else { throw null; } } else { throw (`${response.status}`); } } catch (error) { return null } } interface complainBody { previousText: string, violationID: string | null, success: boolean, errorMessage?: string | null } export async function createComplaint( state: complainBody | undefined, formData: FormData ): Promise { const email = await getSessionData('email'); const violationId = formData.get('violationId') as string || ''; const text = formData.get('note') as string || ''; try { const response = await fetch(`${API_BASE_URL}/api/v1/data`, { method: 'POST', body: JSON.stringify({ version: 1, msg_id: 30013, message_body: { action: 'create', violation_id: violationId, text: text, email: email } }), headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' } }); if (response.ok) { let parsed = await response.json(); if (parsed?.message_code === 0) { return { violationID: violationId, previousText: text, success: true }; } else { throw parsed; } } else { throw (`${response.status}`); } } catch (error: unknown) { if (error && typeof error === 'object' && 'message_code' in error) { const err = error as { message_code: number }; if (err.message_code === 2) { return { violationID: violationId, previousText: text, success: false, errorMessage: 'the-complaint-has-not-been-registered' } } } return { violationID: violationId, previousText: text, success: false, errorMessage: 'error-save' } } } export async function fetchComplainInfo(id: number) { try { const response = await fetch(`${API_BASE_URL}/api/v1/data`, { method: 'POST', body: JSON.stringify({ version: 1, msg_id: 30013, message_body: { action: 'get_by_violation', violation_id: id } }), headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' } }); if (response.ok) { let parsed = await response.json(); if (parsed?.message_code === 0) { return { body: parsed?.message_body } } else { throw parsed; } } else { throw (`${response.status}`); } } catch (error) { return { errorMessage: 'error' } } } export type MatchStatus = 'NEW' | 'SHOWED' | 'LEGAL_IN_WORK' | 'COMPLAINT_IN_WORK' | 'COMPLAINT_AND_LEGAL_IN_WORK' | 'AUTHORIZED_USE'; export async function updateMatchStatus(id: number, status: MatchStatus) { 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: 30009, message_body: { action: 'updateStatus', violation_id: id, status: status, token: token } }), headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' } }); if (response.ok) { let parsed = await response.json(); if (parsed?.message_body.status) { return true } else { throw parsed; } } else { throw (`${response.status}`); } } catch (error) { return false } }