refactor racing use cases

This commit is contained in:
2025-12-21 00:43:42 +01:00
parent e9d6f90bb2
commit c12656d671
308 changed files with 14401 additions and 7419 deletions

View File

@@ -1,47 +1,78 @@
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
import type { AllLeaguesWithCapacityOutputPort } from '../ports/output/AllLeaguesWithCapacityOutputPort';
import type { AsyncUseCase } from '@core/shared/application';
import { Result } from '@/shared/application/Result';
import type { League } from '../../domain/entities/League';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
export type GetAllLeaguesWithCapacityInput = {};
export type LeagueCapacitySummary = {
league: League;
currentDrivers: number;
maxDrivers: number;
};
export type GetAllLeaguesWithCapacityResult = {
leagues: LeagueCapacitySummary[];
};
export type GetAllLeaguesWithCapacityErrorCode = 'REPOSITORY_ERROR';
/**
* Use Case for retrieving all leagues with capacity information.
* Orchestrates domain logic and returns result.
* Orchestrates domain logic and delegates presentation to an output port.
*/
export class GetAllLeaguesWithCapacityUseCase
implements AsyncUseCase<void, AllLeaguesWithCapacityOutputPort, string>
{
export class GetAllLeaguesWithCapacityUseCase {
constructor(
private readonly leagueRepository: ILeagueRepository,
private readonly leagueMembershipRepository: ILeagueMembershipRepository,
private readonly output: UseCaseOutputPort<GetAllLeaguesWithCapacityResult>,
) {}
async execute(): Promise<Result<AllLeaguesWithCapacityOutputPort, ApplicationErrorCode<string>>> {
const leagues = await this.leagueRepository.findAll();
async execute(
_input: GetAllLeaguesWithCapacityInput = {},
): Promise<
Result<
void,
ApplicationErrorCode<GetAllLeaguesWithCapacityErrorCode, { message: string }>
>
> {
try {
const leagues = await this.leagueRepository.findAll();
const memberCounts: Record<string, number> = {};
const summaries: LeagueCapacitySummary[] = [];
for (const league of leagues) {
const members = await this.leagueMembershipRepository.getLeagueMembers(league.id);
for (const league of leagues) {
const members = await this.leagueMembershipRepository.getLeagueMembers(league.id);
const usedSlots = members.filter(
(m) =>
m.status === 'active' &&
(m.role === 'owner' ||
m.role === 'admin' ||
m.role === 'steward' ||
m.role === 'member'),
).length;
const currentDrivers = members.filter(
(m) =>
m.status === 'active' &&
(m.role === 'owner' ||
m.role === 'admin' ||
m.role === 'steward' ||
m.role === 'member'),
).length;
memberCounts[league.id] = usedSlots;
const maxDrivers = league.settings.maxDrivers ?? 0;
summaries.push({ league, currentDrivers, maxDrivers });
}
this.output.present({ leagues: summaries });
return Result.ok(undefined);
} catch (error: unknown) {
const message =
error instanceof Error && error.message
? error.message
: 'Failed to load leagues with capacity';
return Result.err({
code: 'REPOSITORY_ERROR',
details: { message },
});
}
const output: AllLeaguesWithCapacityOutputPort = {
leagues,
memberCounts,
};
return Result.ok(output);
}
}