From 7887a8d3be3fdbacddbd1110772bbf03634cac58 Mon Sep 17 00:00:00 2001 From: nav Date: Tue, 12 May 2026 08:52:39 +0000 Subject: [PATCH] feat(center-nodes): add node status model and status update endpoint --- .../src/entities/guild-node.entity.ts | 8 ++++-- .../src/nodes/dto.update-node-status.dto.ts | 6 +++++ .../src/nodes/nodes.controller.ts | 25 +++++++++++++++++++ docs/TODO-backend-center-guild.md | 2 +- 4 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 Fabric.Backend.Center/src/nodes/dto.update-node-status.dto.ts diff --git a/Fabric.Backend.Center/src/entities/guild-node.entity.ts b/Fabric.Backend.Center/src/entities/guild-node.entity.ts index e2260ee..a59f5ee 100644 --- a/Fabric.Backend.Center/src/entities/guild-node.entity.ts +++ b/Fabric.Backend.Center/src/entities/guild-node.entity.ts @@ -14,8 +14,12 @@ export class GuildNode { @Column() endpoint!: string; - @Column({ default: 'active' }) - status!: string; + @Column({ + type: 'enum', + enum: ['active', 'offline', 'revoked'], + default: 'active', + }) + status!: 'active' | 'offline' | 'revoked'; @CreateDateColumn() createdAt!: Date; diff --git a/Fabric.Backend.Center/src/nodes/dto.update-node-status.dto.ts b/Fabric.Backend.Center/src/nodes/dto.update-node-status.dto.ts new file mode 100644 index 0000000..869a139 --- /dev/null +++ b/Fabric.Backend.Center/src/nodes/dto.update-node-status.dto.ts @@ -0,0 +1,6 @@ +import { IsIn } from 'class-validator'; + +export class UpdateNodeStatusDto { + @IsIn(['active', 'offline', 'revoked']) + status!: 'active' | 'offline' | 'revoked'; +} diff --git a/Fabric.Backend.Center/src/nodes/nodes.controller.ts b/Fabric.Backend.Center/src/nodes/nodes.controller.ts index 89523e6..e9270f9 100644 --- a/Fabric.Backend.Center/src/nodes/nodes.controller.ts +++ b/Fabric.Backend.Center/src/nodes/nodes.controller.ts @@ -5,7 +5,10 @@ import { DefaultValuePipe, ForbiddenException, Get, + NotFoundException, + Param, ParseIntPipe, + Patch, Post, Query, } from '@nestjs/common'; @@ -13,6 +16,7 @@ import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { GuildNode } from '../entities/guild-node.entity'; import { RegisterNodeDto } from './dto.register-node.dto'; +import { UpdateNodeStatusDto } from './dto.update-node-status.dto'; @Controller('nodes') export class NodesController { @@ -61,6 +65,27 @@ export class NodesController { }; } + @Patch(':nodeId/status') + async updateStatus( + @Param('nodeId') nodeId: string, + @Body() body: UpdateNodeStatusDto, + ) { + const node = await this.nodeRepo.findOne({ where: { nodeId } }); + if (!node) { + throw new NotFoundException('node not found'); + } + + node.status = body.status; + const saved = await this.nodeRepo.save(node); + return { + id: saved.id, + nodeId: saved.nodeId, + name: saved.name, + endpoint: saved.endpoint, + status: saved.status, + }; + } + @Get() async list( @Query('page', new DefaultValuePipe(1), ParseIntPipe) page: number, diff --git a/docs/TODO-backend-center-guild.md b/docs/TODO-backend-center-guild.md index 992740e..972deae 100644 --- a/docs/TODO-backend-center-guild.md +++ b/docs/TODO-backend-center-guild.md @@ -25,7 +25,7 @@ ### 1.2 Guild Node 注册与握手 - [x] `POST /nodes/register` shared-secret 校验 - [x] node 唯一性校验(nodeId/endpoint) -- [ ] node 状态模型(active/offline/revoked) +- [x] node 状态模型(active/offline/revoked) - [x] `GET /nodes` 列表 + 分页 - [ ] node 心跳接口(可选)