All files / src/modules/retailers/processors product-sync.processor.ts

0% Statements 0/35
0% Branches 0/4
0% Functions 0/5
0% Lines 0/32

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                                                                                                                                                     
import { Processor, Process } from '@nestjs/bull';
import { Logger } from '@nestjs/common';
import { Job } from 'bull';
import { RetailerSyncService } from '../services/retailer-sync.service';
 
@Processor('product-sync')
export class ProductSyncProcessor {
  private readonly logger = new Logger(ProductSyncProcessor.name);
 
  constructor(private retailerSyncService: RetailerSyncService) {}
 
  @Process('sync-single-product')
  async handleSingleProductSync(job: Job<{
    retailerName: string;
    productId: string;
  }>) {
    const { retailerName, productId } = job.data;
    
    this.logger.log(`Processing single product sync: ${productId} from ${retailerName}`);
    
    try {
      // Implementation would depend on specific retailer service
      // This is a placeholder for the actual sync logic
      await new Promise(resolve => setTimeout(resolve, 1000)); // Simulate work
      
      this.logger.log(`Successfully synced product ${productId} from ${retailerName}`);
    } catch (error) {
      this.logger.error(`Failed to sync product ${productId} from ${retailerName}`, error);
      throw error;
    }
  }
 
  @Process('bulk-sync')
  async handleBulkSync(job: Job<{
    retailerName: string;
    category?: string;
    priority?: 'low' | 'normal' | 'high';
  }>) {
    const { retailerName, category } = job.data;
    
    this.logger.log(`Processing bulk sync for ${retailerName}${category ? ` in category ${category}` : ''}`);
    
    try {
      const result = await this.retailerSyncService.syncRetailer(retailerName);
      
      this.logger.log(`Bulk sync completed for ${retailerName}: ${result.productsProcessed} products processed`);
      
      return result;
    } catch (error) {
      this.logger.error(`Bulk sync failed for ${retailerName}`, error);
      throw error;
    }
  }
 
  @Process('health-check')
  async handleHealthCheck(job: Job<{
    retailerName: string;
  }>) {
    const { retailerName } = job.data;
    
    this.logger.log(`Processing health check for ${retailerName}`);
    
    try {
      const status = await this.retailerSyncService.checkRetailerHealth(retailerName);
      
      this.logger.log(`Health check completed for ${retailerName}: ${status.healthy ? 'healthy' : 'unhealthy'}`);
      
      return status;
    } catch (error) {
      this.logger.error(`Health check failed for ${retailerName}`, error);
      throw error;
    }
  }
}