Files
Edu/services/msg/src/preferences/preferences.controller.ts
SpecialX 7b7abbb309 feat(msg): 完整实现 msg 消息服务
包含 channels/preferences/templates/grpc/kafka/outbox/push/redis 等完整实现
2026-07-10 19:09:52 +08:00

49 lines
1.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { Body, Controller, Get, Param, Put } from "@nestjs/common";
import { PreferencesService } from "./preferences.service.js";
import { updatePreferencesSchema } from "./preferences.dto.js";
import type { UpdatePreferencesDto } from "./preferences.dto.js";
import {
Permissions,
RequirePermission,
} from "../middleware/permission.guard.js";
/**
* PreferencesController —— 用户通知偏好 REST API。
*
* 对齐 02-architecture-design.md §4.1
* - GET /preferences/user/:userId查询偏好
* - PUT /preferences/user/:userId更新偏好
*/
@Controller("preferences")
export class PreferencesController {
constructor(private readonly service: PreferencesService) {}
@Get("user/:userId")
@RequirePermission(Permissions.MSG_NOTIFICATION_READ)
async getByUserId(
@Param("userId") userId: string,
): Promise<{ success: true; data: unknown }> {
const preferences = await this.service.getByUserId(userId);
return { success: true, data: { preferences } };
}
@Put("user/:userId")
@RequirePermission(Permissions.MSG_NOTIFICATION_MANAGE)
async update(
@Param("userId") userId: string,
@Body() body: unknown,
): Promise<{ success: true }> {
// body 来自 HTTP 请求体,类型为 unknown转 Record 以便合并 userId从 unknown 转换允许 as
const obj: Record<string, unknown> =
typeof body === "object" && body !== null
? (body as Record<string, unknown>)
: {};
const dto: UpdatePreferencesDto = updatePreferencesSchema.parse({
...obj,
userId,
});
await this.service.update(dto);
return { success: true };
}
}