79 lines
3.2 KiB
TypeScript
79 lines
3.2 KiB
TypeScript
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
|
|
import type { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
|
|
import type { ILeagueScoringConfigRepository } from '../../domain/repositories/ILeagueScoringConfigRepository';
|
|
import type { IGameRepository } from '../../domain/repositories/IGameRepository';
|
|
import type { GetLeagueScoringPresetByIdInputPort } from '../ports/input/GetLeagueScoringPresetByIdInputPort';
|
|
import type { LeagueScoringPresetOutputPort } from '../ports/output/LeagueScoringPresetOutputPort';
|
|
import type { LeagueScoringConfigOutputPort } from '../ports/output/LeagueScoringConfigOutputPort';
|
|
import type { AsyncUseCase } from '@core/shared/application/AsyncUseCase';
|
|
import { Result } from '@core/shared/application/Result';
|
|
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
|
|
|
type GetLeagueScoringConfigErrorCode =
|
|
| 'LEAGUE_NOT_FOUND'
|
|
| 'NO_SEASONS'
|
|
| 'NO_ACTIVE_SEASON'
|
|
| 'NO_SCORING_CONFIG'
|
|
| 'GAME_NOT_FOUND';
|
|
|
|
/**
|
|
* Use Case for retrieving a league's scoring configuration for its active season.
|
|
*/
|
|
export class GetLeagueScoringConfigUseCase
|
|
implements AsyncUseCase<{ leagueId: string }, LeagueScoringConfigOutputPort, GetLeagueScoringConfigErrorCode>
|
|
{
|
|
constructor(
|
|
private readonly leagueRepository: ILeagueRepository,
|
|
private readonly seasonRepository: ISeasonRepository,
|
|
private readonly leagueScoringConfigRepository: ILeagueScoringConfigRepository,
|
|
private readonly gameRepository: IGameRepository,
|
|
private readonly getLeagueScoringPresetById: (input: GetLeagueScoringPresetByIdInputPort) => Promise<LeagueScoringPresetOutputPort | undefined>,
|
|
) {}
|
|
|
|
async execute(params: { leagueId: string }): Promise<Result<LeagueScoringConfigOutputPort, ApplicationErrorCode<GetLeagueScoringConfigErrorCode>>> {
|
|
const { leagueId } = params;
|
|
|
|
const league = await this.leagueRepository.findById(leagueId);
|
|
if (!league) {
|
|
return Result.err({ code: 'LEAGUE_NOT_FOUND' });
|
|
}
|
|
|
|
const seasons = await this.seasonRepository.findByLeagueId(leagueId);
|
|
if (!seasons || seasons.length === 0) {
|
|
return Result.err({ code: 'NO_SEASONS' });
|
|
}
|
|
|
|
const activeSeason =
|
|
seasons.find((s) => s.status === 'active') ?? seasons[0];
|
|
|
|
if (!activeSeason) {
|
|
return Result.err({ code: 'NO_ACTIVE_SEASON' });
|
|
}
|
|
|
|
const scoringConfig =
|
|
await this.leagueScoringConfigRepository.findBySeasonId(activeSeason.id);
|
|
if (!scoringConfig) {
|
|
return Result.err({ code: 'NO_SCORING_CONFIG' });
|
|
}
|
|
|
|
const game = await this.gameRepository.findById(activeSeason.gameId);
|
|
if (!game) {
|
|
return Result.err({ code: 'GAME_NOT_FOUND' });
|
|
}
|
|
|
|
const presetId = scoringConfig.scoringPresetId;
|
|
const preset = presetId ? await this.getLeagueScoringPresetById({ presetId }) : undefined;
|
|
|
|
const output: LeagueScoringConfigOutputPort = {
|
|
leagueId: league.id,
|
|
seasonId: activeSeason.id,
|
|
gameId: game.id,
|
|
gameName: game.name,
|
|
...(presetId !== undefined ? { scoringPresetId: presetId } : {}),
|
|
...(preset !== undefined ? { preset } : {}),
|
|
championships: scoringConfig.championships,
|
|
};
|
|
|
|
return Result.ok(output);
|
|
}
|
|
} |