refactor racing use cases
This commit is contained in:
@@ -1,19 +1,65 @@
|
||||
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
|
||||
import type { AsyncUseCase } from '@core/shared/application';
|
||||
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { GetLeagueOwnerSummaryOutputPort } from '../ports/output/GetLeagueOwnerSummaryOutputPort';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application';
|
||||
import type { Driver } from '../../domain/entities/Driver';
|
||||
import type { League } from '../../domain/entities/League';
|
||||
|
||||
export interface GetLeagueOwnerSummaryUseCaseParams {
|
||||
ownerId: string;
|
||||
}
|
||||
export type GetLeagueOwnerSummaryInput = {
|
||||
leagueId: string;
|
||||
};
|
||||
|
||||
export class GetLeagueOwnerSummaryUseCase implements AsyncUseCase<GetLeagueOwnerSummaryUseCaseParams, GetLeagueOwnerSummaryOutputPort, 'NO_ERROR'> {
|
||||
constructor(private readonly driverRepository: IDriverRepository) {}
|
||||
export type GetLeagueOwnerSummaryResult = {
|
||||
league: League;
|
||||
owner: Driver;
|
||||
rating: number;
|
||||
rank: number;
|
||||
};
|
||||
|
||||
async execute(params: GetLeagueOwnerSummaryUseCaseParams): Promise<Result<GetLeagueOwnerSummaryOutputPort, ApplicationErrorCode<'NO_ERROR'>>> {
|
||||
const driver = await this.driverRepository.findById(params.ownerId);
|
||||
const summary = driver ? { driver: { id: driver.id, iracingId: driver.iracingId.toString(), name: driver.name.toString(), country: driver.country.toString(), bio: driver.bio?.toString(), joinedAt: driver.joinedAt.toDate().toISOString() }, rating: 0, rank: 0 } : null;
|
||||
return Result.ok({ summary });
|
||||
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 } });
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user