feat(teacher-bff): 添加视口聚合端点 + 修复身份头读取

- 新增 GET /teacher/viewports 聚合 IAM 视口配置
- 修复 controller 从 x-user-id header 读取身份(替代 AuthenticatedRequest)
- 添加 zod + @types/express 依赖
This commit is contained in:
SpecialX
2026-07-09 00:49:24 +08:00
parent a2f0ca26ae
commit b2c2f6e567
4 changed files with 67 additions and 20 deletions

6
pnpm-lock.yaml generated
View File

@@ -488,10 +488,16 @@ importers:
rxjs: rxjs:
specifier: ^7.8.0 specifier: ^7.8.0
version: 7.8.2 version: 7.8.2
zod:
specifier: ^3.23.0
version: 3.25.76
devDependencies: devDependencies:
'@nestjs/cli': '@nestjs/cli':
specifier: ^10.4.0 specifier: ^10.4.0
version: 10.4.9 version: 10.4.9
'@types/express':
specifier: ^4.17.0
version: 4.17.25
'@types/node': '@types/node':
specifier: ^22.0.0 specifier: ^22.0.0
version: 22.20.0 version: 22.20.0

View File

@@ -19,10 +19,12 @@
"prom-client": "^15.1.0", "prom-client": "^15.1.0",
"@opentelemetry/api": "^1.9.0", "@opentelemetry/api": "^1.9.0",
"reflect-metadata": "^0.2.2", "reflect-metadata": "^0.2.2",
"rxjs": "^7.8.0" "rxjs": "^7.8.0",
"zod": "^3.23.0"
}, },
"devDependencies": { "devDependencies": {
"@nestjs/cli": "^10.4.0", "@nestjs/cli": "^10.4.0",
"@types/express": "^4.17.0",
"@types/node": "^22.0.0", "@types/node": "^22.0.0",
"eslint": "^9.10.0", "eslint": "^9.10.0",
"typescript": "^5.6.0", "typescript": "^5.6.0",

View File

@@ -1,19 +1,28 @@
import { Controller, Get, Req } from '@nestjs/common'; import { Controller, Get, Req, UnauthorizedException } from "@nestjs/common";
import type { Request } from 'express'; import type { Request } from "express";
import { TeacherService } from './teacher.service.js'; import { TeacherService } from "./teacher.service.js";
interface AuthenticatedRequest extends Request { @Controller("teacher")
userId?: string;
}
@Controller('teacher')
export class TeacherController { export class TeacherController {
constructor(private readonly service: TeacherService) {} constructor(private readonly service: TeacherService) {}
@Get('dashboard') @Get("dashboard")
async dashboard(@Req() req: Request) { async dashboard(@Req() req: Request) {
const authReq = req as AuthenticatedRequest; const userId = req.headers["x-user-id"] as string;
const data = await this.service.getDashboard(authReq.userId as string); if (!userId) {
return { success: true, data }; throw new UnauthorizedException("Missing x-user-id header");
}
const data = await this.service.getDashboard(userId);
return { success: true as const, data };
}
@Get("viewports")
async viewports(@Req() req: Request) {
const userId = req.headers["x-user-id"] as string;
if (!userId) {
throw new UnauthorizedException("Missing x-user-id header");
}
const data = await this.service.getViewports(userId);
return { success: true as const, data };
} }
} }

View File

@@ -1,22 +1,52 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from "@nestjs/common";
import { env } from '../config/env.js'; import { env } from "../config/env.js";
export interface ViewportItem {
key: string;
label: string;
route: string;
icon: string | null;
sortOrder: string;
requiredPermission: string | null;
}
@Injectable() @Injectable()
export class TeacherService { export class TeacherService {
// 聚合 IAM + classes 服务的数据 // 聚合 IAM + classes 服务的数据
async getDashboard(userId: string): Promise<unknown> { async getDashboard(userId: string): Promise<{
user: unknown;
classes: unknown;
}> {
const [iamRes, classesRes] = await Promise.allSettled([ const [iamRes, classesRes] = await Promise.allSettled([
fetch(`${env.IamServiceUrl}/iam/me`, { fetch(`${env.IamServiceUrl}/iam/me`, {
headers: { 'x-user-id': userId }, headers: { "x-user-id": userId },
}), }),
fetch(`${env.ClassesServiceUrl}/classes`, { fetch(`${env.ClassesServiceUrl}/classes`, {
headers: { 'x-user-id': userId }, headers: { "x-user-id": userId },
}), }),
]); ]);
return { return {
user: iamRes.status === 'fulfilled' ? await iamRes.value.json() : null, user: iamRes.status === "fulfilled" ? await iamRes.value.json() : null,
classes: classesRes.status === 'fulfilled' ? await classesRes.value.json() : null, classes:
classesRes.status === "fulfilled"
? await classesRes.value.json()
: null,
}; };
} }
// 聚合 IAM 视口配置L1 导航)
async getViewports(userId: string): Promise<ViewportItem[]> {
const res = await fetch(`${env.IamServiceUrl}/iam/viewports`, {
headers: { "x-user-id": userId },
});
if (!res.ok) {
return [];
}
const json = (await res.json()) as {
success: boolean;
data?: ViewportItem[];
};
return json.data ?? [];
}
} }