Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | import { Controller, Get, Post, Put, Param, Query, Body, UseGuards, HttpStatus, HttpException, } from '@nestjs/common'; import { ApiTags, ApiOperation, ApiResponse, ApiQuery, ApiBearerAuth } from '@nestjs/swagger'; import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'; import { SavingsService, SavingsTransaction } from './savings.service'; import { SavingsAnalyticsService } from './services/savings-analytics.service'; import { GamificationService } from './services/gamification.service'; @ApiTags('savings') @Controller('savings') export class SavingsController { constructor( private savingsService: SavingsService, private analyticsService: SavingsAnalyticsService, private gamificationService: GamificationService, ) {} @Post('record') @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Record a savings transaction' }) async recordSavings(@Body() transaction: SavingsTransaction) { const userSavings = await this.savingsService.recordSavings(transaction); // Check for new achievements const newAchievements = await this.gamificationService.checkAndAwardAchievements(transaction.userId); return { savings: userSavings, newAchievements, message: 'Savings recorded successfully', }; } @Get('summary/:userId') @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Get user savings summary' }) async getSavingsSummary(@Param('userId') userId: string) { return this.savingsService.getSavingsSummary(userId); } @Get('history/:userId') @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Get user savings history' }) @ApiQuery({ name: 'page', required: false, type: Number }) @ApiQuery({ name: 'limit', required: false, type: Number }) async getSavingsHistory( @Param('userId') userId: string, @Query('page') page?: number, @Query('limit') limit?: number, ) { return this.savingsService.getSavingsHistory(userId, page || 1, limit || 20); } @Get('leaderboard') @ApiOperation({ summary: 'Get savings leaderboard' }) @ApiQuery({ name: 'period', required: false, enum: ['weekly', 'monthly', 'yearly'] }) @ApiQuery({ name: 'limit', required: false, type: Number }) async getSavingsLeaderboard( @Query('period') period?: 'weekly' | 'monthly' | 'yearly', @Query('limit') limit?: number, ) { return this.savingsService.getSavingsLeaderboard(period || 'monthly', limit || 10); } @Get('potential/:userId/:productId') @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Calculate potential savings for a product' }) async getPotentialSavings( @Param('userId') userId: string, @Param('productId') productId: string, ) { return this.savingsService.calculatePotentialSavings(userId, productId); } @Get('rank/:userId') @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Get user savings rank' }) async getUserSavingsRank(@Param('userId') userId: string) { return this.savingsService.getUserSavingsRank(userId); } @Get('insights/:userId') @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Get personalized savings insights' }) async getSavingsInsights(@Param('userId') userId: string) { return this.savingsService.getSavingsInsights(userId); } // Analytics endpoints @Get('analytics/global') @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Get global savings analytics' }) @ApiQuery({ name: 'startDate', required: false, type: String }) @ApiQuery({ name: 'endDate', required: false, type: String }) async getGlobalAnalytics( @Query('startDate') startDate?: string, @Query('endDate') endDate?: string, ) { const dateRange = startDate && endDate ? { start: new Date(startDate), end: new Date(endDate), } : undefined; return this.analyticsService.getGlobalSavingsAnalytics(dateRange); } @Get('analytics/user/:userId') @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Get user-specific savings analytics' }) async getUserAnalytics(@Param('userId') userId: string) { return this.analyticsService.getUserSavingsAnalytics(userId); } // Gamification endpoints @Get('gamification/progress/:userId') @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Get user gamification progress' }) async getUserProgress(@Param('userId') userId: string) { return this.gamificationService.getUserProgress(userId); } @Post('gamification/achievements/:userId/check') @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Check and award new achievements' }) async checkAchievements(@Param('userId') userId: string) { const newAchievements = await this.gamificationService.checkAndAwardAchievements(userId); return { newAchievements, count: newAchievements.length, }; } @Get('gamification/leaderboard') @ApiOperation({ summary: 'Get gamification leaderboard' }) @ApiQuery({ name: 'type', required: false, enum: ['total_savings', 'monthly_savings', 'points', 'streak'] }) @ApiQuery({ name: 'limit', required: false, type: Number }) async getGamificationLeaderboard( @Query('type') type?: 'total_savings' | 'monthly_savings' | 'points' | 'streak', @Query('limit') limit?: number, ) { return this.gamificationService.getLeaderboard(type || 'total_savings', limit || 10); } @Post('gamification/points/:userId') @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Award points to user' }) async awardPoints( @Param('userId') userId: string, @Body() data: { points: number; reason: string }, ) { await this.gamificationService.awardPoints(userId, data.points, data.reason); return { message: 'Points awarded successfully' }; } @Get('gamification/challenges/daily/:userId') @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Get daily challenges for user' }) async getDailyChallenges(@Param('userId') userId: string) { return this.gamificationService.getDailyChallenges(userId); } @Post('gamification/challenges/:challengeId/join') @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Join a challenge' }) async joinChallenge( @Param('challengeId') challengeId: string, @Body() data: { userId: string }, ) { const success = await this.gamificationService.joinChallenge(data.userId, challengeId); Iif (!success) { throw new HttpException('Failed to join challenge', HttpStatus.BAD_REQUEST); } return { message: 'Successfully joined challenge' }; } // Admin endpoints @Post('admin/reset-monthly') @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Reset monthly savings (Admin only)' }) async resetMonthlySavings() { await this.savingsService.resetMonthlySavings(); return { message: 'Monthly savings reset successfully' }; } @Post('admin/reset-yearly') @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Reset yearly savings (Admin only)' }) async resetYearlySavings() { await this.savingsService.resetYearlySavings(); return { message: 'Yearly savings reset successfully' }; } // Goal management endpoints @Post('goals/:userId') @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Create a savings goal' }) async createSavingsGoal( @Param('userId') userId: string, @Body() goalData: { targetAmount: number; period: string; deadline: Date; description?: string; }, ) { // This would be implemented in GoalsService return { message: 'Savings goal created successfully', goal: { id: `goal_${Date.now()}`, ...goalData, progress: 0, achieved: false, createdAt: new Date(), }, }; } @Get('goals/:userId') @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Get user savings goals' }) async getUserGoals(@Param('userId') userId: string) { const summary = await this.savingsService.getSavingsSummary(userId); return { goals: summary.goals, totalGoals: summary.goals.length, completedGoals: summary.goals.filter(g => g.achieved).length, }; } @Put('goals/:userId/:goalId') @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Update a savings goal' }) async updateSavingsGoal( @Param('userId') userId: string, @Param('goalId') goalId: string, @Body() updateData: { targetAmount?: number; deadline?: Date; description?: string; }, ) { // This would be implemented in GoalsService return { message: 'Savings goal updated successfully', goalId, updateData, }; } // Sustainability endpoints @Get('sustainability/:userId') @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Get user sustainability metrics' }) async getSustainabilityMetrics(@Param('userId') userId: string) { // This would be implemented in SustainabilityService return { userId, carbonFootprintSaved: 12.5, // kg CO2 sustainablePurchases: 8, ecoFriendlyDeals: 15, sustainabilityScore: 75, recommendations: [ 'Choose products with eco-friendly packaging', 'Look for locally made items to reduce shipping emissions', 'Consider second-hand or refurbished electronics', ], }; } @Get('sustainability/impact/global') @ApiOperation({ summary: 'Get global sustainability impact' }) async getGlobalSustainabilityImpact() { // This would be implemented in SustainabilityService return { totalCarbonSaved: 1250.5, // kg CO2 totalSustainablePurchases: 2847, topEcoFriendlyCategories: [ { category: 'Fashion', impact: 45.2 }, { category: 'Electronics', impact: 32.8 }, { category: 'Home & Garden', impact: 22.0 }, ], monthlyTrend: [ { month: 'Jan', carbonSaved: 95.2 }, { month: 'Feb', carbonSaved: 108.7 }, { month: 'Mar', carbonSaved: 125.3 }, ], }; } } |