|
|
@@ -1,6 +1,13 @@
|
|
|
import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify'
|
|
|
import { UserService } from '../services/user.service'
|
|
|
-import { ListUserQuery, LoginBody, RegisterBody, ResetPasswordBody } from '../dto/user.dto'
|
|
|
+import {
|
|
|
+ ListUserQuery,
|
|
|
+ LoginBody,
|
|
|
+ RegisterBody,
|
|
|
+ ResetPasswordBody,
|
|
|
+ CreateUserBody,
|
|
|
+ UpdateUserBody
|
|
|
+} from '../dto/user.dto'
|
|
|
|
|
|
export class UserController {
|
|
|
private userService: UserService
|
|
|
@@ -92,8 +99,92 @@ export class UserController {
|
|
|
|
|
|
async list(request: FastifyRequest<{ Querystring: ListUserQuery }>, reply: FastifyReply) {
|
|
|
try {
|
|
|
- const users = await this.userService.list(request.query.page, request.query.size, request.query.role)
|
|
|
- return reply.send(users)
|
|
|
+ const { user, query } = request
|
|
|
+
|
|
|
+ if (!user) {
|
|
|
+ return reply.code(403).send({
|
|
|
+ error: 'Forbidden',
|
|
|
+ message: 'Unauthorized access'
|
|
|
+ })
|
|
|
+ }
|
|
|
+ const parentId = user.id
|
|
|
+ const { page, size } = query
|
|
|
+
|
|
|
+ const result = await this.userService.findAllChildUsers(parentId, page, size)
|
|
|
+
|
|
|
+ return reply.send(result)
|
|
|
+ } catch (error) {
|
|
|
+ return reply.code(500).send(error)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async createUser(request: FastifyRequest<{ Body: CreateUserBody }>, reply: FastifyReply) {
|
|
|
+ try {
|
|
|
+ const { password, name, role } = request.body
|
|
|
+
|
|
|
+ const existingUser = await this.userService.findByName(name)
|
|
|
+ if (existingUser) {
|
|
|
+ return reply.code(400).send({ message: '用户名已存在' })
|
|
|
+ }
|
|
|
+
|
|
|
+ const user = await this.userService.create(password, name, role, request.user.id)
|
|
|
+
|
|
|
+ return reply.code(201).send({
|
|
|
+ user: {
|
|
|
+ id: user.id,
|
|
|
+ name: user.name,
|
|
|
+ role: user.role,
|
|
|
+ createdAt: user.createdAt,
|
|
|
+ updatedAt: user.updatedAt
|
|
|
+ }
|
|
|
+ })
|
|
|
+ } catch (error) {
|
|
|
+ return reply.code(500).send(error)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async updateUser(request: FastifyRequest<{ Body: UpdateUserBody }>, reply: FastifyReply) {
|
|
|
+ try {
|
|
|
+ const { id, name, password, role } = request.body
|
|
|
+
|
|
|
+ try {
|
|
|
+ await this.userService.findById(id)
|
|
|
+ } catch (error) {
|
|
|
+ return reply.code(404).send({ message: '用户不存在' })
|
|
|
+ }
|
|
|
+
|
|
|
+ if (name) {
|
|
|
+ const existingUser = await this.userService.findByName(name)
|
|
|
+ if (existingUser && existingUser.id !== id) {
|
|
|
+ return reply.code(400).send({ message: '用户名已存在' })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ const updatedUser = await this.userService.updateUser(id, { name, password, role })
|
|
|
+
|
|
|
+ return reply.send({
|
|
|
+ user: {
|
|
|
+ id: updatedUser.id,
|
|
|
+ name: updatedUser.name,
|
|
|
+ role: updatedUser.role,
|
|
|
+ createdAt: updatedUser.createdAt,
|
|
|
+ updatedAt: updatedUser.updatedAt
|
|
|
+ }
|
|
|
+ })
|
|
|
+ } catch (error) {
|
|
|
+ return reply.code(500).send(error)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async getChildApiUsers(request: FastifyRequest<{ Querystring: { parentId?: number } }>, reply: FastifyReply) {
|
|
|
+ try {
|
|
|
+ const parentId = request.query.parentId || request.user.id
|
|
|
+
|
|
|
+ const childUsers = await this.userService.findChildChannelUsers(parentId)
|
|
|
+
|
|
|
+ return reply.send({
|
|
|
+ users: childUsers
|
|
|
+ })
|
|
|
} catch (error) {
|
|
|
return reply.code(500).send(error)
|
|
|
}
|