refactor racing use cases
This commit is contained in:
@@ -1,33 +1,83 @@
|
||||
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
|
||||
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
|
||||
import type { AsyncUseCase } from '@core/shared/application';
|
||||
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
|
||||
import type { Driver } from '../../domain/entities/Driver';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { GetLeagueJoinRequestsOutputPort } from '../ports/output/GetLeagueJoinRequestsOutputPort';
|
||||
|
||||
export interface GetLeagueJoinRequestsUseCaseParams {
|
||||
export interface GetLeagueJoinRequestsInput {
|
||||
leagueId: string;
|
||||
}
|
||||
|
||||
export class GetLeagueJoinRequestsUseCase implements AsyncUseCase<GetLeagueJoinRequestsUseCaseParams, GetLeagueJoinRequestsOutputPort, 'NO_ERROR'> {
|
||||
export type GetLeagueJoinRequestsErrorCode = 'LEAGUE_NOT_FOUND' | 'REPOSITORY_ERROR';
|
||||
|
||||
export interface LeagueJoinRequestWithDriver {
|
||||
id: string;
|
||||
leagueId: string;
|
||||
driverId: string;
|
||||
requestedAt: Date;
|
||||
message?: string;
|
||||
driver: Driver;
|
||||
}
|
||||
|
||||
export interface GetLeagueJoinRequestsResult {
|
||||
joinRequests: LeagueJoinRequestWithDriver[];
|
||||
}
|
||||
|
||||
export class GetLeagueJoinRequestsUseCase {
|
||||
constructor(
|
||||
private readonly leagueMembershipRepository: ILeagueMembershipRepository,
|
||||
private readonly driverRepository: IDriverRepository,
|
||||
private readonly leagueRepository: ILeagueRepository,
|
||||
private readonly output: UseCaseOutputPort<GetLeagueJoinRequestsResult>,
|
||||
) {}
|
||||
|
||||
async execute(params: GetLeagueJoinRequestsUseCaseParams): Promise<Result<GetLeagueJoinRequestsOutputPort, ApplicationErrorCode<'NO_ERROR'>>> {
|
||||
const joinRequests = await this.leagueMembershipRepository.getJoinRequests(params.leagueId);
|
||||
const driverIds = [...new Set(joinRequests.map(r => r.driverId))];
|
||||
const drivers = await Promise.all(driverIds.map(id => this.driverRepository.findById(id)));
|
||||
const driverMap = new Map(drivers.filter(d => d !== null).map(d => [d!.id, { id: d!.id, name: d!.name }]));
|
||||
const enrichedJoinRequests = joinRequests
|
||||
.filter(request => driverMap.has(request.driverId))
|
||||
.map(request => ({
|
||||
...request,
|
||||
driver: driverMap.get(request.driverId)!,
|
||||
}));
|
||||
return Result.ok({
|
||||
joinRequests: enrichedJoinRequests,
|
||||
});
|
||||
async execute(
|
||||
input: GetLeagueJoinRequestsInput,
|
||||
): Promise<Result<void, ApplicationErrorCode<GetLeagueJoinRequestsErrorCode, { message: string }>>> {
|
||||
try {
|
||||
const leagueExists = await this.leagueRepository.exists(input.leagueId);
|
||||
|
||||
if (!leagueExists) {
|
||||
return Result.err({
|
||||
code: 'LEAGUE_NOT_FOUND',
|
||||
details: { message: 'League not found' },
|
||||
});
|
||||
}
|
||||
|
||||
const joinRequests = await this.leagueMembershipRepository.getJoinRequests(input.leagueId);
|
||||
const driverIds = [...new Set(joinRequests.map(request => request.driverId))];
|
||||
const drivers = await Promise.all(driverIds.map(id => this.driverRepository.findById(id)));
|
||||
|
||||
const driverMap = new Map(
|
||||
drivers.filter((driver): driver is Driver => driver !== null).map(driver => [driver.id, driver]),
|
||||
);
|
||||
|
||||
const enrichedJoinRequests: LeagueJoinRequestWithDriver[] = joinRequests
|
||||
.filter(request => driverMap.has(request.driverId))
|
||||
.map(request => ({
|
||||
...request,
|
||||
driver: driverMap.get(request.driverId)!,
|
||||
}));
|
||||
|
||||
const result: GetLeagueJoinRequestsResult = {
|
||||
joinRequests: enrichedJoinRequests,
|
||||
};
|
||||
|
||||
this.output.present(result);
|
||||
|
||||
return Result.ok(undefined);
|
||||
} catch (error: unknown) {
|
||||
const message =
|
||||
error && typeof error === 'object' && 'message' in error && typeof (error as any).message === 'string'
|
||||
? (error as any).message
|
||||
: 'Failed to load league join requests';
|
||||
|
||||
return Result.err({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: { message },
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user