Files
gridpilot.gg/core/racing/application/use-cases/GetAllLeaguesWithCapacityUseCase.ts
2025-12-19 19:42:19 +01:00

47 lines
1.6 KiB
TypeScript

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 { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
/**
* Use Case for retrieving all leagues with capacity information.
* Orchestrates domain logic and returns result.
*/
export class GetAllLeaguesWithCapacityUseCase
implements AsyncUseCase<void, AllLeaguesWithCapacityOutputPort, string>
{
constructor(
private readonly leagueRepository: ILeagueRepository,
private readonly leagueMembershipRepository: ILeagueMembershipRepository,
) {}
async execute(): Promise<Result<AllLeaguesWithCapacityOutputPort, ApplicationErrorCode<string>>> {
const leagues = await this.leagueRepository.findAll();
const memberCounts: Record<string, number> = {};
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;
memberCounts[league.id] = usedSlots;
}
const output: AllLeaguesWithCapacityOutputPort = {
leagues,
memberCounts,
};
return Result.ok(output);
}
}