49 lines
1.6 KiB
TypeScript
49 lines
1.6 KiB
TypeScript
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 };
|
||
}
|
||
}
|