156 lines
4.2 KiB
TypeScript
156 lines
4.2 KiB
TypeScript
import type {
|
|
IncentiveBalance,
|
|
IncentiveLedgerItem,
|
|
QuestionBoardAnswer,
|
|
QuestionBoardItem,
|
|
QuestionBoardTab
|
|
} from '@/types/api';
|
|
import { apiRequest } from './api';
|
|
|
|
type RawAnswer = {
|
|
id: number;
|
|
questionId: number;
|
|
authorName: string;
|
|
content: string;
|
|
status: string;
|
|
best: boolean;
|
|
mine: boolean;
|
|
createdAt: string;
|
|
};
|
|
|
|
type RawQuestion = {
|
|
id: number;
|
|
type: string;
|
|
category: string;
|
|
title: string;
|
|
content: string;
|
|
authorName: string;
|
|
rewardPoints: number;
|
|
rewardAccountType: string;
|
|
ruleCode: string;
|
|
deadline?: string | null;
|
|
status: string;
|
|
bestAnswerId?: number | null;
|
|
answerCount: number;
|
|
mine: boolean;
|
|
createdAt: string;
|
|
answers?: RawAnswer[];
|
|
};
|
|
|
|
type RawPoints = {
|
|
balance: number;
|
|
pointsBalance: number;
|
|
studyCreditBalance: number;
|
|
entries: Array<{
|
|
id: number;
|
|
type: string;
|
|
businessId: number;
|
|
points: number;
|
|
accountType: 'POINTS' | 'STUDY_CREDIT' | string;
|
|
reason: string;
|
|
createdAt: string;
|
|
}>;
|
|
};
|
|
|
|
const normalizeAnswer = (item: RawAnswer): QuestionBoardAnswer => ({
|
|
id: item.id,
|
|
questionId: item.questionId,
|
|
content: item.content,
|
|
authorName: item.authorName,
|
|
status: item.status,
|
|
best: item.best,
|
|
mine: item.mine,
|
|
recommended: false,
|
|
createTime: item.createdAt
|
|
});
|
|
|
|
const normalizeQuestion = (item: RawQuestion): QuestionBoardItem => ({
|
|
id: item.id,
|
|
type: item.type,
|
|
title: item.title,
|
|
content: item.content,
|
|
category: item.category || '工作求助',
|
|
authorName: item.authorName,
|
|
status: item.status,
|
|
rewardPoints: item.rewardPoints || 0,
|
|
rewardAccountType: item.rewardAccountType || 'POINTS',
|
|
answerCount: item.answerCount || 0,
|
|
deadline: item.deadline || null,
|
|
createTime: item.createdAt,
|
|
bestAnswerId: item.bestAnswerId || null,
|
|
mine: item.mine,
|
|
answers: (item.answers || []).map(normalizeAnswer)
|
|
});
|
|
|
|
export const listQuestions = async (tab: QuestionBoardTab) => {
|
|
const rows = await apiRequest<RawQuestion[]>({
|
|
url: `/api/aihr/community/questions${tab === 'MINE' ? '?mine=true' : ''}`,
|
|
method: 'GET',
|
|
timeout: 15000
|
|
});
|
|
const normalized = rows.map(normalizeQuestion);
|
|
if (tab === 'UNANSWERED') return normalized.filter((item) => item.answerCount === 0);
|
|
if (tab === 'REWARDED') return normalized.filter((item) => item.rewardPoints > 0);
|
|
return normalized;
|
|
};
|
|
|
|
export const getQuestion = async (id: number) =>
|
|
normalizeQuestion(await apiRequest<RawQuestion>({
|
|
url: `/api/aihr/community/questions/${id}`,
|
|
method: 'GET',
|
|
timeout: 15000
|
|
}));
|
|
|
|
export const publishQuestion = (data: { title: string; content: string; category: string }) =>
|
|
apiRequest<RawQuestion>({
|
|
url: '/api/aihr/community/questions',
|
|
method: 'POST',
|
|
data: data,
|
|
timeout: 15000
|
|
}).then(normalizeQuestion);
|
|
|
|
export const answerQuestion = (questionId: number, content: string) =>
|
|
apiRequest<RawAnswer>({
|
|
url: `/api/aihr/community/questions/${questionId}/answers`,
|
|
method: 'POST',
|
|
data: { content },
|
|
timeout: 15000
|
|
}).then(normalizeAnswer);
|
|
|
|
export const chooseBestAnswer = (questionId: number, answerId: number) =>
|
|
apiRequest<unknown>({
|
|
url: `/api/aihr/community/questions/${questionId}/best-answer`,
|
|
method: 'POST',
|
|
data: { answerId },
|
|
timeout: 15000
|
|
});
|
|
|
|
export const getMyIncentiveBalances = async (): Promise<IncentiveBalance[]> => {
|
|
const result = await apiRequest<RawPoints>({
|
|
url: '/api/aihr/community/points/me',
|
|
method: 'GET',
|
|
timeout: 15000
|
|
});
|
|
return [
|
|
{ accountType: 'POINTS', displayName: '奖励积分', balance: result.pointsBalance ?? result.balance ?? 0, unitName: '积分' },
|
|
{ accountType: 'STUDY_CREDIT', displayName: '学习学分', balance: result.studyCreditBalance || 0, unitName: '学分' }
|
|
];
|
|
};
|
|
|
|
export const getMyIncentiveLedger = async (): Promise<IncentiveLedgerItem[]> => {
|
|
const result = await apiRequest<RawPoints>({
|
|
url: '/api/aihr/community/points/me',
|
|
method: 'GET',
|
|
timeout: 15000
|
|
});
|
|
return (result.entries || []).map((item) => ({
|
|
id: item.id,
|
|
accountType: item.accountType,
|
|
points: item.points,
|
|
direction: item.points < 0 ? 'REVERSE' : 'EARN',
|
|
sourceType: item.type,
|
|
remark: item.reason,
|
|
createTime: item.createdAt
|
|
}));
|
|
};
|