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 | import { Processor, Process } from '@nestjs/bull'; import { Logger } from '@nestjs/common'; import { Job } from 'bull'; import { CouponValidationService } from '../services/coupon-validation.service'; import { CouponScrapingService } from '../services/coupon-scraping.service'; import { DealDetectionService } from '../services/deal-detection.service'; @Processor('coupon-processing') export class CouponProcessor { private readonly logger = new Logger(CouponProcessor.name); constructor( private couponValidationService: CouponValidationService, private couponScrapingService: CouponScrapingService, private dealDetectionService: DealDetectionService, ) {} @Process('validate-coupon') async handleCouponValidation(job: Job<{ couponId: string; }>) { const { couponId } = job.data; this.logger.log(`Processing coupon validation: ${couponId}`); try { // This would validate the coupon with the retailer // For now, we'll just log the validation this.logger.log(`Coupon ${couponId} validation completed`); } catch (error) { this.logger.error(`Failed to validate coupon ${couponId}`, error); throw error; } } @Process('scrape-coupons') async handleCouponScraping(job: Job<{ retailerName?: string; sourceUrl?: string; }>) { const { retailerName, sourceUrl } = job.data; this.logger.log(`Processing coupon scraping for ${retailerName || 'all retailers'}`); try { let result; if (retailerName) { result = await this.couponScrapingService.scrapeCouponsForRetailer(retailerName); } else { result = await this.couponScrapingService.scrapeAllSources(); } this.logger.log(`Coupon scraping completed: ${result.newCoupons} new, ${result.updatedCoupons} updated`); return result; } catch (error) { this.logger.error(`Failed to scrape coupons`, error); throw error; } } @Process('detect-deals') async handleDealDetection(job: Job<{ category?: string; userId?: string; }>) { const { category, userId } = job.data; this.logger.log(`Processing deal detection${category ? ` for category ${category}` : ''}${userId ? ` for user ${userId}` : ''}`); try { let deals; if (userId) { deals = await this.dealDetectionService.getPersonalizedDeals(userId); } else if (category) { deals = await this.dealDetectionService.getDealsByCategory(category); } else { const result = await this.dealDetectionService.detectAllDeals(); deals = result.topDeals; } this.logger.log(`Deal detection completed: ${deals.length} deals found`); return deals; } catch (error) { this.logger.error(`Failed to detect deals`, error); throw error; } } @Process('send-coupon-alerts') async handleCouponAlerts(job: Job<{ userId: string; alertType: 'price_drop' | 'new_coupon' | 'expiring_soon'; couponId?: string; productId?: string; }>) { const { userId, alertType, couponId, productId } = job.data; this.logger.log(`Processing coupon alert for user ${userId}: ${alertType}`); try { // This would send notifications to the user // Implementation would depend on notification service this.logger.log(`Coupon alert sent to user ${userId}`); } catch (error) { this.logger.error(`Failed to send coupon alert to user ${userId}`, error); throw error; } } @Process('cleanup-expired-coupons') async handleExpiredCouponsCleanup(job: Job) { this.logger.log('Processing expired coupons cleanup'); try { // This would update expired coupons and clean up old data this.logger.log('Expired coupons cleanup completed'); } catch (error) { this.logger.error('Failed to cleanup expired coupons', error); throw error; } } @Process('update-coupon-performance') async handleCouponPerformanceUpdate(job: Job<{ couponId: string; usageData: { userId: string; productId: string; savingsAmount: number; orderValue: number; }; }>) { const { couponId, usageData } = job.data; this.logger.log(`Processing coupon performance update: ${couponId}`); try { await this.couponValidationService.recordCouponUsage( couponId, usageData.userId, usageData.productId, usageData.savingsAmount, ); this.logger.log(`Coupon performance updated for ${couponId}`); } catch (error) { this.logger.error(`Failed to update coupon performance for ${couponId}`, error); throw error; } } } |