108 lines
3.6 KiB
TypeScript
108 lines
3.6 KiB
TypeScript
import type { ITeamRepository } from '../../domain/repositories/ITeamRepository';
|
|
import type { ITeamMembershipRepository } from '../../domain/repositories/ITeamMembershipRepository';
|
|
import type { ITeamStatsRepository } from '../../domain/repositories/ITeamStatsRepository';
|
|
import type { IMediaRepository } from '../../domain/repositories/IMediaRepository';
|
|
import type { Logger } from '@core/shared/application';
|
|
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 GetAllTeamsInput = {};
|
|
|
|
export type GetAllTeamsErrorCode = 'REPOSITORY_ERROR';
|
|
|
|
export interface TeamSummary {
|
|
id: string;
|
|
name: string;
|
|
tag: string;
|
|
description: string;
|
|
ownerId: string;
|
|
leagues: string[];
|
|
createdAt: Date;
|
|
memberCount: number;
|
|
totalWins?: number;
|
|
totalRaces?: number;
|
|
performanceLevel?: string;
|
|
specialization?: string;
|
|
region?: string;
|
|
languages?: string[];
|
|
logoUrl?: string;
|
|
rating?: number;
|
|
}
|
|
|
|
export interface GetAllTeamsResult {
|
|
teams: TeamSummary[];
|
|
totalCount: number;
|
|
}
|
|
|
|
/**
|
|
* Use Case for retrieving all teams.
|
|
*/
|
|
export class GetAllTeamsUseCase {
|
|
constructor(
|
|
private readonly teamRepository: ITeamRepository,
|
|
private readonly teamMembershipRepository: ITeamMembershipRepository,
|
|
private readonly teamStatsRepository: ITeamStatsRepository,
|
|
private readonly mediaRepository: IMediaRepository,
|
|
private readonly logger: Logger,
|
|
private readonly output: UseCaseOutputPort<GetAllTeamsResult>,
|
|
) {}
|
|
|
|
async execute(
|
|
_input: GetAllTeamsInput = {},
|
|
): Promise<Result<void, ApplicationErrorCode<GetAllTeamsErrorCode, { message: string }>>> {
|
|
void _input;
|
|
this.logger.debug('Executing GetAllTeamsUseCase');
|
|
|
|
try {
|
|
const teams = await this.teamRepository.findAll();
|
|
|
|
const enrichedTeams: TeamSummary[] = await Promise.all(
|
|
teams.map(async (team) => {
|
|
const memberCount = await this.teamMembershipRepository.countByTeamId(team.id);
|
|
const stats = await this.teamStatsRepository.getTeamStats(team.id);
|
|
const logoUrl = await this.mediaRepository.getTeamLogo(team.id);
|
|
|
|
return {
|
|
id: team.id,
|
|
name: team.name.props,
|
|
tag: team.tag.props,
|
|
description: team.description.props,
|
|
ownerId: team.ownerId.toString(),
|
|
leagues: team.leagues.map(l => l.toString()),
|
|
createdAt: team.createdAt.toDate(),
|
|
memberCount,
|
|
// Add stats fields
|
|
...(stats ? {
|
|
totalWins: stats.totalWins,
|
|
totalRaces: stats.totalRaces,
|
|
performanceLevel: stats.performanceLevel,
|
|
specialization: stats.specialization,
|
|
region: stats.region,
|
|
languages: stats.languages,
|
|
logoUrl: logoUrl || stats.logoUrl,
|
|
rating: stats.rating,
|
|
} : {}),
|
|
};
|
|
}),
|
|
);
|
|
|
|
const result: GetAllTeamsResult = {
|
|
teams: enrichedTeams,
|
|
totalCount: enrichedTeams.length,
|
|
};
|
|
|
|
this.logger.debug('Successfully retrieved all teams.');
|
|
this.output.present(result);
|
|
|
|
return Result.ok(undefined);
|
|
} catch (error) {
|
|
this.logger.error('Error retrieving all teams', error instanceof Error ? error : new Error(String(error)));
|
|
return Result.err({
|
|
code: 'REPOSITORY_ERROR',
|
|
details: { message: error instanceof Error ? error.message : 'Failed to load teams' },
|
|
});
|
|
}
|
|
}
|
|
}
|