86 lines
2.9 KiB
TypeScript
86 lines
2.9 KiB
TypeScript
/**
|
|
* 按任务保存的研判情报报告详情。
|
|
*
|
|
* 后端 `/api/task-reports/by-task/{taskId}` 返回的外壳和原 Consumer
|
|
* `selectSqReport` 完全一致:`{ state, msg, data }`。其中 `contentJson`
|
|
* 保持为 JSON 字符串,由使用方按 `categoryType` 反序列化。
|
|
*/
|
|
|
|
import { apiFetch } from "@/core/api/fetch-client";
|
|
import { getBackendBaseURL } from "@/core/config";
|
|
|
|
export type TaskReportTaskId = string | number;
|
|
|
|
export interface TaskReportRecord {
|
|
id: string;
|
|
taskId: TaskReportTaskId;
|
|
createTime: string;
|
|
updateTime: string;
|
|
sessionId: string;
|
|
contentJson: string;
|
|
categoryType: string;
|
|
}
|
|
|
|
export interface TaskReportResponse {
|
|
state: string;
|
|
msg: string;
|
|
data: TaskReportRecord[];
|
|
}
|
|
|
|
/** 保存/修改时可省略由后端生成或维护的 id、创建/更新时间。 */
|
|
export type TaskReportRecordDraft = Omit<
|
|
TaskReportRecord,
|
|
"id" | "taskId" | "createTime" | "updateTime"
|
|
> &
|
|
Partial<Pick<TaskReportRecord, "id" | "taskId" | "createTime" | "updateTime">>;
|
|
|
|
const BASE = () => `${getBackendBaseURL()}/api/task-reports/by-task`;
|
|
|
|
function endpoint(taskId: TaskReportTaskId) {
|
|
return `${BASE()}/${encodeURIComponent(String(taskId))}`;
|
|
}
|
|
|
|
async function readResponse(res: Response, context: string): Promise<TaskReportResponse> {
|
|
const body = (await res.json().catch(() => null)) as TaskReportResponse | { detail?: unknown } | null;
|
|
if (!res.ok) {
|
|
const detail = body && typeof body === "object" && "detail" in body ? String(body.detail) : "";
|
|
throw new Error(detail || `${context} failed (${res.status})`);
|
|
}
|
|
if (!body || !Array.isArray((body as TaskReportResponse).data)) {
|
|
throw new Error(`${context} returned an invalid report payload`);
|
|
}
|
|
return body as TaskReportResponse;
|
|
}
|
|
|
|
/** 查询任务详情;尚未保存时后端返回 `{ state: "200", data: [] }`。 */
|
|
export async function getTaskReport(taskId: TaskReportTaskId): Promise<TaskReportResponse> {
|
|
const res = await apiFetch(endpoint(taskId), { method: "GET" });
|
|
return readResponse(res, "get task report");
|
|
}
|
|
|
|
/** 首次保存一个任务的完整 `data` 数组。任务已有详情时会返回 409。 */
|
|
export async function saveTaskReport(
|
|
taskId: TaskReportTaskId,
|
|
data: TaskReportRecordDraft[],
|
|
): Promise<TaskReportResponse> {
|
|
const res = await apiFetch(endpoint(taskId), {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
return readResponse(res, "save task report");
|
|
}
|
|
|
|
/** 修改一个任务的完整 `data` 数组。保留同 id 项的原始 createTime。 */
|
|
export async function updateTaskReport(
|
|
taskId: TaskReportTaskId,
|
|
data: TaskReportRecordDraft[],
|
|
): Promise<TaskReportResponse> {
|
|
const res = await apiFetch(endpoint(taskId), {
|
|
method: "PUT",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ data }),
|
|
});
|
|
return readResponse(res, "update task report");
|
|
}
|