feat(quota): wire knowledge_base_count quota into KB controller
This commit is contained in:
parent
807488a929
commit
fef0695845
@ -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 { KnowledgeBaseService } from './knowledge-base.service';
|
||||
import { QuotaGuardService } from '../membership/quota-guard.service';
|
||||
import { CurrentUser } from '../../common/decorators/current-user.decorator';
|
||||
import { PaginationDto } from '../../common/dto/pagination.dto';
|
||||
import type { UserPayload } from '../../common/types';
|
||||
@ -8,12 +9,23 @@ import type { UserPayload } from '../../common/types';
|
||||
@ApiTags('knowledge-base')
|
||||
@Controller('knowledge-bases')
|
||||
export class KnowledgeBaseController {
|
||||
constructor(private readonly service: KnowledgeBaseService) {}
|
||||
constructor(
|
||||
private readonly service: KnowledgeBaseService,
|
||||
private readonly quotaGuard: QuotaGuardService,
|
||||
) {}
|
||||
|
||||
@Post()
|
||||
@ApiOperation({ summary: '创建知识库' })
|
||||
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()
|
||||
|
||||
@ -4,8 +4,10 @@ import { KnowledgeBaseService } from './knowledge-base.service';
|
||||
import { KnowledgeBaseRepository } from './knowledge-base.repository';
|
||||
import { SystemKnowledgeBaseSeed } from './system-kb.seed';
|
||||
import { PrismaService } from '../../infrastructure/database/prisma.service';
|
||||
import { MembershipModule } from '../membership/membership.module';
|
||||
|
||||
@Module({
|
||||
imports: [MembershipModule],
|
||||
controllers: [KnowledgeBaseController],
|
||||
providers: [KnowledgeBaseService, KnowledgeBaseRepository, SystemKnowledgeBaseSeed, PrismaService],
|
||||
exports: [KnowledgeBaseService],
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user