1. Y-Log 上报 optDetail 前缀 B1-CM助手 → CM助手-全量(全量替换 44 个前端文件 + 文档) 2. 知识库入库 nodeName 改为只取 md5(标题).md(去掉时间戳),同一篇 md 每次入库一致,避免重复记录 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
214 lines
7.5 KiB
TypeScript
214 lines
7.5 KiB
TypeScript
import { apiFetch } from "@/core/api";
|
|
import { getBackendBaseURL } from "@/core/config";
|
|
import { OptType, writeYLog } from "@/core/log/y-log";
|
|
|
|
import type {
|
|
Position,
|
|
PositionCreateInput,
|
|
PositionListResponse,
|
|
PositionMember,
|
|
PositionUpdateInput,
|
|
PositionUser,
|
|
TagBindings,
|
|
TagBindingsResponse,
|
|
} from "./types";
|
|
|
|
async function parseDetail(res: Response, fallback: string): Promise<Error> {
|
|
const data = (await res.json().catch(() => ({}))) as { detail?: unknown };
|
|
const detail = typeof data.detail === "string" && data.detail ? data.detail : null;
|
|
return new Error(detail ?? `${fallback} (HTTP ${res.status})`);
|
|
}
|
|
|
|
const base = () => `${getBackendBaseURL()}/api/positions`;
|
|
|
|
export async function listPositions(): Promise<Position[]> {
|
|
const res = await apiFetch(base());
|
|
if (!res.ok) throw await parseDetail(res, "Failed to list positions");
|
|
const data = (await res.json()) as PositionListResponse;
|
|
writeYLog({
|
|
optType: OptType.search,
|
|
optDetail: "CM助手-全量-岗位-查询",
|
|
url: "/api/positions",
|
|
returnParam: data.positions,
|
|
});
|
|
return data.positions;
|
|
}
|
|
|
|
export async function createPosition(input: PositionCreateInput): Promise<Position> {
|
|
const res = await apiFetch(base(), {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(input),
|
|
});
|
|
if (!res.ok) throw await parseDetail(res, "Failed to create position");
|
|
const created = (await res.json()) as Position;
|
|
writeYLog({
|
|
optType: OptType.create,
|
|
optDetail: "CM助手-全量-岗位-新增",
|
|
url: "/api/positions",
|
|
reqParam: input,
|
|
returnParam: created,
|
|
});
|
|
return created;
|
|
}
|
|
|
|
export async function updatePosition(id: string, input: PositionUpdateInput): Promise<Position> {
|
|
const res = await apiFetch(`${base()}/${encodeURIComponent(id)}`, {
|
|
method: "PUT",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(input),
|
|
});
|
|
if (!res.ok) throw await parseDetail(res, "Failed to update position");
|
|
const updated = (await res.json()) as Position;
|
|
writeYLog({
|
|
optType: OptType.edit,
|
|
optDetail: "CM助手-全量-岗位-编辑",
|
|
url: `/api/positions/${id}`,
|
|
reqParam: input,
|
|
returnParam: updated,
|
|
});
|
|
return updated;
|
|
}
|
|
|
|
export async function deletePosition(id: string): Promise<void> {
|
|
const res = await apiFetch(`${base()}/${encodeURIComponent(id)}`, { method: "DELETE" });
|
|
if (!res.ok) throw await parseDetail(res, "Failed to delete position");
|
|
writeYLog({
|
|
optType: OptType.delete,
|
|
optDetail: "CM助手-全量-岗位-删除",
|
|
url: `/api/positions/${id}`,
|
|
reqParam: { id },
|
|
});
|
|
}
|
|
|
|
export async function getTagBindings(positionId: string): Promise<TagBindings> {
|
|
const res = await apiFetch(`${base()}/${encodeURIComponent(positionId)}/tag-bindings`);
|
|
if (!res.ok) throw await parseDetail(res, "Failed to load tag bindings");
|
|
const data = (await res.json()) as TagBindingsResponse;
|
|
writeYLog({
|
|
optType: OptType.view,
|
|
optDetail: "CM助手-全量-岗位-标签绑定查看",
|
|
url: `/api/positions/${positionId}/tag-bindings`,
|
|
reqParam: { positionId },
|
|
returnParam: data.bindings,
|
|
});
|
|
return data.bindings;
|
|
}
|
|
|
|
export async function setTagBindings(positionId: string, bindings: TagBindings): Promise<TagBindings> {
|
|
const res = await apiFetch(`${base()}/${encodeURIComponent(positionId)}/tag-bindings`, {
|
|
method: "PUT",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ bindings }),
|
|
});
|
|
if (!res.ok) throw await parseDetail(res, "Failed to save tag bindings");
|
|
const data = (await res.json()) as TagBindingsResponse;
|
|
writeYLog({
|
|
optType: OptType.edit,
|
|
optDetail: "CM助手-全量-岗位-标签绑定保存",
|
|
url: `/api/positions/${positionId}/tag-bindings`,
|
|
reqParam: { positionId, bindings },
|
|
returnParam: data.bindings,
|
|
});
|
|
return data.bindings;
|
|
}
|
|
|
|
export async function listMembers(positionId: string): Promise<PositionMember[]> {
|
|
const res = await apiFetch(`${base()}/${encodeURIComponent(positionId)}/members`);
|
|
if (!res.ok) throw await parseDetail(res, "Failed to list members");
|
|
const data = (await res.json()) as { members: PositionMember[] };
|
|
writeYLog({
|
|
optType: OptType.search,
|
|
optDetail: "CM助手-全量-岗位-成员查询",
|
|
url: `/api/positions/${positionId}/members`,
|
|
reqParam: { positionId },
|
|
returnParam: data.members,
|
|
});
|
|
return data.members;
|
|
}
|
|
|
|
export async function addMembers(positionId: string, userIds: string[]): Promise<PositionMember[]> {
|
|
const res = await apiFetch(`${base()}/${encodeURIComponent(positionId)}/members`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ user_ids: userIds }),
|
|
});
|
|
if (!res.ok) throw await parseDetail(res, "Failed to add members");
|
|
const data = (await res.json()) as { members: PositionMember[] };
|
|
writeYLog({
|
|
optType: OptType.create,
|
|
optDetail: "CM助手-全量-岗位-添加成员",
|
|
url: `/api/positions/${positionId}/members`,
|
|
reqParam: { positionId, userIds },
|
|
returnParam: data.members,
|
|
});
|
|
return data.members;
|
|
}
|
|
|
|
export async function removeMembers(positionId: string, userIds: string[]): Promise<PositionMember[]> {
|
|
const res = await apiFetch(`${base()}/${encodeURIComponent(positionId)}/members`, {
|
|
method: "DELETE",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ user_ids: userIds }),
|
|
});
|
|
if (!res.ok) throw await parseDetail(res, "Failed to remove members");
|
|
const data = (await res.json()) as { members: PositionMember[] };
|
|
writeYLog({
|
|
optType: OptType.delete,
|
|
optDetail: "CM助手-全量-岗位-移除成员",
|
|
url: `/api/positions/${positionId}/members`,
|
|
reqParam: { positionId, userIds },
|
|
returnParam: data.members,
|
|
});
|
|
return data.members;
|
|
}
|
|
|
|
export async function resyncPosition(positionId: string): Promise<number> {
|
|
const res = await apiFetch(`${base()}/${encodeURIComponent(positionId)}/resync`, { method: "POST" });
|
|
if (!res.ok) throw await parseDetail(res, "Failed to resync position");
|
|
const data = (await res.json()) as { synced: number };
|
|
writeYLog({
|
|
optType: OptType.other,
|
|
optDetail: "CM助手-全量-岗位-同步",
|
|
url: `/api/positions/${positionId}/resync`,
|
|
reqParam: { positionId },
|
|
returnParam: data,
|
|
});
|
|
return data.synced;
|
|
}
|
|
|
|
export async function getMyPosition(): Promise<Position | null> {
|
|
const res = await apiFetch(`${base()}/me`);
|
|
if (!res.ok) throw await parseDetail(res, "Failed to load my position");
|
|
const data = (await res.json()) as { position: Position | null };
|
|
return data.position ?? null;
|
|
}
|
|
|
|
export async function listPositionUsers(): Promise<PositionUser[]> {
|
|
const res = await apiFetch(`${base()}/users`);
|
|
if (!res.ok) throw await parseDetail(res, "Failed to list users");
|
|
const data = (await res.json()) as { users: PositionUser[] };
|
|
writeYLog({
|
|
optType: OptType.search,
|
|
optDetail: "CM助手-全量-岗位-用户查询",
|
|
url: "/api/positions/users",
|
|
returnParam: data.users,
|
|
});
|
|
return data.users;
|
|
}
|
|
|
|
export async function setUserPosition(userId: string, positionId: string | null): Promise<void> {
|
|
const res = await apiFetch(`${base()}/users/${encodeURIComponent(userId)}`, {
|
|
method: "PUT",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ position_id: positionId }),
|
|
});
|
|
if (!res.ok) throw await parseDetail(res, "Failed to set user position");
|
|
writeYLog({
|
|
optType: OptType.edit,
|
|
optDetail: "CM助手-全量-岗位-设置用户岗位",
|
|
url: `/api/positions/users/${userId}`,
|
|
reqParam: { userId, positionId },
|
|
});
|
|
}
|