All files / libs/payout-calculation/compute-engine/core-engine/src/engine calculationUpdater.ts

79.16% Statements 38/48
100% Branches 3/3
100% Functions 3/3
79.16% Lines 38/48

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 491x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 80x 80x 80x 80x 1x 1x 1x 1x 1x 80x                     80x 80x 1x 1x 1x 1x 1x 80x 80x 1x  
import { Logger } from '@nestjs/common';
import { type Repository } from 'typeorm';
 
import { type Calculation, type Company } from '@amalia/core/models';
import { toError } from '@amalia/ext/typescript';
 
const refreshInterval = 5000;
 
/**
 * Calculation updater: once started, will update calculation last updated date
 * every ${refreshInterval} seconds.
 */
export class CalculationUpdater {
  private readonly logger = new Logger(CalculationUpdater.name);
 
  private calculationInterval: NodeJS.Timeout | undefined = undefined;
 
  public constructor(
    private readonly company: Company,
    private readonly calculationRepository: Repository<Calculation>,
    private readonly calculation: Calculation,
  ) {}
 
  /**
   * Start the calculation updater.
   */
  public start() {
    this.calculationInterval = setInterval(async () => {
      try {
        await this.calculationRepository.update({ id: this.calculation.id }, { lastUpdatedAt: new Date() });
      } catch (e) {
        const error = toError(e);
        this.logger.error({
          message: error.message,
          error,
          companyName: this.company.name,
        });
      }
    }, refreshInterval);
  }
 
  /**
   * Stop the calculation updater.
   */
  public stop() {
    clearInterval(this.calculationInterval);
  }
}