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 | import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { In, Repository } from 'typeorm'; import { Calculation, Company } from '@amalia/core/models'; import { CALCULATION_ONGOING_STATUSES, CalculationStatus } from '@amalia/core/types'; import { DataRefreshmentsService, StopRefreshmentUseCase } from '@amalia/data-capture/imports/core'; import { promiseAllSettledAutoThrow } from '@amalia/ext/typescript'; @Injectable() export class StopCalculationsUseCase { public constructor( @InjectRepository(Calculation) private readonly calculationRepository: Repository<Calculation>, private readonly refreshmentsService: DataRefreshmentsService, private readonly stopRefreshmentUseCase: StopRefreshmentUseCase, ) {} /** * Mark a calculation as stopping to request its stop. * @param company */ public async execute(company: Company): Promise<void> { const calculationsToStop = await this.calculationRepository.find({ where: { company: { id: company.id }, status: In(CALCULATION_ONGOING_STATUSES) }, }); await promiseAllSettledAutoThrow( calculationsToStop.map(async (calculationToStop) => { // If the calculation was still at the refresh step, we can mark it as stopped right now (because the engine hasn't started yet). calculationToStop.status = calculationToStop.status === CalculationStatus.REFRESH ? CalculationStatus.STOPPED : CalculationStatus.STOPPING; await this.calculationRepository.save(calculationToStop); await this.eventuallyStopRefreshments(company, calculationToStop.id); }), ); } private async eventuallyStopRefreshments(company: Company, calculationId: string) { const refreshments = await this.refreshmentsService.getRefreshmentsByTriggerCalculationId(company, calculationId); await Promise.allSettled( refreshments.map((r) => this.stopRefreshmentUseCase.execute({ refreshmentId: r.id, forceStop: true })), ); } } |