refactor racing use cases
This commit is contained in:
@@ -3,76 +3,126 @@ import type { ILeagueMembershipRepository } from '../../domain/repositories/ILea
|
||||
import type { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
|
||||
import type { ILeagueScoringConfigRepository } from '../../domain/repositories/ILeagueScoringConfigRepository';
|
||||
import type { IGameRepository } from '../../domain/repositories/IGameRepository';
|
||||
import type { LeagueScoringPresetProvider } from '../ports/LeagueScoringPresetProvider';
|
||||
import type { LeagueEnrichedData, AllLeaguesWithCapacityAndScoringOutputPort } from '../ports/output/AllLeaguesWithCapacityAndScoringOutputPort';
|
||||
import type { League } from '../../domain/entities/League';
|
||||
import type { Season } from '../../domain/entities/season/Season';
|
||||
import type { LeagueScoringConfig } from '../../domain/entities/LeagueScoringConfig';
|
||||
import type { Game } from '../../domain/entities/Game';
|
||||
import type { LeagueScoringPreset } from '../../../bootstrap/LeagueScoringPresets';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
|
||||
export type GetAllLeaguesWithCapacityAndScoringInput = {};
|
||||
|
||||
export type LeagueCapacityAndScoringSummary = {
|
||||
league: League;
|
||||
currentDrivers: number;
|
||||
maxDrivers: number;
|
||||
season?: Season;
|
||||
scoringConfig?: LeagueScoringConfig;
|
||||
game?: Game;
|
||||
preset?: LeagueScoringPreset;
|
||||
};
|
||||
|
||||
export type GetAllLeaguesWithCapacityAndScoringResult = {
|
||||
leagues: LeagueCapacityAndScoringSummary[];
|
||||
};
|
||||
|
||||
export type GetAllLeaguesWithCapacityAndScoringErrorCode = 'REPOSITORY_ERROR';
|
||||
|
||||
/**
|
||||
* Use Case for retrieving all leagues with capacity and scoring information.
|
||||
* Orchestrates domain logic and delegates presentation to the presenter.
|
||||
* Orchestrates domain logic and delegates presentation to an output port.
|
||||
*/
|
||||
export class GetAllLeaguesWithCapacityAndScoringUseCase
|
||||
{
|
||||
export class GetAllLeaguesWithCapacityAndScoringUseCase {
|
||||
constructor(
|
||||
private readonly leagueRepository: ILeagueRepository,
|
||||
private readonly leagueMembershipRepository: ILeagueMembershipRepository,
|
||||
private readonly seasonRepository: ISeasonRepository,
|
||||
private readonly leagueScoringConfigRepository: ILeagueScoringConfigRepository,
|
||||
private readonly gameRepository: IGameRepository,
|
||||
private readonly presetProvider: LeagueScoringPresetProvider,
|
||||
private readonly presetProvider: { getPresetById(presetId: string): LeagueScoringPreset | undefined },
|
||||
private readonly output: UseCaseOutputPort<GetAllLeaguesWithCapacityAndScoringResult>,
|
||||
) {}
|
||||
|
||||
async execute(): Promise<Result<AllLeaguesWithCapacityAndScoringOutputPort>> {
|
||||
const leagues = await this.leagueRepository.findAll();
|
||||
async execute(
|
||||
_input: GetAllLeaguesWithCapacityAndScoringInput = {},
|
||||
): Promise<
|
||||
Result<
|
||||
void,
|
||||
ApplicationErrorCode<
|
||||
GetAllLeaguesWithCapacityAndScoringErrorCode,
|
||||
{ message: string }
|
||||
>
|
||||
>
|
||||
> {
|
||||
try {
|
||||
const leagues = await this.leagueRepository.findAll();
|
||||
|
||||
const enrichedLeagues: LeagueEnrichedData[] = [];
|
||||
const enrichedLeagues: LeagueCapacityAndScoringSummary[] = [];
|
||||
|
||||
for (const league of leagues) {
|
||||
const members = await this.leagueMembershipRepository.getLeagueMembers(league.id);
|
||||
for (const league of leagues) {
|
||||
const members = await this.leagueMembershipRepository.getLeagueMembers(league.id);
|
||||
|
||||
const usedDriverSlots = members.filter(
|
||||
(m) =>
|
||||
m.status === 'active' &&
|
||||
(m.role === 'owner' ||
|
||||
m.role === 'admin' ||
|
||||
m.role === 'steward' ||
|
||||
m.role === 'member'),
|
||||
).length;
|
||||
const currentDrivers = members.filter(
|
||||
(m) =>
|
||||
m.status === 'active' &&
|
||||
(m.role === 'owner' ||
|
||||
m.role === 'admin' ||
|
||||
m.role === 'steward' ||
|
||||
m.role === 'member'),
|
||||
).length;
|
||||
|
||||
const seasons = await this.seasonRepository.findByLeagueId(league.id);
|
||||
const activeSeason =
|
||||
seasons && seasons.length > 0
|
||||
? seasons.find((s) => s.status === 'active') ?? seasons[0]
|
||||
: undefined;
|
||||
const seasons = await this.seasonRepository.findByLeagueId(league.id);
|
||||
const activeSeason =
|
||||
seasons && seasons.length > 0
|
||||
? seasons.find((s) => s.status === 'active') ?? seasons[0]
|
||||
: undefined;
|
||||
|
||||
let scoringConfig: LeagueEnrichedData['scoringConfig'];
|
||||
let game: LeagueEnrichedData['game'];
|
||||
let preset: LeagueEnrichedData['preset'];
|
||||
let scoringConfig: LeagueScoringConfig | undefined;
|
||||
let game: Game | undefined;
|
||||
let preset: LeagueScoringPreset | undefined;
|
||||
|
||||
if (activeSeason) {
|
||||
const scoringConfigResult =
|
||||
await this.leagueScoringConfigRepository.findBySeasonId(activeSeason.id);
|
||||
scoringConfig = scoringConfigResult ?? undefined;
|
||||
if (scoringConfig) {
|
||||
const gameResult = await this.gameRepository.findById(activeSeason.gameId);
|
||||
game = gameResult ?? undefined;
|
||||
const presetId = scoringConfig.scoringPresetId;
|
||||
if (presetId) {
|
||||
preset = this.presetProvider.getPresetById(presetId);
|
||||
if (activeSeason) {
|
||||
const scoringConfigResult =
|
||||
await this.leagueScoringConfigRepository.findBySeasonId(activeSeason.id);
|
||||
scoringConfig = scoringConfigResult ?? undefined;
|
||||
if (scoringConfig) {
|
||||
const gameResult = await this.gameRepository.findById(activeSeason.gameId);
|
||||
game = gameResult ?? undefined;
|
||||
const presetId = scoringConfig.scoringPresetId;
|
||||
if (presetId) {
|
||||
preset = this.presetProvider.getPresetById(presetId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const maxDrivers = league.settings.maxDrivers ?? 0;
|
||||
|
||||
enrichedLeagues.push({
|
||||
league,
|
||||
currentDrivers,
|
||||
maxDrivers,
|
||||
...(activeSeason ? { season: activeSeason } : {}),
|
||||
...(scoringConfig ? { scoringConfig } : {}),
|
||||
...(game ? { game } : {}),
|
||||
...(preset ? { preset } : {}),
|
||||
});
|
||||
}
|
||||
|
||||
enrichedLeagues.push({
|
||||
league,
|
||||
usedDriverSlots,
|
||||
...(activeSeason ? { season: activeSeason } : {}),
|
||||
...(scoringConfig ? { scoringConfig } : {}),
|
||||
...(game ? { game } : {}),
|
||||
...(preset ? { preset } : {}),
|
||||
this.output.present({ leagues: enrichedLeagues });
|
||||
|
||||
return Result.ok(undefined);
|
||||
} catch (error: unknown) {
|
||||
const message =
|
||||
error instanceof Error && error.message
|
||||
? error.message
|
||||
: 'Failed to load leagues with capacity and scoring';
|
||||
|
||||
return Result.err({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: { message },
|
||||
});
|
||||
}
|
||||
return Result.ok({ leagues: enrichedLeagues });
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user