refactor use cases
This commit is contained in:
@@ -8,7 +8,6 @@ import { RaceResultGenerator } from '../utils/RaceResultGenerator';
|
||||
import { RatingUpdateService } from '@core/identity/domain/services/RatingUpdateService';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
import type { IRaceResultsProvider } from '@core/identity/application/ports/IRaceResultsProvider';
|
||||
|
||||
export interface CompleteRaceWithRatingsInput {
|
||||
@@ -36,19 +35,17 @@ interface DriverRatingProvider {
|
||||
* EVOLVED (Slice 7): Now uses ledger-based rating updates for transparency and auditability.
|
||||
*/
|
||||
export class CompleteRaceUseCaseWithRatings {
|
||||
constructor(
|
||||
private readonly raceRepository: IRaceRepository,
|
||||
constructor(private readonly raceRepository: IRaceRepository,
|
||||
private readonly raceRegistrationRepository: IRaceRegistrationRepository,
|
||||
private readonly resultRepository: IResultRepository,
|
||||
private readonly standingRepository: IStandingRepository,
|
||||
private readonly driverRatingProvider: DriverRatingProvider,
|
||||
private readonly ratingUpdateService: RatingUpdateService,
|
||||
private readonly output: UseCaseOutputPort<CompleteRaceWithRatingsResult>,
|
||||
private readonly raceResultsProvider?: IRaceResultsProvider, // Optional: for new ledger flow
|
||||
) {}
|
||||
|
||||
async execute(command: CompleteRaceWithRatingsInput): Promise<
|
||||
Result<void, ApplicationErrorCode<CompleteRaceWithRatingsErrorCode, { message: string }>>
|
||||
Result<CompleteRaceWithRatingsResult, ApplicationErrorCode<CompleteRaceWithRatingsErrorCode, { message: string }>>
|
||||
> {
|
||||
try {
|
||||
const { raceId } = command;
|
||||
@@ -96,6 +93,8 @@ export class CompleteRaceUseCaseWithRatings {
|
||||
|
||||
await this.updateStandings(race.leagueId, results);
|
||||
|
||||
const ratingsUpdatedForDriverIds: string[] = [];
|
||||
|
||||
// SLICE 7: Use new ledger-based approach if raceResultsProvider is available
|
||||
// This provides backward compatibility while evolving to event-driven architecture
|
||||
try {
|
||||
@@ -121,15 +120,20 @@ export class CompleteRaceUseCaseWithRatings {
|
||||
if (!ratingResult.success) {
|
||||
console.warn(`[Slice 7] Ledger-based rating update failed for race ${raceId}, falling back to legacy method`);
|
||||
await this.updateDriverRatingsLegacy(results, registeredDriverIds.length);
|
||||
ratingsUpdatedForDriverIds.push(...registeredDriverIds);
|
||||
} else {
|
||||
ratingsUpdatedForDriverIds.push(...(ratingResult.driversUpdated || []));
|
||||
}
|
||||
} catch (error) {
|
||||
// If ledger approach throws error, fall back to legacy method
|
||||
console.warn(`[Slice 7] Ledger-based rating update threw error for race ${raceId}, falling back to legacy method: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
||||
await this.updateDriverRatingsLegacy(results, registeredDriverIds.length);
|
||||
ratingsUpdatedForDriverIds.push(...registeredDriverIds);
|
||||
}
|
||||
} else {
|
||||
// BACKWARD COMPATIBLE: Use legacy direct update approach
|
||||
await this.updateDriverRatingsLegacy(results, registeredDriverIds.length);
|
||||
ratingsUpdatedForDriverIds.push(...registeredDriverIds);
|
||||
}
|
||||
} catch (error) {
|
||||
return Result.err({
|
||||
@@ -143,12 +147,10 @@ export class CompleteRaceUseCaseWithRatings {
|
||||
const completedRace = race.complete();
|
||||
await this.raceRepository.update(completedRace);
|
||||
|
||||
this.output.present({
|
||||
return Result.ok({
|
||||
raceId,
|
||||
ratingsUpdatedForDriverIds: registeredDriverIds,
|
||||
ratingsUpdatedForDriverIds,
|
||||
});
|
||||
|
||||
return Result.ok(undefined);
|
||||
} catch (error) {
|
||||
return Result.err({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
|
||||
Reference in New Issue
Block a user