69 lines
2.8 KiB
TypeScript
69 lines
2.8 KiB
TypeScript
import { Body, Controller, ForbiddenException, Get, Param, Patch, Query, UseGuards } from '@nestjs/common';
|
|
import { ApiBearerAuth, ApiBody, ApiOperation, ApiTags } from '@nestjs/swagger';
|
|
import { firstValueFrom } from 'rxjs';
|
|
import { CoreGrpcService } from '../core-grpc.service';
|
|
import { CurrentAdmin } from '../decorators/current-admin.decorator';
|
|
import { ListAdminBotsQueryDto, SetBotActiveDto } from '../dto/bot.dto';
|
|
import { AdminGuard, AdminRequestUser } from '../guards/admin.guard';
|
|
|
|
function assertManageAllBots(admin: AdminRequestUser) {
|
|
if (admin.isSuperAdmin || admin.permissions.includes('bots.manage.all')) {
|
|
return;
|
|
}
|
|
throw new ForbiddenException('Недостаточно прав для управления всеми ботами');
|
|
}
|
|
|
|
@ApiTags('Администрирование ботов')
|
|
@ApiBearerAuth()
|
|
@UseGuards(AdminGuard)
|
|
@Controller('admin/bots')
|
|
export class AdminBotController {
|
|
constructor(private readonly core: CoreGrpcService) {}
|
|
|
|
@Get()
|
|
@ApiOperation({ summary: 'Список всех ботов', description: 'Административный список Telegram-ботов с поиском и пагинацией.' })
|
|
listAllBots(@Query() query: ListAdminBotsQueryDto, @CurrentAdmin() admin: AdminRequestUser) {
|
|
assertManageAllBots(admin);
|
|
return firstValueFrom(
|
|
this.core.bot.ListAllBots({
|
|
requesterId: admin.id,
|
|
isSuperAdmin: admin.isSuperAdmin,
|
|
search: query.search,
|
|
page: query.page,
|
|
limit: query.limit
|
|
})
|
|
);
|
|
}
|
|
|
|
@Get('metrics')
|
|
@ApiOperation({ summary: 'Метрики ботов', description: 'Сводная статистика по ботам системы.' })
|
|
getMetrics(@CurrentAdmin() admin: AdminRequestUser) {
|
|
assertManageAllBots(admin);
|
|
return firstValueFrom(this.core.bot.GetBotMetrics({ requesterId: admin.id, isSuperAdmin: admin.isSuperAdmin }));
|
|
}
|
|
|
|
@Get(':botId')
|
|
@ApiOperation({ summary: 'Получить бота (админ)', description: 'Возвращает любого бота по ID.' })
|
|
getBot(@Param('botId') botId: string, @CurrentAdmin() admin: AdminRequestUser) {
|
|
assertManageAllBots(admin);
|
|
return firstValueFrom(
|
|
this.core.bot.GetBot({ requesterId: admin.id, botId, isSuperAdmin: admin.isSuperAdmin })
|
|
);
|
|
}
|
|
|
|
@Patch(':botId/active')
|
|
@ApiOperation({ summary: 'Заблокировать или разблокировать бота', description: 'Анти-abuse: отключает Bot API токен.' })
|
|
@ApiBody({ type: SetBotActiveDto })
|
|
setActive(@Param('botId') botId: string, @Body() dto: SetBotActiveDto, @CurrentAdmin() admin: AdminRequestUser) {
|
|
assertManageAllBots(admin);
|
|
return firstValueFrom(
|
|
this.core.bot.SetBotActive({
|
|
requesterId: admin.id,
|
|
botId,
|
|
isActive: dto.isActive,
|
|
isSuperAdmin: admin.isSuperAdmin
|
|
})
|
|
);
|
|
}
|
|
}
|