|
| 1 | +import { Controller, Get, Query, Param, UseGuards, Req } from '@nestjs/common'; |
| 2 | +import { ApiTags, ApiOperation, ApiBearerAuth, ApiResponse } from '@nestjs/swagger'; |
| 3 | +import { AnalyticsService } from './analytics.service'; |
| 4 | +import { AnalyticsQueryDto, DashboardStatsResponse, TrendsResponse, DistributionResponse } from './dto/analytics-query.dto'; |
| 5 | +import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'; |
| 6 | +import { RbacGuard } from '../auth/guards/rbac.guard'; |
| 7 | +import { CustomThrottlerGuard } from '../security/throttler.guard'; |
| 8 | +import { RequirePermission } from '../common/decorators/require-permission.decorator'; |
| 9 | + |
| 10 | +@ApiTags('Analytics') |
| 11 | +@ApiBearerAuth('JWT-auth') |
| 12 | +@UseGuards(JwtAuthGuard, RbacGuard, CustomThrottlerGuard) |
| 13 | +@Controller('api/v1/analytics') |
| 14 | +export class AnalyticsController { |
| 15 | + constructor(private readonly analyticsService: AnalyticsService) {} |
| 16 | + |
| 17 | + @Get('dashboard') |
| 18 | + @ApiOperation({ summary: 'Get main dashboard statistics' }) |
| 19 | + @RequirePermission('analytics', 'read') |
| 20 | + async getDashboardStats(@Query() query: AnalyticsQueryDto, @Req() req: any): Promise<DashboardStatsResponse> { |
| 21 | + return this.analyticsService.getDashboardStats(query, req.user); |
| 22 | + } |
| 23 | + |
| 24 | + @Get('asset-stats') |
| 25 | + @ApiOperation({ summary: 'Get asset-specific statistics' }) |
| 26 | + @RequirePermission('analytics', 'read') |
| 27 | + async getAssetStats() { |
| 28 | + return this.analyticsService.getAssetStats(); |
| 29 | + } |
| 30 | + |
| 31 | + @Get('trends') |
| 32 | + @ApiOperation({ summary: 'Get trend data for charts' }) |
| 33 | + @RequirePermission('analytics', 'read') |
| 34 | + async getTrends(@Query() query: AnalyticsQueryDto, @Req() req: any): Promise<TrendsResponse> { |
| 35 | + return this.analyticsService.getTrends(query, req.user); |
| 36 | + } |
| 37 | + |
| 38 | + @Get('distribution') |
| 39 | + @ApiOperation({ summary: 'Get asset distribution data' }) |
| 40 | + @RequirePermission('analytics', 'read') |
| 41 | + async getDistribution(@Query() query: AnalyticsQueryDto, @Req() req: any): Promise<DistributionResponse> { |
| 42 | + return this.analyticsService.getDistribution(query, req.user); |
| 43 | + } |
| 44 | + |
| 45 | + @Get('top-assets') |
| 46 | + @ApiOperation({ summary: 'Get most expensive/valuable assets' }) |
| 47 | + @RequirePermission('analytics', 'read') |
| 48 | + async getTopAssets() { |
| 49 | + return this.analyticsService.getTopAssets(); |
| 50 | + } |
| 51 | + |
| 52 | + @Get('alerts') |
| 53 | + @ApiOperation({ summary: 'Get assets requiring attention' }) |
| 54 | + @RequirePermission('analytics', 'read') |
| 55 | + async getAlerts() { |
| 56 | + return this.analyticsService.getAlerts(); |
| 57 | + } |
| 58 | + |
| 59 | + @Get('departments/comparison') |
| 60 | + @ApiOperation({ summary: 'Compare departments' }) |
| 61 | + @RequirePermission('analytics', 'read') |
| 62 | + async compareDepartments() { |
| 63 | + // Basic implementation using existing logic |
| 64 | + return this.analyticsService.getDistribution({}, { role: 'ADMIN' }); |
| 65 | + } |
| 66 | + |
| 67 | + @Get('departments/:id') |
| 68 | + @ApiOperation({ summary: 'Get department-specific analytics' }) |
| 69 | + @RequirePermission('analytics', 'read') |
| 70 | + async getDepartmentAnalytics(@Param('id') id: string) { |
| 71 | + return this.analyticsService.getDepartmentAnalytics(id); |
| 72 | + } |
| 73 | + |
| 74 | + @Get('locations/utilization') |
| 75 | + @ApiOperation({ summary: 'Get location utilization rates' }) |
| 76 | + @RequirePermission('analytics', 'read') |
| 77 | + async getLocationUtilization() { |
| 78 | + return this.analyticsService.getDistribution({}, { role: 'ADMIN' }); |
| 79 | + } |
| 80 | + |
| 81 | + @Get('locations/:id') |
| 82 | + @ApiOperation({ summary: 'Get location-specific analytics' }) |
| 83 | + @RequirePermission('analytics', 'read') |
| 84 | + async getLocationAnalytics(@Param('id') id: string) { |
| 85 | + return this.analyticsService.getLocationAnalytics(id); |
| 86 | + } |
| 87 | + |
| 88 | + @Get('users/activity') |
| 89 | + @ApiOperation({ summary: 'Get user activity statistics' }) |
| 90 | + @RequirePermission('analytics', 'read') |
| 91 | + async getUserActivity() { |
| 92 | + // Placeholder |
| 93 | + return { activity: [] }; |
| 94 | + } |
| 95 | + |
| 96 | + @Get('users/:id') |
| 97 | + @ApiOperation({ summary: "Get user's asset assignment history" }) |
| 98 | + @RequirePermission('analytics', 'read') |
| 99 | + async getUserAnalytics(@Param('id') id: string) { |
| 100 | + return this.analyticsService.getUserAnalytics(id); |
| 101 | + } |
| 102 | + |
| 103 | + @Get('timeline') |
| 104 | + @ApiOperation({ summary: 'Asset registrations over time' }) |
| 105 | + @RequirePermission('analytics', 'read') |
| 106 | + async getTimeline(@Query() query: AnalyticsQueryDto, @Req() req: any) { |
| 107 | + return this.analyticsService.getTrends(query, req.user); |
| 108 | + } |
| 109 | + |
| 110 | + @Get('forecast') |
| 111 | + @ApiOperation({ summary: 'Predictive analytics (asset needs)' }) |
| 112 | + @RequirePermission('analytics', 'read') |
| 113 | + async getForecast() { |
| 114 | + // Mock forecast implementation |
| 115 | + return { |
| 116 | + forecast: [ |
| 117 | + { month: '2025-02', predictedNeed: 15, confidence: 0.85 }, |
| 118 | + { month: '2025-03', predictedNeed: 22, confidence: 0.78 }, |
| 119 | + ] |
| 120 | + }; |
| 121 | + } |
| 122 | +} |
0 commit comments