refactor racing use cases
This commit is contained in:
@@ -1,24 +1,37 @@
|
||||
import type { ITeamRepository } from '../../domain/repositories/ITeamRepository';
|
||||
import type { ITeamMembershipRepository } from '../../domain/repositories/ITeamMembershipRepository';
|
||||
import type { DriverTeamOutputPort } from '../ports/output/DriverTeamOutputPort';
|
||||
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';
|
||||
import type { Team } from '../../domain/entities/Team';
|
||||
import type { TeamMembership } from '../../domain/types/TeamMembership';
|
||||
|
||||
export type GetDriverTeamInput = {
|
||||
driverId: string;
|
||||
};
|
||||
|
||||
export type GetDriverTeamResult = {
|
||||
driverId: string;
|
||||
team: Team;
|
||||
membership: TeamMembership;
|
||||
};
|
||||
|
||||
export type GetDriverTeamErrorCode = 'MEMBERSHIP_NOT_FOUND' | 'TEAM_NOT_FOUND' | 'REPOSITORY_ERROR';
|
||||
|
||||
/**
|
||||
* Use Case for retrieving a driver's team.
|
||||
* Orchestrates domain logic and returns result.
|
||||
*/
|
||||
export class GetDriverTeamUseCase
|
||||
implements AsyncUseCase<{ driverId: string }, Result<DriverTeamOutputPort, ApplicationErrorCode<'MEMBERSHIP_NOT_FOUND' | 'TEAM_NOT_FOUND' | 'REPOSITORY_ERROR', { message: string }>>>
|
||||
{
|
||||
export class GetDriverTeamUseCase {
|
||||
constructor(
|
||||
private readonly teamRepository: ITeamRepository,
|
||||
private readonly membershipRepository: ITeamMembershipRepository,
|
||||
private readonly logger: Logger,
|
||||
private readonly output: UseCaseOutputPort<GetDriverTeamResult>,
|
||||
) {}
|
||||
|
||||
async execute(input: { driverId: string }): Promise<Result<DriverTeamOutputPort, ApplicationErrorCode<'MEMBERSHIP_NOT_FOUND' | 'TEAM_NOT_FOUND' | 'REPOSITORY_ERROR', { message: string }>>> {
|
||||
async execute(input: GetDriverTeamInput): Promise<Result<void, ApplicationErrorCode<GetDriverTeamErrorCode, { message: string }>>> {
|
||||
this.logger.debug(`Executing GetDriverTeamUseCase for driverId: ${input.driverId}`);
|
||||
try {
|
||||
const membership = await this.membershipRepository.getActiveMembershipForDriver(input.driverId);
|
||||
@@ -33,24 +46,18 @@ export class GetDriverTeamUseCase
|
||||
this.logger.error(`Team not found for teamId: ${membership.teamId}`);
|
||||
return Result.err({ code: 'TEAM_NOT_FOUND', details: { message: `Team not found for teamId ${membership.teamId}` } });
|
||||
}
|
||||
this.logger.debug(`Found team for teamId: ${team.id}, name: ${team.name}`);
|
||||
this.logger.debug(`Found team for teamId: ${team.id}`);
|
||||
|
||||
const output: DriverTeamOutputPort = {
|
||||
const result: GetDriverTeamResult = {
|
||||
driverId: input.driverId,
|
||||
team: {
|
||||
id: team.id,
|
||||
name: team.name.value,
|
||||
tag: team.tag.value,
|
||||
description: team.description.value,
|
||||
ownerId: team.ownerId.value,
|
||||
leagues: team.leagues.map(l => l.value),
|
||||
createdAt: team.createdAt.value,
|
||||
},
|
||||
team,
|
||||
membership,
|
||||
};
|
||||
|
||||
this.logger.info(`Successfully retrieved driver team for driverId: ${input.driverId}`);
|
||||
return Result.ok(output);
|
||||
this.output.present(result);
|
||||
|
||||
return Result.ok(undefined);
|
||||
} catch (error) {
|
||||
this.logger.error('Error executing GetDriverTeamUseCase', error instanceof Error ? error : new Error(String(error)));
|
||||
return Result.err({ code: 'REPOSITORY_ERROR', details: { message: error instanceof Error ? error.message : 'Unknown error occurred' } });
|
||||
|
||||
Reference in New Issue
Block a user