refactor racing use cases
This commit is contained in:
@@ -12,12 +12,12 @@ import type {
|
||||
TeamMembershipStatus,
|
||||
TeamRole,
|
||||
} from '../../domain/types/TeamMembership';
|
||||
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 { CreateTeamOutputPort } from '../ports/output/CreateTeamOutputPort';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
|
||||
export interface CreateTeamCommandDTO {
|
||||
export interface CreateTeamInput {
|
||||
name: string;
|
||||
tag: string;
|
||||
description: string;
|
||||
@@ -25,27 +25,42 @@ export interface CreateTeamCommandDTO {
|
||||
leagues: string[];
|
||||
}
|
||||
|
||||
export class CreateTeamUseCase
|
||||
implements AsyncUseCase<CreateTeamCommandDTO, CreateTeamOutputPort, 'ALREADY_IN_TEAM' | 'REPOSITORY_ERROR'>
|
||||
{
|
||||
export interface CreateTeamResult {
|
||||
team: Team;
|
||||
}
|
||||
|
||||
export type CreateTeamErrorCode =
|
||||
| 'VALIDATION_ERROR'
|
||||
| 'LEAGUE_NOT_FOUND'
|
||||
| 'REPOSITORY_ERROR';
|
||||
|
||||
export class CreateTeamUseCase {
|
||||
constructor(
|
||||
private readonly teamRepository: ITeamRepository,
|
||||
private readonly membershipRepository: ITeamMembershipRepository,
|
||||
private readonly logger: Logger,
|
||||
private readonly output: UseCaseOutputPort<CreateTeamResult>,
|
||||
) {}
|
||||
|
||||
async execute(
|
||||
command: CreateTeamCommandDTO,
|
||||
): Promise<Result<CreateTeamOutputPort, ApplicationErrorCode<'ALREADY_IN_TEAM' | 'REPOSITORY_ERROR', { message: string }>>> {
|
||||
this.logger.debug('Executing CreateTeamUseCase', { command });
|
||||
const { name, tag, description, ownerId, leagues } = command;
|
||||
input: CreateTeamInput,
|
||||
): Promise<
|
||||
Result<void, ApplicationErrorCode<CreateTeamErrorCode, { message: string }>>
|
||||
> {
|
||||
this.logger.debug('Executing CreateTeamUseCase', { input });
|
||||
const { name, tag, description, ownerId, leagues } = input;
|
||||
|
||||
const existingMembership = await this.membershipRepository.getActiveMembershipForDriver(
|
||||
ownerId,
|
||||
);
|
||||
const existingMembership =
|
||||
await this.membershipRepository.getActiveMembershipForDriver(ownerId);
|
||||
if (existingMembership) {
|
||||
this.logger.warn('Validation failed: Driver already belongs to a team', { ownerId });
|
||||
return Result.err({ code: 'ALREADY_IN_TEAM', details: { message: 'Driver already belongs to a team' } });
|
||||
this.logger.warn(
|
||||
'Validation failed: Driver already belongs to a team',
|
||||
{ ownerId },
|
||||
);
|
||||
return Result.err({
|
||||
code: 'VALIDATION_ERROR',
|
||||
details: { message: 'Driver already belongs to a team' },
|
||||
});
|
||||
}
|
||||
|
||||
this.logger.info('Command validated successfully.');
|
||||
@@ -63,7 +78,9 @@ export class CreateTeamUseCase
|
||||
});
|
||||
|
||||
const createdTeam = await this.teamRepository.create(team);
|
||||
this.logger.info(`Team ${createdTeam.name} (${createdTeam.id}) created successfully.`);
|
||||
this.logger.info(
|
||||
`Team ${createdTeam.name} (${createdTeam.id}) created successfully.`,
|
||||
);
|
||||
|
||||
const membership: TeamMembership = {
|
||||
teamId: createdTeam.id,
|
||||
@@ -76,11 +93,17 @@ export class CreateTeamUseCase
|
||||
await this.membershipRepository.saveMembership(membership);
|
||||
this.logger.debug('Team membership created successfully.');
|
||||
|
||||
const result: CreateTeamOutputPort = { team: createdTeam };
|
||||
const result: CreateTeamResult = { team: createdTeam };
|
||||
this.logger.debug('CreateTeamUseCase completed successfully.', { result });
|
||||
return Result.ok(result);
|
||||
this.output.present(result);
|
||||
return Result.ok(undefined);
|
||||
} catch (error) {
|
||||
return Result.err({ code: 'REPOSITORY_ERROR', details: { message: error instanceof Error ? error.message : 'Unknown error' } });
|
||||
return Result.err({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: {
|
||||
message: error instanceof Error ? error.message : 'Unknown error',
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user