feat(quota): wire knowledge_base_count quota into KB controller
All checks were successful
Deploy API Server / build-and-unit (push) Successful in 39s
Deploy API Server / current-integration (push) Successful in 32s
Deploy API Server / backward-compat (push) Successful in 0s
Deploy API Server / deploy (push) Successful in 1m4s

This commit is contained in:
wangdl 2026-06-26 20:28:29 +08:00
parent 807488a929
commit fef0695845
2 changed files with 17 additions and 3 deletions

View File

@ -1,6 +1,7 @@
import { Controller, Get, Post, Patch, Delete, Body, Param, Query } from '@nestjs/common'; import { Controller, Get, Post, Patch, Delete, Body, Param, Query, BadRequestException } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger'; import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
import { KnowledgeBaseService } from './knowledge-base.service'; import { KnowledgeBaseService } from './knowledge-base.service';
import { QuotaGuardService } from '../membership/quota-guard.service';
import { CurrentUser } from '../../common/decorators/current-user.decorator'; import { CurrentUser } from '../../common/decorators/current-user.decorator';
import { PaginationDto } from '../../common/dto/pagination.dto'; import { PaginationDto } from '../../common/dto/pagination.dto';
import type { UserPayload } from '../../common/types'; import type { UserPayload } from '../../common/types';
@ -8,12 +9,23 @@ import type { UserPayload } from '../../common/types';
@ApiTags('knowledge-base') @ApiTags('knowledge-base')
@Controller('knowledge-bases') @Controller('knowledge-bases')
export class KnowledgeBaseController { export class KnowledgeBaseController {
constructor(private readonly service: KnowledgeBaseService) {} constructor(
private readonly service: KnowledgeBaseService,
private readonly quotaGuard: QuotaGuardService,
) {}
@Post() @Post()
@ApiOperation({ summary: '创建知识库' }) @ApiOperation({ summary: '创建知识库' })
async create(@CurrentUser() user: UserPayload, @Body() dto: any) { async create(@CurrentUser() user: UserPayload, @Body() dto: any) {
return this.service.create(String(user?.id || 'anonymous'), dto); const userId = String(user?.id || 'anonymous');
const opId = `kb_create:${userId}:${Date.now()}`;
const check = await this.quotaGuard.check(userId, 'knowledge_base_count', opId);
if (!check.allowed) throw new BadRequestException(this.quotaGuard.buildError(check));
try {
const result = await this.service.create(userId, dto);
this.quotaGuard.confirm(opId).catch(() => {});
return result;
} catch { this.quotaGuard.cancel(opId).catch(() => {}); throw new BadRequestException('Knowledge base creation failed'); }
} }
@Get() @Get()

View File

@ -4,8 +4,10 @@ import { KnowledgeBaseService } from './knowledge-base.service';
import { KnowledgeBaseRepository } from './knowledge-base.repository'; import { KnowledgeBaseRepository } from './knowledge-base.repository';
import { SystemKnowledgeBaseSeed } from './system-kb.seed'; import { SystemKnowledgeBaseSeed } from './system-kb.seed';
import { PrismaService } from '../../infrastructure/database/prisma.service'; import { PrismaService } from '../../infrastructure/database/prisma.service';
import { MembershipModule } from '../membership/membership.module';
@Module({ @Module({
imports: [MembershipModule],
controllers: [KnowledgeBaseController], controllers: [KnowledgeBaseController],
providers: [KnowledgeBaseService, KnowledgeBaseRepository, SystemKnowledgeBaseSeed, PrismaService], providers: [KnowledgeBaseService, KnowledgeBaseRepository, SystemKnowledgeBaseSeed, PrismaService],
exports: [KnowledgeBaseService], exports: [KnowledgeBaseService],