All files / src/modules/savings/processors savings.processor.ts

0% Statements 0/51
0% Branches 0/1
0% Functions 0/6
0% Lines 0/49

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                                                                                                                                                                                                 
import { Processor, Process } from '@nestjs/bull';
import { Logger } from '@nestjs/common';
import { Job } from 'bull';
import { SavingsService, SavingsTransaction } from '../savings.service';
import { GamificationService } from '../services/gamification.service';
import { GoalsService } from '../services/goals.service';
 
@Processor('savings-processing')
export class SavingsProcessor {
  private readonly logger = new Logger(SavingsProcessor.name);
 
  constructor(
    private savingsService: SavingsService,
    private gamificationService: GamificationService,
    private goalsService: GoalsService,
  ) {}
 
  @Process('record-savings')
  async handleSavingsRecord(job: Job<SavingsTransaction>) {
    const transaction = job.data;
    this.logger.log(`Processing savings record for user ${transaction.userId}`);
 
    try {
      // Record the savings
      await this.savingsService.recordSavings(transaction);
 
      // Update goal progress
      await this.goalsService.updateGoalProgress(transaction.userId, transaction.amount);
 
      // Check for new achievements
      await this.gamificationService.checkAndAwardAchievements(transaction.userId);
 
      this.logger.log(`Successfully processed savings record for user ${transaction.userId}`);
    } catch (error) {
      this.logger.error(`Failed to process savings record for user ${transaction.userId}`, error);
      throw error;
    }
  }
 
  @Process('update-leaderboard')
  async handleLeaderboardUpdate(job: Job) {
    this.logger.log('Processing leaderboard update');
 
    try {
      // This would update cached leaderboard data
      // For now, just log the completion
      this.logger.log('Leaderboard update completed');
    } catch (error) {
      this.logger.error('Failed to update leaderboard', error);
      throw error;
    }
  }
 
  @Process('monthly-reset')
  async handleMonthlyReset(job: Job) {
    this.logger.log('Processing monthly savings reset');
 
    try {
      await this.savingsService.resetMonthlySavings();
      this.logger.log('Monthly savings reset completed');
    } catch (error) {
      this.logger.error('Failed to reset monthly savings', error);
      throw error;
    }
  }
 
  @Process('yearly-reset')
  async handleYearlyReset(job: Job) {
    this.logger.log('Processing yearly savings reset');
 
    try {
      await this.savingsService.resetYearlySavings();
      this.logger.log('Yearly savings reset completed');
    } catch (error) {
      this.logger.error('Failed to reset yearly savings', error);
      throw error;
    }
  }
 
  @Process('achievement-check')
  async handleAchievementCheck(job: Job<{ userId: string }>) {
    const { userId } = job.data;
    this.logger.log(`Processing achievement check for user ${userId}`);
 
    try {
      const newAchievements = await this.gamificationService.checkAndAwardAchievements(userId);
      
      Iif (newAchievements.length > 0) {
        this.logger.log(`Awarded ${newAchievements.length} new achievements to user ${userId}`);
      }
    } catch (error) {
      this.logger.error(`Failed to check achievements for user ${userId}`, error);
      throw error;
    }
  }
}