Files
gridpilot.gg/core/racing/application/use-cases/GetLeagueAdminUseCase.ts
2026-01-16 19:46:49 +01:00

40 lines
1.2 KiB
TypeScript

import { Result } from '@core/shared/domain/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { League } from '../../domain/entities/League';
import type { LeagueRepository } from '../../domain/repositories/LeagueRepository';
export type GetLeagueAdminInput = {
leagueId: string;
};
export type GetLeagueAdminResult = {
league: League;
};
export type GetLeagueAdminErrorCode = 'LEAGUE_NOT_FOUND' | 'REPOSITORY_ERROR';
export class GetLeagueAdminUseCase {
constructor(
private readonly leagueRepository: LeagueRepository,
) {}
async execute(
input: GetLeagueAdminInput,
): Promise<Result<GetLeagueAdminResult, ApplicationErrorCode<GetLeagueAdminErrorCode, { message: string }>>> {
try {
const league = await this.leagueRepository.findById(input.leagueId);
if (!league) {
return Result.err({ code: 'LEAGUE_NOT_FOUND', details: { message: 'League not found' } });
}
return Result.ok({ league });
} catch (error) {
const err = error instanceof Error ? error : new Error('Unknown error');
return Result.err({
code: 'REPOSITORY_ERROR',
details: { message: err.message ?? 'Failed to load league admin data' },
});
}
}
}