49 lines
1.6 KiB
TypeScript
49 lines
1.6 KiB
TypeScript
import { TeamRatingEvent } from '@core/racing/domain/entities/TeamRatingEvent';
|
|
import type { TeamRatingEventRepository } from '@core/racing/domain/repositories/TeamRatingEventRepository';
|
|
import type { TeamRatingRepository } from '@core/racing/domain/repositories/TeamRatingRepository';
|
|
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: TeamRatingEventRepository,
|
|
private readonly ratingRepository: TeamRatingRepository,
|
|
) {}
|
|
|
|
/**
|
|
* Execute the use case
|
|
*
|
|
* @param events - Array of rating events to append
|
|
* @returns The updated team rating snapshot
|
|
*/
|
|
async execute(events: TeamRatingEvent[]): Promise<void> {
|
|
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);
|
|
}
|
|
}
|
|
} |