54 lines
1.7 KiB
TypeScript
54 lines
1.7 KiB
TypeScript
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
|
|
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
|
|
import type {
|
|
IAllLeaguesWithCapacityPresenter,
|
|
AllLeaguesWithCapacityResultDTO,
|
|
AllLeaguesWithCapacityViewModel,
|
|
} from '../presenters/IAllLeaguesWithCapacityPresenter';
|
|
import type { UseCase } from '@core/shared/application/UseCase';
|
|
|
|
/**
|
|
* Use Case for retrieving all leagues with capacity information.
|
|
* Orchestrates domain logic and delegates presentation to the presenter.
|
|
*/
|
|
export class GetAllLeaguesWithCapacityUseCase
|
|
implements UseCase<void, AllLeaguesWithCapacityResultDTO, AllLeaguesWithCapacityViewModel, IAllLeaguesWithCapacityPresenter>
|
|
{
|
|
constructor(
|
|
private readonly leagueRepository: ILeagueRepository,
|
|
private readonly leagueMembershipRepository: ILeagueMembershipRepository,
|
|
) {}
|
|
|
|
async execute(
|
|
_input: void,
|
|
presenter: IAllLeaguesWithCapacityPresenter,
|
|
): Promise<void> {
|
|
presenter.reset();
|
|
|
|
const leagues = await this.leagueRepository.findAll();
|
|
|
|
const memberCounts = new Map<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.set(league.id, usedSlots);
|
|
}
|
|
|
|
const dto: AllLeaguesWithCapacityResultDTO = {
|
|
leagues,
|
|
memberCounts,
|
|
};
|
|
|
|
presenter.present(dto);
|
|
}
|
|
} |