Files
gridpilot.gg/core/racing/application/use-cases/GetLeagueOwnerSummaryUseCase.ts
2025-12-21 00:43:42 +01:00

65 lines
2.0 KiB
TypeScript

import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { UseCaseOutputPort } from '@core/shared/application';
import type { Driver } from '../../domain/entities/Driver';
import type { League } from '../../domain/entities/League';
export type GetLeagueOwnerSummaryInput = {
leagueId: string;
};
export type GetLeagueOwnerSummaryResult = {
league: League;
owner: Driver;
rating: number;
rank: number;
};
export type GetLeagueOwnerSummaryErrorCode =
| 'LEAGUE_NOT_FOUND'
| 'OWNER_NOT_FOUND'
| 'REPOSITORY_ERROR';
export class GetLeagueOwnerSummaryUseCase {
constructor(
private readonly leagueRepository: ILeagueRepository,
private readonly driverRepository: IDriverRepository,
private readonly output: UseCaseOutputPort<GetLeagueOwnerSummaryResult>,
) {}
async execute(
input: GetLeagueOwnerSummaryInput,
): Promise<Result<void, ApplicationErrorCode<GetLeagueOwnerSummaryErrorCode, { 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' } });
}
const ownerId = league.ownerId.toString();
const owner = await this.driverRepository.findById(ownerId);
if (!owner) {
return Result.err({ code: 'OWNER_NOT_FOUND', details: { message: 'League owner not found' } });
}
this.output.present({
league,
owner,
rating: 0,
rank: 0,
});
return Result.ok(undefined);
} catch (error) {
const message =
error instanceof Error && error.message
? error.message
: 'Failed to fetch league owner summary';
return Result.err({ code: 'REPOSITORY_ERROR', details: { message } });
}
}
}