feat(users): 拆分 C 端身份并迁移历史聊天

- digital.users: 独立开户登录与凭据版本校验,后台查看用户及原始会话
- 017迁移: 保留聊天消息、实验分组和人工指定期限,迁移历史账号归属
- tests: 覆盖身份隔离、凭据失效、会话归属和长账号登录,同步 README
This commit is contained in:
湛兮
2026-10-27 18:56:19 +08:00
parent 57c89f4f32
commit b9d1a7f6bd
14 changed files with 747 additions and 32 deletions
+7 -3
View File
@@ -23,6 +23,7 @@ import {
conflict,
forbidden,
notFound,
unauthorized,
} from "../../shared/http-error";
import { rows, write, json, audit, transaction } from "./digital.repository";
import {
@@ -53,7 +54,7 @@ import {
type ContentRow,
} from "./digital.chat";
const params = z.object({ id: idSchema });
const accountSchema = z.string().regex(/^(employee|super_admin):[0-9]+$/);
const accountSchema = z.string().regex(/^user:[0-9]+$/);
const adminGuard = async (r: FastifyRequest) => {
await authGuard(r);
const u = await authService.getCurrentUser(r.user);
@@ -61,9 +62,12 @@ const adminGuard = async (r: FastifyRequest) => {
};
const userGuard = async (r: FastifyRequest) => {
await authGuard(r);
// 旧管理员/员工前台 Cookie 失效时走正常退出流程,不能继续读取迁移后的聊天。
if (r.user.accountType !== "DIGITAL_USER" || r.user.scene !== "DIGITAL_APP")
throw unauthorized("请使用独立 C 端账号重新登录");
await authService.getCurrentUser(r.user);
};
/** 已存在的账号体系是唯一身份来源,忽略请求体中的用户标识。 */
/** 独立 C 端令牌是唯一用户身份来源,忽略请求体中的用户标识。 */
export async function digitalRoutes(app: FastifyInstance) {
app.register(
async (front) => {
@@ -477,7 +481,7 @@ export async function digitalRoutes(app: FastifyInstance) {
admin.get("/accounts", async () =>
ok(
await rows(
"SELECT CONCAT('employee:',id) account,name,created_at AS createdAt FROM employees WHERE status='ACTIVE' AND deleted_at IS NULL UNION ALL SELECT CONCAT('super_admin:',id),display_name,created_at FROM super_admins WHERE status='ACTIVE' LIMIT 500",
"SELECT CONCAT('user:',id) account,display_name name,created_at AS createdAt FROM dh_users WHERE status='ACTIVE' ORDER BY id LIMIT 500",
),
),
);