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 | import { Controller, Get, Post, Put, Delete, 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 { CouponsService } from './coupons.service'; import { CouponValidationService } from './services/coupon-validation.service'; import { CouponScrapingService } from './services/coupon-scraping.service'; import { DealDetectionService } from './services/deal-detection.service'; import { CouponAnalyticsService } from './services/coupon-analytics.service'; @ApiTags('coupons') @Controller('coupons') export class CouponsController { constructor( private couponsService: CouponsService, private validationService: CouponValidationService, private scrapingService: CouponScrapingService, private dealDetectionService: DealDetectionService, private analyticsService: CouponAnalyticsService, ) {} @Get() @ApiOperation({ summary: 'Get all active coupons' }) @ApiQuery({ name: 'retailer', required: false, type: String }) @ApiQuery({ name: 'category', required: false, type: String }) @ApiQuery({ name: 'page', required: false, type: Number }) @ApiQuery({ name: 'limit', required: false, type: Number }) async getCoupons( @Query('retailer') retailer?: string, @Query('category') category?: string, @Query('page') page?: number, @Query('limit') limit?: number, ) { return this.couponsService.findAll({ retailer, category, page: page || 1, limit: limit || 50, }); } @Get(':id') @ApiOperation({ summary: 'Get coupon by ID' }) async getCoupon(@Param('id') id: string) { const coupon = await this.couponsService.findOne(id); Iif (!coupon) { throw new HttpException('Coupon not found', HttpStatus.NOT_FOUND); } return coupon; } @Post('validate') @ApiOperation({ summary: 'Validate a coupon code' }) async validateCoupon(@Body() validateData: { code: string; retailerId: string; productId?: string; userId?: string; purchaseAmount?: number; }) { return this.validationService.validateCoupon( validateData.code, validateData.retailerId, validateData.productId, validateData.userId, validateData.purchaseAmount, ); } @Post('apply') @ApiOperation({ summary: 'Apply coupon to a product' }) async applyCoupon(@Body() applyData: { code: string; productId: string; variantSku: string; userId?: string; }) { return this.validationService.applyCouponToProduct( applyData.code, applyData.productId, applyData.variantSku, applyData.userId, ); } @Get('product/:productId/best') @ApiOperation({ summary: 'Get best coupons for a product' }) async getBestCouponsForProduct( @Param('productId') productId: string, @Query('variantSku') variantSku: string, @Query('userId') userId?: string, ) { Iif (!variantSku) { throw new HttpException('Variant SKU is required', HttpStatus.BAD_REQUEST); } return this.validationService.findBestCouponsForProduct( productId, variantSku, userId, ); } @Get('deals/all') @ApiOperation({ summary: 'Get all detected deals' }) @ApiQuery({ name: 'category', required: false, type: String }) @ApiQuery({ name: 'limit', required: false, type: Number }) async getAllDeals( @Query('category') category?: string, @Query('limit') limit?: number, ) { Iif (category) { return this.dealDetectionService.getDealsByCategory(category, limit || 20); } const result = await this.dealDetectionService.detectAllDeals(); return { deals: result.topDeals.slice(0, limit || 20), totalDeals: result.totalDeals, categories: result.categories, }; } @Get('deals/personalized/:userId') @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Get personalized deals for a user' }) @ApiQuery({ name: 'limit', required: false, type: Number }) async getPersonalizedDeals( @Param('userId') userId: string, @Query('limit') limit?: number, ) { return this.dealDetectionService.getPersonalizedDeals(userId, limit || 20); } @Post('scrape') @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Trigger coupon scraping' }) async triggerScraping(@Body() scrapeData: { retailer?: string; }) { Iif (scrapeData.retailer) { return this.scrapingService.scrapeCouponsForRetailer(scrapeData.retailer); } return this.scrapingService.scrapeAllSources(); } @Get('analytics/overview') @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Get coupon analytics overview' }) @ApiQuery({ name: 'startDate', required: false, type: String }) @ApiQuery({ name: 'endDate', required: false, type: String }) async getAnalyticsOverview( @Query('startDate') startDate?: string, @Query('endDate') endDate?: string, ) { const dateRange = startDate && endDate ? { start: new Date(startDate), end: new Date(endDate), } : undefined; return this.analyticsService.getCouponAnalytics(dateRange); } @Get('analytics/retailer/:retailerId') @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Get retailer-specific coupon analytics' }) async getRetailerAnalytics(@Param('retailerId') retailerId: string) { return this.analyticsService.getRetailerCouponReport(retailerId); } @Get('analytics/roi/:couponId') @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Get ROI analysis for a specific coupon' }) async getCouponROI(@Param('couponId') couponId: string) { return this.analyticsService.getCouponROIAnalysis(couponId); } @Get('scraping/stats') @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Get coupon scraping statistics' }) async getScrapingStats() { return this.scrapingService.getScrapingStats(); } @Post('bulk-validate') @ApiOperation({ summary: 'Validate multiple coupon codes' }) async bulkValidateCoupons(@Body() validateData: { codes: string[]; retailerId: string; productIds?: string[]; userId?: string; }) { return this.validationService.validateBulkCoupons( validateData.codes, validateData.retailerId, validateData.productIds, validateData.userId, ); } @Post(':id/usage') @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Record coupon usage' }) async recordUsage( @Param('id') couponId: string, @Body() usageData: { userId: string; productId: string; savingsAmount: number; }, ) { await this.validationService.recordCouponUsage( couponId, usageData.userId, usageData.productId, usageData.savingsAmount, ); return { message: 'Usage recorded successfully' }; } @Get(':id/performance') @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Get coupon performance metrics' }) async getCouponPerformance(@Param('id') couponId: string) { return this.validationService.getCouponPerformanceMetrics(couponId); } @Post() @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Create a new coupon' }) async createCoupon(@Body() createCouponData: any) { return this.couponsService.create(createCouponData); } @Put(':id') @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Update a coupon' }) async updateCoupon(@Param('id') id: string, @Body() updateData: any) { return this.couponsService.update(id, updateData); } @Delete(':id') @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Delete a coupon' }) async deleteCoupon(@Param('id') id: string) { const result = await this.couponsService.delete(id); Iif (!result) { throw new HttpException('Coupon not found', HttpStatus.NOT_FOUND); } return { message: 'Coupon deleted successfully' }; } } |