feat(teacher-bff): 添加视口聚合端点 + 修复身份头读取
- 新增 GET /teacher/viewports 聚合 IAM 视口配置 - 修复 controller 从 x-user-id header 读取身份(替代 AuthenticatedRequest) - 添加 zod + @types/express 依赖
This commit is contained in:
@@ -1,19 +1,28 @@
|
||||
import { Controller, Get, Req } from '@nestjs/common';
|
||||
import type { Request } from 'express';
|
||||
import { TeacherService } from './teacher.service.js';
|
||||
import { Controller, Get, Req, UnauthorizedException } from "@nestjs/common";
|
||||
import type { Request } from "express";
|
||||
import { TeacherService } from "./teacher.service.js";
|
||||
|
||||
interface AuthenticatedRequest extends Request {
|
||||
userId?: string;
|
||||
}
|
||||
|
||||
@Controller('teacher')
|
||||
@Controller("teacher")
|
||||
export class TeacherController {
|
||||
constructor(private readonly service: TeacherService) {}
|
||||
|
||||
@Get('dashboard')
|
||||
@Get("dashboard")
|
||||
async dashboard(@Req() req: Request) {
|
||||
const authReq = req as AuthenticatedRequest;
|
||||
const data = await this.service.getDashboard(authReq.userId as string);
|
||||
return { success: true, data };
|
||||
const userId = req.headers["x-user-id"] as string;
|
||||
if (!userId) {
|
||||
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 };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1,52 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { env } from '../config/env.js';
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { env } from "../config/env.js";
|
||||
|
||||
export interface ViewportItem {
|
||||
key: string;
|
||||
label: string;
|
||||
route: string;
|
||||
icon: string | null;
|
||||
sortOrder: string;
|
||||
requiredPermission: string | null;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class TeacherService {
|
||||
// 聚合 IAM + classes 服务的数据
|
||||
async getDashboard(userId: string): Promise<unknown> {
|
||||
async getDashboard(userId: string): Promise<{
|
||||
user: unknown;
|
||||
classes: unknown;
|
||||
}> {
|
||||
const [iamRes, classesRes] = await Promise.allSettled([
|
||||
fetch(`${env.IamServiceUrl}/iam/me`, {
|
||||
headers: { 'x-user-id': userId },
|
||||
headers: { "x-user-id": userId },
|
||||
}),
|
||||
fetch(`${env.ClassesServiceUrl}/classes`, {
|
||||
headers: { 'x-user-id': userId },
|
||||
headers: { "x-user-id": userId },
|
||||
}),
|
||||
]);
|
||||
|
||||
return {
|
||||
user: iamRes.status === 'fulfilled' ? await iamRes.value.json() : null,
|
||||
classes: classesRes.status === 'fulfilled' ? await classesRes.value.json() : null,
|
||||
user: iamRes.status === "fulfilled" ? await iamRes.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 ?? [];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user