import type { ITeamRatingEventRepository } from '@core/racing/domain/repositories/ITeamRatingEventRepository'; import type { ITeamRatingRepository } from '@core/racing/domain/repositories/ITeamRatingRepository'; import { TeamRatingEvent } from '@core/racing/domain/entities/TeamRatingEvent'; import { TeamRatingSnapshotCalculator } from '@core/racing/domain/services/TeamRatingSnapshotCalculator'; /** * Use Case: AppendTeamRatingEventsUseCase * * Appends new rating events to the ledger and updates the team rating snapshot. * Mirrors the AppendRatingEventsUseCase pattern for users. */ export class AppendTeamRatingEventsUseCase { constructor( private readonly ratingEventRepository: ITeamRatingEventRepository, private readonly ratingRepository: ITeamRatingRepository, ) {} /** * Execute the use case * * @param events - Array of rating events to append * @returns The updated team rating snapshot */ async execute(events: TeamRatingEvent[]): Promise { if (events.length === 0) { return; } // Get unique team IDs from events const teamIds = [...new Set(events.map(e => e.teamId))]; // Save all events for (const event of events) { await this.ratingEventRepository.save(event); } // Update snapshots for each affected team for (const teamId of teamIds) { // Get all events for this team const allEvents = await this.ratingEventRepository.getAllByTeamId(teamId); // Calculate new snapshot const snapshot = TeamRatingSnapshotCalculator.calculate(teamId, allEvents); // Save snapshot await this.ratingRepository.save(snapshot); } } }