cr
This commit is contained in:
@@ -10,32 +10,32 @@ import {
|
||||
UseInterceptors,
|
||||
Inject,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
} from "@nestjs/common";
|
||||
import {
|
||||
CacheInterceptor,
|
||||
CacheKey,
|
||||
CacheTTL,
|
||||
CACHE_MANAGER,
|
||||
} from '@nestjs/cache-manager';
|
||||
import * as cacheManager from 'cache-manager';
|
||||
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
|
||||
import { Roles } from '../../common/decorators';
|
||||
import { PrismaService } from '../../database/prisma.service';
|
||||
import { PaginationDto } from '../../common/dto/pagination.dto';
|
||||
} from "@nestjs/cache-manager";
|
||||
import * as cacheManager from "cache-manager";
|
||||
import { ApiTags, ApiBearerAuth, ApiOperation } from "@nestjs/swagger";
|
||||
import { Roles } from "../../common/decorators";
|
||||
import { PrismaService } from "../../database/prisma.service";
|
||||
import { PaginationDto } from "../../common/dto/pagination.dto";
|
||||
import {
|
||||
ApiResponse,
|
||||
createSuccessResponse,
|
||||
createPaginatedResponse,
|
||||
PaginatedData,
|
||||
} from '../../common/types/api-response.type';
|
||||
import { plainToInstance } from 'class-transformer';
|
||||
import { UserResponseDto } from '../users/dto/user.dto';
|
||||
import { UserRole } from '@prisma/client';
|
||||
} from "../../common/types/api-response.type";
|
||||
import { plainToInstance } from "class-transformer";
|
||||
import { UserResponseDto } from "../users/dto/user.dto";
|
||||
import { UserRole } from "@prisma/client";
|
||||
|
||||
@ApiTags('Admin')
|
||||
@ApiTags("Admin")
|
||||
@ApiBearerAuth()
|
||||
@Controller('admin')
|
||||
@Roles('superadmin')
|
||||
@Controller("admin")
|
||||
@Roles("superadmin")
|
||||
export class AdminController {
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
@@ -44,8 +44,8 @@ export class AdminController {
|
||||
|
||||
// ================== Users Management ==================
|
||||
|
||||
@Get('users')
|
||||
@ApiOperation({ summary: 'Get all users (admin)' })
|
||||
@Get("users")
|
||||
@ApiOperation({ summary: "Get all users (admin)" })
|
||||
async getAllUsers(
|
||||
@Query() pagination: PaginationDto,
|
||||
): Promise<ApiResponse<PaginatedData<UserResponseDto>>> {
|
||||
@@ -73,10 +73,10 @@ export class AdminController {
|
||||
);
|
||||
}
|
||||
|
||||
@Get('users/:id')
|
||||
@ApiOperation({ summary: 'Get user by ID' })
|
||||
@Get("users/:id")
|
||||
@ApiOperation({ summary: "Get user by ID" })
|
||||
async getUserById(
|
||||
@Param('id') id: string,
|
||||
@Param("id") id: string,
|
||||
): Promise<ApiResponse<UserResponseDto>> {
|
||||
const user = await this.prisma.user.findUnique({
|
||||
where: { id },
|
||||
@@ -84,27 +84,27 @@ export class AdminController {
|
||||
usageLimit: true,
|
||||
analyses: {
|
||||
take: 5,
|
||||
orderBy: { createdAt: 'desc' },
|
||||
orderBy: { createdAt: "desc" },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new NotFoundException('User not found');
|
||||
throw new NotFoundException("User not found");
|
||||
}
|
||||
|
||||
return createSuccessResponse(plainToInstance(UserResponseDto, user));
|
||||
}
|
||||
|
||||
@Put('users/:id/toggle-active')
|
||||
@ApiOperation({ summary: 'Toggle user active status' })
|
||||
@Put("users/:id/toggle-active")
|
||||
@ApiOperation({ summary: "Toggle user active status" })
|
||||
async toggleUserActive(
|
||||
@Param('id') id: string,
|
||||
@Param("id") id: string,
|
||||
): Promise<ApiResponse<UserResponseDto>> {
|
||||
const user = await this.prisma.user.findUnique({ where: { id } });
|
||||
|
||||
if (!user) {
|
||||
throw new NotFoundException('User not found');
|
||||
throw new NotFoundException("User not found");
|
||||
}
|
||||
|
||||
const updated = await this.prisma.user.update({
|
||||
@@ -114,14 +114,14 @@ export class AdminController {
|
||||
|
||||
return createSuccessResponse(
|
||||
plainToInstance(UserResponseDto, updated),
|
||||
'User status updated',
|
||||
"User status updated",
|
||||
);
|
||||
}
|
||||
|
||||
@Put('users/:id/role')
|
||||
@ApiOperation({ summary: 'Update user role' })
|
||||
@Put("users/:id/role")
|
||||
@ApiOperation({ summary: "Update user role" })
|
||||
async updateUserRole(
|
||||
@Param('id') id: string,
|
||||
@Param("id") id: string,
|
||||
@Body() data: { role: UserRole },
|
||||
): Promise<ApiResponse<UserResponseDto>> {
|
||||
const user = await this.prisma.user.update({
|
||||
@@ -131,14 +131,14 @@ export class AdminController {
|
||||
|
||||
return createSuccessResponse(
|
||||
plainToInstance(UserResponseDto, user),
|
||||
'User role updated',
|
||||
"User role updated",
|
||||
);
|
||||
}
|
||||
|
||||
@Put('users/:id/subscription')
|
||||
@ApiOperation({ summary: 'Update user subscription' })
|
||||
@Put("users/:id/subscription")
|
||||
@ApiOperation({ summary: "Update user subscription" })
|
||||
async updateUserSubscription(
|
||||
@Param('id') id: string,
|
||||
@Param("id") id: string,
|
||||
@Body()
|
||||
data: { subscriptionStatus: string; subscriptionExpiresAt?: string },
|
||||
): Promise<ApiResponse<UserResponseDto>> {
|
||||
@@ -154,40 +154,40 @@ export class AdminController {
|
||||
|
||||
return createSuccessResponse(
|
||||
plainToInstance(UserResponseDto, user),
|
||||
'User subscription updated',
|
||||
"User subscription updated",
|
||||
);
|
||||
}
|
||||
|
||||
@Delete('users/:id')
|
||||
@ApiOperation({ summary: 'Soft delete a user' })
|
||||
async deleteUser(@Param('id') id: string): Promise<ApiResponse<null>> {
|
||||
@Delete("users/:id")
|
||||
@ApiOperation({ summary: "Soft delete a user" })
|
||||
async deleteUser(@Param("id") id: string): Promise<ApiResponse<null>> {
|
||||
await this.prisma.user.update({
|
||||
where: { id },
|
||||
data: { deletedAt: new Date() },
|
||||
});
|
||||
return createSuccessResponse(null, 'User deleted');
|
||||
return createSuccessResponse(null, "User deleted");
|
||||
}
|
||||
|
||||
// ================== App Settings ==================
|
||||
|
||||
@Get('settings')
|
||||
@Get("settings")
|
||||
@UseInterceptors(CacheInterceptor)
|
||||
@CacheKey('app_settings')
|
||||
@CacheKey("app_settings")
|
||||
@CacheTTL(60 * 1000)
|
||||
@ApiOperation({ summary: 'Get all app settings' })
|
||||
@ApiOperation({ summary: "Get all app settings" })
|
||||
async getAllSettings(): Promise<ApiResponse<Record<string, string>>> {
|
||||
const settings = await this.prisma.appSetting.findMany();
|
||||
const settingsMap: Record<string, string> = {};
|
||||
for (const s of settings) {
|
||||
settingsMap[s.key] = s.value || '';
|
||||
settingsMap[s.key] = s.value || "";
|
||||
}
|
||||
return createSuccessResponse(settingsMap);
|
||||
}
|
||||
|
||||
@Put('settings/:key')
|
||||
@ApiOperation({ summary: 'Update an app setting' })
|
||||
@Put("settings/:key")
|
||||
@ApiOperation({ summary: "Update an app setting" })
|
||||
async updateSetting(
|
||||
@Param('key') key: string,
|
||||
@Param("key") key: string,
|
||||
@Body() data: { value: string },
|
||||
): Promise<ApiResponse<{ key: string; value: string }>> {
|
||||
const setting = await this.prisma.appSetting.upsert({
|
||||
@@ -195,17 +195,17 @@ export class AdminController {
|
||||
update: { value: data.value },
|
||||
create: { key, value: data.value },
|
||||
});
|
||||
await this.cacheManager.del('app_settings');
|
||||
await this.cacheManager.del("app_settings");
|
||||
return createSuccessResponse(
|
||||
{ key: setting.key, value: setting.value || '' },
|
||||
'Setting updated',
|
||||
{ key: setting.key, value: setting.value || "" },
|
||||
"Setting updated",
|
||||
);
|
||||
}
|
||||
|
||||
// ================== Usage Limits ==================
|
||||
|
||||
@Get('usage-limits')
|
||||
@ApiOperation({ summary: 'Get all usage limits' })
|
||||
@Get("usage-limits")
|
||||
@ApiOperation({ summary: "Get all usage limits" })
|
||||
async getAllUsageLimits(@Query() pagination: PaginationDto) {
|
||||
const { skip, take } = pagination;
|
||||
|
||||
@@ -218,7 +218,7 @@ export class AdminController {
|
||||
select: { id: true, email: true, firstName: true, lastName: true },
|
||||
},
|
||||
},
|
||||
orderBy: { lastResetDate: 'desc' },
|
||||
orderBy: { lastResetDate: "desc" },
|
||||
}),
|
||||
this.prisma.usageLimit.count(),
|
||||
]);
|
||||
@@ -231,8 +231,8 @@ export class AdminController {
|
||||
);
|
||||
}
|
||||
|
||||
@Post('usage-limits/reset-all')
|
||||
@ApiOperation({ summary: 'Reset all usage limits' })
|
||||
@Post("usage-limits/reset-all")
|
||||
@ApiOperation({ summary: "Reset all usage limits" })
|
||||
async resetAllUsageLimits(): Promise<ApiResponse<{ count: number }>> {
|
||||
const result = await this.prisma.usageLimit.updateMany({
|
||||
data: {
|
||||
@@ -244,14 +244,14 @@ export class AdminController {
|
||||
|
||||
return createSuccessResponse(
|
||||
{ count: result.count },
|
||||
'All usage limits reset',
|
||||
"All usage limits reset",
|
||||
);
|
||||
}
|
||||
|
||||
// ================== Analytics ==================
|
||||
|
||||
@Get('analytics/overview')
|
||||
@ApiOperation({ summary: 'Get system analytics overview' })
|
||||
@Get("analytics/overview")
|
||||
@ApiOperation({ summary: "Get system analytics overview" })
|
||||
async getAnalyticsOverview() {
|
||||
const [
|
||||
totalUsers,
|
||||
@@ -262,7 +262,7 @@ export class AdminController {
|
||||
] = await Promise.all([
|
||||
this.prisma.user.count(),
|
||||
this.prisma.user.count({ where: { isActive: true } }),
|
||||
this.prisma.user.count({ where: { subscriptionStatus: 'active' } }),
|
||||
this.prisma.user.count({ where: { subscriptionStatus: "active" } }),
|
||||
this.prisma.match.count(),
|
||||
this.prisma.prediction.count(),
|
||||
]);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { AdminController } from './admin.controller';
|
||||
import { Module } from "@nestjs/common";
|
||||
import { AdminController } from "./admin.controller";
|
||||
|
||||
@Module({
|
||||
controllers: [AdminController],
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Exclude, Expose, Type } from 'class-transformer';
|
||||
import { Exclude, Expose, Type } from "class-transformer";
|
||||
|
||||
@Exclude()
|
||||
export class PermissionResponseDto {
|
||||
|
||||
Reference in New Issue
Block a user