refactor racing use cases
This commit is contained in:
@@ -1,27 +1,50 @@
|
||||
import type { ITeamRepository } from '../../domain/repositories/ITeamRepository';
|
||||
import type { ITeamMembershipRepository } from '../../domain/repositories/ITeamMembershipRepository';
|
||||
import type { GetAllTeamsOutputPort } from '../ports/output/GetAllTeamsOutputPort';
|
||||
import type { AsyncUseCase, Logger } from '@core/shared/application';
|
||||
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;
|
||||
}
|
||||
|
||||
export interface GetAllTeamsResult {
|
||||
teams: TeamSummary[];
|
||||
totalCount: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Use Case for retrieving all teams.
|
||||
*/
|
||||
export class GetAllTeamsUseCase implements AsyncUseCase<void, GetAllTeamsOutputPort, 'REPOSITORY_ERROR'> {
|
||||
export class GetAllTeamsUseCase {
|
||||
constructor(
|
||||
private readonly teamRepository: ITeamRepository,
|
||||
private readonly teamMembershipRepository: ITeamMembershipRepository,
|
||||
private readonly logger: Logger,
|
||||
private readonly output: UseCaseOutputPort<GetAllTeamsResult>,
|
||||
) {}
|
||||
|
||||
async execute(): Promise<Result<AllTeamsResultDTO, ApplicationErrorCode<'REPOSITORY_ERROR', { message: string }>>> {
|
||||
async execute(
|
||||
_input: GetAllTeamsInput = {},
|
||||
): Promise<Result<void, ApplicationErrorCode<GetAllTeamsErrorCode, { message: string }>>> {
|
||||
this.logger.debug('Executing GetAllTeamsUseCase');
|
||||
|
||||
try {
|
||||
const teams = await this.teamRepository.findAll();
|
||||
|
||||
const enrichedTeams: AllTeamsResultDTO['teams'] = await Promise.all(
|
||||
const enrichedTeams: TeamSummary[] = await Promise.all(
|
||||
teams.map(async (team) => {
|
||||
const memberCount = await this.teamMembershipRepository.countByTeamId(team.id);
|
||||
return {
|
||||
@@ -37,19 +60,21 @@ export class GetAllTeamsUseCase implements AsyncUseCase<void, GetAllTeamsOutputP
|
||||
}),
|
||||
);
|
||||
|
||||
const dto: GetAllTeamsOutputPort = {
|
||||
const result: GetAllTeamsResult = {
|
||||
teams: enrichedTeams,
|
||||
totalCount: enrichedTeams.length,
|
||||
};
|
||||
|
||||
this.logger.debug('Successfully retrieved all teams.');
|
||||
return Result.ok(dto);
|
||||
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 : 'Unknown error occurred' },
|
||||
details: { message: error instanceof Error ? error.message : 'Failed to load teams' },
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user