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

720 lines
15 KiB
TypeScript
Raw Normal View History

2026-03-12 17:44:02 +07:00
'use server'
import { getSessionData } from '@/app/actions/session';
import { API_BASE_URL, createComplaintSchema, createClaimSchema } from '@/app/actions/definitions';
2026-03-12 17:44:02 +07:00
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) {
2026-03-13 13:42:07 +07:00
const text = await response.text();
if (text) {
try {
const parsed = JSON.parse(text);
2026-03-13 13:42:07 +07:00
return parsed;
} catch (e) {
2026-03-13 13:42:07 +07:00
return {
status: text === 'Task not found' ? 'task-not-found' : text
};
}
} else {
return null;
}
2026-03-12 17:44:02 +07:00
} else {
return null
}
} catch (error) {
return null
}
}
export async function startGlobalMonitoring(monitoringType: 'monitoring' | 'all_files') {
const token = await getSessionData('token');
try {
const response = await fetch(`${API_BASE_URL}/api/v1/global-search/start`, {
method: 'POST',
body: JSON.stringify({
2026-03-13 13:42:07 +07:00
searchType: monitoringType,
...(monitoringType === 'all_files' && { fileIds: [] })
2026-03-12 17:44:02 +07:00
}),
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
}
});
if (response.ok) {
2026-03-13 13:42:07 +07:00
const parsed: {
taskId: string,
status: string,
totalFiles: number
} = await response.json();
if (parsed?.status === 'ACCEPTED') {
2026-03-12 17:44:02 +07:00
return parsed
} else {
return null
}
} else {
return null
}
} catch (error) {
return null
}
2026-03-16 17:12:52 +07:00
}
export async function getViolationFilesArray(props: {
page?: number,
size?: number,
start_date?: string,
end_date?: string,
file_name?: string
}) {
2026-03-16 17:12:52 +07:00
const token = await getSessionData('token');
const { page, size, start_date, end_date, file_name } = props;
2026-03-16 17:12:52 +07:00
try {
const body: Record<string, any> = {
token: token
};
2026-03-16 17:12:52 +07:00
if (page !== undefined) body.page = page;
if (size !== undefined) body.size = size;
if (start_date !== undefined) body.start_date = start_date;
if (end_date !== undefined) body.end_date = end_date;
if (file_name !== undefined) body.file_name = file_name;
const response = await fetch(`${API_BASE_URL}/api/v1/global-search/violation-summary-with-files`, {
method: 'POST',
2026-03-16 17:12:52 +07:00
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify(body),
2026-03-16 17:12:52 +07:00
});
if (response.ok) {
let parsed = await response.json();
2026-03-27 22:31:34 +07:00
if (parsed.content.length) {
2026-03-16 17:12:52 +07:00
return parsed;
} else {
throw parsed;
}
} else {
throw (`${response.status}`);
}
} catch (error) {
return []
}
}
2026-05-11 14:10:42 +07:00
export async function getFileMatches(fileId: string, page: number, status?: string, violationId?: string) {
2026-03-18 18:09:33 +07:00
const token = await getSessionData('token');
2026-03-16 17:12:52 +07:00
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',
2026-03-27 16:45:58 +07:00
token: token,
status: status,
...(violationId && { action: 'getByViolationId' }),
...(violationId && { violation_id: violationId })
2026-03-16 17:12:52 +07:00
}
}),
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
});
if (response.ok) {
let parsed = await response.json();
2026-03-27 22:31:34 +07:00
2026-03-16 17:12:52 +07:00
if (parsed.message_body) {
return parsed.message_body;
} else {
throw null;
}
} else {
throw (`${response.status}`);
}
} catch (error) {
return null
}
2026-03-18 18:09:33 +07:00
}
2026-03-19 12:49:46 +07:00
export async function fetchViolationAnalyticStatistic(group: 'domain' | 'tld') {
2026-03-18 18:09:33 +07:00
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: {
2026-03-19 12:49:46 +07:00
group_by: group,
action: "group",
token: token,
sort_direction: 'desc'
2026-03-18 18:09:33 +07:00
}
}),
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
});
if (response.ok) {
let parsed = await response.json();
2026-03-19 12:49:46 +07:00
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;
2026-03-18 18:09:33 +07:00
} else {
throw null;
}
} else {
throw (`${response.status}`);
}
} catch (error) {
return null
}
}
2026-04-28 13:08:35 +07:00
export async function fetchViolationFinalSummaryStatistic() {
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: 30023,
message_body: {
token: token,
}
}),
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
});
if (response.ok) {
let parsed = await response.json();
if (parsed?.message_code === 0) {
return parsed?.message_body;
} else {
throw null;
}
} else {
throw (`${response.status}`);
}
} catch (error) {
return null
}
}
interface complainBody {
previousText: string,
2026-03-27 15:10:06 +07:00
violationID: string | null,
success: boolean,
2026-03-31 14:47:39 +07:00
errorMessage?: Record<string, string> | null
}
export async function createComplaint(
state: complainBody | undefined,
formData: FormData
): Promise<complainBody> {
const email = await getSessionData('email');
const token = await getSessionData('token');
const violationId = formData.get('violationId') as string || '';
2026-03-31 14:47:39 +07:00
const textArea = formData.get('note') as string || '';
const validatedFields = createComplaintSchema.safeParse({
textArea
});
if (!validatedFields.success) {
const errors: Record<string, string> = {}
JSON.parse(validatedFields.error.message).forEach((obj: {
message: string,
path: string
}) => {
if (errors[obj.path[0]]) {
errors[obj.path[0]] = `${errors[obj.path[0]]} ${obj.message}`;
} else {
errors[obj.path[0]] = obj.message;
}
});
return {
violationID: violationId,
previousText: textArea,
success: false,
errorMessage: errors
}
}
try {
const response = await fetch(`${API_BASE_URL}/api/v1/data`, {
method: 'POST',
body: JSON.stringify({
version: 1,
msg_id: 30013,
message_body: {
token: token,
action: 'create',
violation_id: violationId,
2026-03-31 14:47:39 +07:00
text: textArea,
email: email
}
}),
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
});
if (response.ok) {
let parsed = await response.json();
if (parsed?.message_code === 0) {
return {
2026-03-27 15:10:06 +07:00
violationID: violationId,
2026-03-31 14:47:39 +07:00
previousText: textArea,
success: true
};
} else {
throw parsed;
}
} else {
throw (`${response.status}`);
}
2026-03-31 14:47:39 +07:00
} catch (error) {
const errors: Record<string, string> = {}
2026-03-27 15:10:06 +07:00
if (error && typeof error === 'object' && 'message_code' in error) {
const err = error as { message_code: number };
if (err.message_code === 2) {
2026-03-31 14:47:39 +07:00
errors['server'] = 'the-complaint-has-not-been-registered'
2026-03-27 15:10:06 +07:00
return {
violationID: violationId,
2026-03-31 14:47:39 +07:00
previousText: textArea,
2026-03-27 15:10:06 +07:00
success: false,
2026-03-31 14:47:39 +07:00
errorMessage: errors
2026-03-27 15:10:06 +07:00
}
}
}
2026-03-31 14:47:39 +07:00
errors['server'] = 'error-save';
2026-03-27 15:10:06 +07:00
return {
2026-03-27 15:10:06 +07:00
violationID: violationId,
2026-03-31 14:47:39 +07:00
previousText: textArea,
2026-03-27 15:10:06 +07:00
success: false,
2026-03-31 14:47:39 +07:00
errorMessage: errors
}
}
}
export async function fetchComplainInfo(id: 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: 30013,
message_body: {
action: 'get_by_violation',
violation_id: id,
token
}
}),
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'
}
}
}
2026-04-25 13:53:30 +07:00
export type MatchStatus = 'NEW' | 'SHOWED' | 'LEGAL_IN_WORK' | 'COMPLAINT_IN_WORK' | 'COMPLAINT_AND_LEGAL_IN_WORK' | 'AUTHORIZED_USE' | 'NOT_OWNER_FILE';
2026-03-27 13:50:41 +07:00
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();
2026-03-27 13:50:41 +07:00
if (parsed?.message_body.status) {
return true
} else {
throw parsed;
}
} else {
throw (`${response.status}`);
}
} catch (error) {
return false
}
2026-04-07 19:05:44 +07:00
}
2026-05-11 14:10:42 +07:00
export async function fetchClaimInfo(id: number) {
2026-04-07 19:05:44 +07:00
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: 30017,
message_body: {
action: 'get_all',
token: token,
2026-05-11 14:10:42 +07:00
pageSize: 1,
2026-04-07 19:05:44 +07:00
pageNumber: 0,
2026-04-20 16:08:10 +07:00
sortBy: "createdAt",
2026-04-07 19:05:44 +07:00
sortDir: "desc",
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 if (parsed?.message_code !== 0 && parsed?.message_desc === 'Law case not found') {
return {
body: null
}
} else {
throw parsed;
}
} else {
throw (`${response.status}`);
}
} catch (error) {
return {
errorMessage: 'error'
}
}
}
interface caseBody {
previousText: string,
previousCaseTitle: string,
previousAmount: string,
2026-04-07 19:05:44 +07:00
violationID: string | null,
success: boolean,
errorMessage?: Record<string, string> | null
}
2026-05-11 14:10:42 +07:00
export async function createClaim(
2026-04-07 19:05:44 +07:00
state: caseBody | undefined,
formData: FormData
): Promise<caseBody> {
const email = await getSessionData('email');
const token = await getSessionData('token');
const violationId = formData.get('violationId') as string || '';
const textArea = formData.get('note') as string || '';
const caseTitle = formData.get('caseTitle') as string || '';
const amount = formData.get('amount') as string || '';
const validatedFields = createClaimSchema.safeParse({
2026-04-07 19:05:44 +07:00
textArea,
caseTitle
});
if (!validatedFields.success) {
const errors: Record<string, string> = {}
JSON.parse(validatedFields.error.message).forEach((obj: {
message: string,
path: string
}) => {
if (errors[obj.path[0]]) {
errors[obj.path[0]] = `${errors[obj.path[0]]} ${obj.message}`;
} else {
errors[obj.path[0]] = obj.message;
}
});
return {
violationID: violationId,
previousText: textArea,
previousCaseTitle: caseTitle,
previousAmount: amount,
2026-04-07 19:05:44 +07:00
success: false,
errorMessage: errors
}
}
try {
const response = await fetch(`${API_BASE_URL}/api/v1/data`, {
method: 'POST',
body: JSON.stringify({
version: 1,
msg_id: 30017,
message_body: {
action: 'create',
violation_id: violationId,
description: textArea,
email: email,
token: token,
name: caseTitle,
priority: 'HIGH',
amount: Number(amount)
}
}),
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
});
if (response.ok) {
let parsed = await response.json();
if (parsed?.message_code === 0) {
return {
violationID: violationId,
previousCaseTitle: caseTitle,
previousText: textArea,
previousAmount: amount,
2026-04-07 19:05:44 +07:00
success: true
};
} else {
throw parsed;
}
} else {
throw (`${response.status}`);
}
} catch (error) {
const errors: Record<string, string> = {}
if (error && typeof error === 'object' && 'message_code' in error) {
const err = error as { message_code: number };
if (err.message_code === 2) {
errors['server'] = 'the-complaint-has-not-been-registered'
return {
violationID: violationId,
previousText: textArea,
previousCaseTitle: caseTitle,
previousAmount: amount,
2026-04-07 19:05:44 +07:00
success: false,
errorMessage: errors
}
}
}
errors['server'] = 'error-save';
return {
violationID: violationId,
previousText: textArea,
previousCaseTitle: caseTitle,
previousAmount: amount,
2026-04-07 19:05:44 +07:00
success: false,
errorMessage: errors
}
}
2026-04-16 17:34:14 +07:00
}
2026-04-20 16:08:10 +07:00
export interface ComplaintsCaseQueryParams {
page?: number,
size?: number,
sort_by?: string,
sort_direction?: 'desc' | 'asc'
}
2026-05-18 11:51:56 +07:00
export async function fetchLastClaims(params: ComplaintsCaseQueryParams) {
2026-04-16 17:34:14 +07:00
const token = await getSessionData('token');
2026-04-20 16:08:10 +07:00
const { page, size, sort_by, sort_direction } = params;
2026-04-16 17:34:14 +07:00
try {
const response = await fetch(`${API_BASE_URL}/api/v1/data`, {
method: 'POST',
body: JSON.stringify({
version: 1,
msg_id: 30017,
message_body: {
action: "get_all",
2026-04-20 16:08:10 +07:00
token: token,
2026-05-18 12:05:12 +07:00
pageNumber: page,
2026-04-20 16:08:10 +07:00
size,
sort_by,
sort_direction
2026-04-16 17:34:14 +07:00
}
}),
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
});
if (response.ok) {
let parsed = await response.json();
2026-04-20 16:08:10 +07:00
if (parsed.message_code === 0) {
return parsed.message_body;
2026-04-16 17:34:14 +07:00
} else {
throw null;
}
} else {
throw (`${response.status}`);
}
} catch (error) {
return null
}
}
2026-04-20 16:08:10 +07:00
export async function fetchLastComplaint(params: ComplaintsCaseQueryParams) {
2026-04-16 17:34:14 +07:00
const token = await getSessionData('token');
2026-04-20 16:08:10 +07:00
const { page, size, sort_by, sort_direction } = params;
2026-04-16 17:34:14 +07:00
try {
const response = await fetch(`${API_BASE_URL}/api/v1/data`, {
method: 'POST',
body: JSON.stringify({
version: 1,
2026-04-20 16:08:10 +07:00
msg_id: 30013,
2026-04-16 17:34:14 +07:00
message_body: {
action: "get_all",
2026-04-20 16:08:10 +07:00
token: token,
page,
size,
sort_by,
sort_direction
2026-04-16 17:34:14 +07:00
}
}),
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
});
if (response.ok) {
let parsed = await response.json();
2026-04-20 16:08:10 +07:00
if (parsed.message_code === 0) {
return parsed.message_body;
2026-04-16 17:34:14 +07:00
} else {
throw null;
}
} else {
throw (`${response.status}`);
}
} catch (error) {
return null
}
2026-03-12 17:44:02 +07:00
}