continue: create case
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
'use server'
|
||||
|
||||
import { getSessionData } from '@/app/actions/session';
|
||||
import { API_BASE_URL, createComplaintSchema } from '@/app/actions/definitions';
|
||||
import { API_BASE_URL, createComplaintSchema, createCaseSchema } from '@/app/actions/definitions';
|
||||
|
||||
export async function getViolationSearchStatus() {
|
||||
const token = await getSessionData('token');
|
||||
@@ -440,4 +440,176 @@ export async function updateMatchStatus(id: number, status: MatchStatus) {
|
||||
} catch (error) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
export async function fetchCaseInfo(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_byId',
|
||||
token: token,
|
||||
id: id
|
||||
}
|
||||
}), */
|
||||
body: JSON.stringify({
|
||||
version: 1,
|
||||
msg_id: 30017,
|
||||
message_body: {
|
||||
action: 'get_all',
|
||||
token: token,
|
||||
pageSize: 10,
|
||||
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,
|
||||
violationID: string | null,
|
||||
success: boolean,
|
||||
errorMessage?: Record<string, string> | null
|
||||
}
|
||||
|
||||
export async function createCase(
|
||||
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 = createCaseSchema.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,
|
||||
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,
|
||||
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,
|
||||
success: false,
|
||||
errorMessage: errors
|
||||
}
|
||||
}
|
||||
}
|
||||
errors['server'] = 'error-save';
|
||||
|
||||
return {
|
||||
violationID: violationId,
|
||||
previousText: textArea,
|
||||
previousCaseTitle: caseTitle,
|
||||
success: false,
|
||||
errorMessage: errors
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user