Merge branch 'dev' into hugo-shipping-notice
This commit is contained in:
commit
9f5493185d
29 files changed
+3957
-12
No files matched your search
@@ -0,0 +1,200 @@
|
||||
/**
|
||||
* features/chat/api/training.api.ts
|
||||
* 用途:培训工作区专用 HTTP API,负责员工进度读取/保存、HR 记录查询、统计和导出。
|
||||
* 依赖:shared/api/client、auth headers、training.types。
|
||||
* 逻辑:把后端 training 领域的 snake_case 返回值统一映射成前端工作区需要的 camelCase 结构。
|
||||
*/
|
||||
import { requestJson } from '@/shared/api/client';
|
||||
import { getAuthHeaders } from '@/shared/utils/auth';
|
||||
import type { TrainingAnswerRecord, TrainingProgressRecord, TrainingRecord, TrainingStage, TrainingStats } from '../training/training.types';
|
||||
|
||||
type TrainingRecordApi = {
|
||||
id: number;
|
||||
user_id?: number;
|
||||
employee_id?: string | null;
|
||||
name?: string | null;
|
||||
department?: string | null;
|
||||
join_date?: string | null;
|
||||
stage?: TrainingStage;
|
||||
current_question_index?: number;
|
||||
score?: number;
|
||||
passed?: boolean;
|
||||
attempt_count?: number;
|
||||
answer_details?: Array<{
|
||||
question_id?: string;
|
||||
selected_option_id?: string;
|
||||
correct?: boolean;
|
||||
}>;
|
||||
completion_time?: string | null;
|
||||
updated_at?: string | null;
|
||||
started_at?: string | null;
|
||||
status?: TrainingRecord['status'];
|
||||
};
|
||||
|
||||
type TrainingListApi = {
|
||||
items: TrainingRecordApi[];
|
||||
total: number;
|
||||
page: number;
|
||||
page_size: number;
|
||||
pages: number;
|
||||
department_options: string[];
|
||||
};
|
||||
|
||||
type TrainingStatsApi = {
|
||||
total: number;
|
||||
passed: number;
|
||||
failed: number;
|
||||
in_progress: number;
|
||||
pass_rate: number;
|
||||
average_score: number;
|
||||
};
|
||||
|
||||
export interface TrainingRecordQuery {
|
||||
keyword?: string;
|
||||
department?: string;
|
||||
status?: 'all' | 'passed' | 'failed' | 'in_progress';
|
||||
page?: number;
|
||||
page_size?: number;
|
||||
}
|
||||
|
||||
export interface SaveTrainingProgressPayload {
|
||||
stage: TrainingStage;
|
||||
currentQuestionIndex: number;
|
||||
score: number;
|
||||
passed: boolean;
|
||||
attemptCount: number;
|
||||
answerDetails: TrainingAnswerRecord[];
|
||||
joinDate?: string | null;
|
||||
}
|
||||
|
||||
const authHeaders = () => ({ headers: getAuthHeaders() });
|
||||
const jsonAuthHeaders = () => ({ headers: { 'Content-Type': 'application/json', ...getAuthHeaders() } });
|
||||
|
||||
function mapAnswerDetails(items?: TrainingRecordApi['answer_details']): TrainingAnswerRecord[] {
|
||||
return Array.isArray(items)
|
||||
? items.map((item) => ({
|
||||
questionId: String(item.question_id || ''),
|
||||
selectedOptionId: String(item.selected_option_id || ''),
|
||||
correct: Boolean(item.correct),
|
||||
}))
|
||||
: [];
|
||||
}
|
||||
|
||||
function mapTrainingProgressRecord(data: TrainingRecordApi): TrainingProgressRecord {
|
||||
return {
|
||||
id: data.id,
|
||||
name: String(data.name || ''),
|
||||
employeeId: String(data.employee_id || ''),
|
||||
department: String(data.department || ''),
|
||||
joinDate: String(data.join_date || ''),
|
||||
completionDate: data.completion_time ? String(data.completion_time).slice(0, 10) : null,
|
||||
score: typeof data.score === 'number' ? data.score : null,
|
||||
attempts: Number(data.attempt_count || 0),
|
||||
status: data.status || 'in_progress',
|
||||
stage: (data.stage || 'video') as TrainingStage,
|
||||
currentQuestionIndex: Number(data.current_question_index || 0),
|
||||
passed: Boolean(data.passed),
|
||||
answerDetails: mapAnswerDetails(data.answer_details),
|
||||
startedAt: data.started_at || null,
|
||||
completionTime: data.completion_time || null,
|
||||
updatedAt: data.updated_at || null,
|
||||
};
|
||||
}
|
||||
|
||||
function mapTrainingRecord(data: TrainingRecordApi): TrainingRecord {
|
||||
const progress = mapTrainingProgressRecord(data);
|
||||
return {
|
||||
id: progress.id,
|
||||
name: progress.name,
|
||||
employeeId: progress.employeeId,
|
||||
department: progress.department,
|
||||
joinDate: progress.joinDate,
|
||||
completionDate: progress.completionDate,
|
||||
score: progress.score,
|
||||
attempts: progress.attempts,
|
||||
status: progress.status,
|
||||
};
|
||||
}
|
||||
|
||||
function buildQueryString(query: TrainingRecordQuery = {}) {
|
||||
const params = new URLSearchParams();
|
||||
if (query.keyword) params.set('keyword', query.keyword);
|
||||
if (query.department) params.set('department', query.department);
|
||||
if (query.status) params.set('status', query.status);
|
||||
if (query.page) params.set('page', String(query.page));
|
||||
if (query.page_size) params.set('page_size', String(query.page_size));
|
||||
return params.toString();
|
||||
}
|
||||
|
||||
export async function getMyTrainingProgress(): Promise<TrainingProgressRecord | null> {
|
||||
const data = await requestJson<TrainingRecordApi | null>('/api/user/training-record', authHeaders());
|
||||
return data ? mapTrainingProgressRecord(data) : null;
|
||||
}
|
||||
|
||||
export async function saveMyTrainingProgress(payload: SaveTrainingProgressPayload): Promise<TrainingProgressRecord> {
|
||||
const data = await requestJson<TrainingRecordApi>('/api/user/training-record/progress', {
|
||||
method: 'PUT',
|
||||
...jsonAuthHeaders(),
|
||||
body: JSON.stringify({
|
||||
stage: payload.stage,
|
||||
current_question_index: payload.currentQuestionIndex,
|
||||
score: payload.score,
|
||||
passed: payload.passed,
|
||||
attempt_count: payload.attemptCount,
|
||||
answer_details: payload.answerDetails.map((item) => ({
|
||||
question_id: item.questionId,
|
||||
selected_option_id: item.selectedOptionId,
|
||||
correct: item.correct,
|
||||
})),
|
||||
join_date: payload.joinDate || undefined,
|
||||
}),
|
||||
});
|
||||
return mapTrainingProgressRecord(data);
|
||||
}
|
||||
|
||||
export async function listTrainingRecords(query: TrainingRecordQuery = {}) {
|
||||
const qs = buildQueryString(query);
|
||||
const data = await requestJson<TrainingListApi>(`/api/admin/training-records${qs ? `?${qs}` : ''}`, authHeaders());
|
||||
return {
|
||||
items: data.items.map(mapTrainingRecord),
|
||||
total: data.total,
|
||||
page: data.page,
|
||||
pageSize: data.page_size,
|
||||
pages: data.pages,
|
||||
departmentOptions: data.department_options || [],
|
||||
};
|
||||
}
|
||||
|
||||
export async function getTrainingStats(query: TrainingRecordQuery = {}): Promise<TrainingStats> {
|
||||
const qs = buildQueryString(query);
|
||||
const data = await requestJson<TrainingStatsApi>(`/api/admin/training-records/stats${qs ? `?${qs}` : ''}`, authHeaders());
|
||||
return {
|
||||
total: Number(data.total || 0),
|
||||
passed: Number(data.passed || 0),
|
||||
failed: Number(data.failed || 0),
|
||||
inProgress: Number(data.in_progress || 0),
|
||||
passRate: Number(data.pass_rate || 0),
|
||||
averageScore: Number(data.average_score || 0),
|
||||
};
|
||||
}
|
||||
|
||||
export async function exportTrainingRecords(query: TrainingRecordQuery = {}): Promise<void> {
|
||||
const qs = buildQueryString(query);
|
||||
const response = await fetch(`/api/admin/training-records/export${qs ? `?${qs}` : ''}`, {
|
||||
method: 'GET',
|
||||
headers: getAuthHeaders(),
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(`Export failed: HTTP ${response.status}`);
|
||||
}
|
||||
|
||||
const blob = await response.blob();
|
||||
const url = URL.createObjectURL(blob);
|
||||
const link = document.createElement('a');
|
||||
link.href = url;
|
||||
link.download = `training_records_${new Date().toISOString().slice(0, 10)}.xlsx`;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
link.remove();
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
Reference in new issue
Block a user