722 lines
15 KiB
TypeScript
722 lines
15 KiB
TypeScript
'use server'
|
|
|
|
import { getSessionData } from '@/app/actions/session';
|
|
import { API_BASE_URL, createComplaintSchema, createClaimSchema } 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');
|
|
|
|
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();
|
|
|
|
if (parsed?.status === 'ACCEPTED') {
|
|
return parsed
|
|
} else {
|
|
return null
|
|
}
|
|
|
|
} else {
|
|
return null
|
|
}
|
|
} catch (error) {
|
|
return null
|
|
}
|
|
}
|
|
|
|
export async function getViolationFilesArray(props: {
|
|
page?: number,
|
|
size?: number,
|
|
start_date?: string,
|
|
end_date?: string,
|
|
file_name?: string
|
|
}) {
|
|
const token = await getSessionData('token');
|
|
const { page, size, start_date, end_date, file_name } = props;
|
|
|
|
try {
|
|
const body: Record<string, any> = {
|
|
token: token
|
|
};
|
|
|
|
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',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Accept': 'application/json',
|
|
},
|
|
body: JSON.stringify(body),
|
|
});
|
|
|
|
if (response.ok) {
|
|
let parsed = await response.json();
|
|
|
|
if (parsed.content.length) {
|
|
return parsed;
|
|
} else {
|
|
throw parsed;
|
|
}
|
|
} else {
|
|
throw (`${response.status}`);
|
|
}
|
|
|
|
} catch (error) {
|
|
return []
|
|
}
|
|
}
|
|
|
|
export async function getFileMatches(fileId: string, page: number, status?: string, violationId?: string, startDate?: string, endDate?: string) {
|
|
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 > 0 ? page - 1 : 0),
|
|
size: 5,
|
|
sort_direction: 'desc',
|
|
token: token,
|
|
status: status,
|
|
...(violationId && { action: 'getByViolationId' }),
|
|
...(violationId && { violation_id: violationId }),
|
|
...((startDate && endDate) && { start_date: startDate, end_date: endDate })
|
|
}
|
|
}),
|
|
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(id: string) {
|
|
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,
|
|
file_id: id
|
|
}
|
|
}),
|
|
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 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,
|
|
violationID: string | null,
|
|
success: boolean,
|
|
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 || '';
|
|
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,
|
|
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 {
|
|
violationID: violationId,
|
|
previousText: textArea,
|
|
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,
|
|
success: false,
|
|
errorMessage: errors
|
|
}
|
|
}
|
|
}
|
|
errors['server'] = 'error-save';
|
|
|
|
return {
|
|
violationID: violationId,
|
|
previousText: textArea,
|
|
success: false,
|
|
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'
|
|
}
|
|
}
|
|
}
|
|
|
|
export type MatchStatus = 'NEW' | 'SHOWED' | 'LEGAL_IN_WORK' | 'COMPLAINT_IN_WORK' | 'COMPLAINT_AND_LEGAL_IN_WORK' | 'AUTHORIZED_USE' | 'NOT_OWNER_FILE';
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
export async function fetchClaimInfo(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: 30017,
|
|
message_body: {
|
|
action: 'get_all',
|
|
token: token,
|
|
pageSize: 1,
|
|
pageNumber: 0,
|
|
sortBy: "createdAt",
|
|
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,
|
|
violationID: string | null,
|
|
success: boolean,
|
|
errorMessage?: Record<string, string> | null
|
|
}
|
|
|
|
export async function createClaim(
|
|
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({
|
|
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,
|
|
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,
|
|
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,
|
|
success: false,
|
|
errorMessage: errors
|
|
}
|
|
}
|
|
}
|
|
errors['server'] = 'error-save';
|
|
|
|
return {
|
|
violationID: violationId,
|
|
previousText: textArea,
|
|
previousCaseTitle: caseTitle,
|
|
previousAmount: amount,
|
|
success: false,
|
|
errorMessage: errors
|
|
}
|
|
}
|
|
}
|
|
|
|
export interface ComplaintsCaseQueryParams {
|
|
page?: number,
|
|
size?: number,
|
|
sort_by?: string,
|
|
sort_direction?: 'desc' | 'asc'
|
|
}
|
|
|
|
export async function fetchLastClaims(params: ComplaintsCaseQueryParams) {
|
|
const token = await getSessionData('token');
|
|
const { page, size, sort_by, sort_direction } = params;
|
|
|
|
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,
|
|
pageNumber: page,
|
|
size,
|
|
sort_by,
|
|
sort_direction
|
|
}
|
|
}),
|
|
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
|
|
}
|
|
}
|
|
|
|
export async function fetchLastComplaint(params: ComplaintsCaseQueryParams) {
|
|
const token = await getSessionData('token');
|
|
const { page, size, sort_by, sort_direction } = params;
|
|
|
|
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_all",
|
|
token: token,
|
|
page,
|
|
size,
|
|
sort_by,
|
|
sort_direction
|
|
}
|
|
}),
|
|
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
|
|
}
|
|
} |