refactor
This commit is contained in:
@@ -6,21 +6,22 @@ import type {
|
||||
TeamRole,
|
||||
} from '../../domain/types/TeamMembership';
|
||||
import type { JoinTeamCommandDTO } from '../dto/TeamCommandAndQueryDTO';
|
||||
import type { AsyncUseCase } from '@core/shared/application';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
import {
|
||||
BusinessRuleViolationError,
|
||||
EntityNotFoundError,
|
||||
} from '../errors/RacingApplicationError';
|
||||
|
||||
export class JoinTeamUseCase implements AsyncUseCase<JoinTeamCommandDTO, void> {
|
||||
import type { AsyncUseCase, Logger } from '@core/shared/application';
|
||||
import { Result as SharedResult } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
type JoinTeamErrorCode = 'ALREADY_IN_TEAM' | 'ALREADY_MEMBER' | 'TEAM_NOT_FOUND' | 'REPOSITORY_ERROR';
|
||||
|
||||
type JoinTeamApplicationError = ApplicationErrorCode<JoinTeamErrorCode, { message: string }>;
|
||||
|
||||
export class JoinTeamUseCase implements AsyncUseCase<JoinTeamCommandDTO, void, JoinTeamErrorCode> {
|
||||
constructor(
|
||||
private readonly teamRepository: ITeamRepository,
|
||||
private readonly membershipRepository: ITeamMembershipRepository,
|
||||
private readonly logger: Logger,
|
||||
) {}
|
||||
|
||||
async execute(command: JoinTeamCommandDTO): Promise<void> {
|
||||
async execute(command: JoinTeamCommandDTO): Promise<SharedResult<void, JoinTeamApplicationError>> {
|
||||
this.logger.debug('Attempting to join team', { command });
|
||||
const { teamId, driverId } = command;
|
||||
|
||||
@@ -30,19 +31,19 @@ export class JoinTeamUseCase implements AsyncUseCase<JoinTeamCommandDTO, void> {
|
||||
);
|
||||
if (existingActive) {
|
||||
this.logger.warn('Driver already belongs to a team', { driverId, teamId });
|
||||
throw new BusinessRuleViolationError('Driver already belongs to a team');
|
||||
return SharedResult.err({ code: 'ALREADY_IN_TEAM', details: { message: 'Driver already belongs to a team' } });
|
||||
}
|
||||
|
||||
const existingMembership = await this.membershipRepository.getMembership(teamId, driverId);
|
||||
if (existingMembership) {
|
||||
this.logger.warn('Driver already has a pending or active membership request', { driverId, teamId });
|
||||
throw new BusinessRuleViolationError('Already a member or have a pending request');
|
||||
return SharedResult.err({ code: 'ALREADY_MEMBER', details: { message: 'Already a member or have a pending request' } });
|
||||
}
|
||||
|
||||
const team = await this.teamRepository.findById(teamId);
|
||||
if (!team) {
|
||||
this.logger.error('Team not found', { entity: 'team', id: teamId });
|
||||
throw new EntityNotFoundError({ entity: 'team', id: teamId });
|
||||
return SharedResult.err({ code: 'TEAM_NOT_FOUND', details: { message: `Team ${teamId} not found` } });
|
||||
}
|
||||
|
||||
const membership: TeamMembership = {
|
||||
@@ -55,12 +56,10 @@ export class JoinTeamUseCase implements AsyncUseCase<JoinTeamCommandDTO, void> {
|
||||
|
||||
await this.membershipRepository.saveMembership(membership);
|
||||
this.logger.info('Driver successfully joined team', { driverId, teamId });
|
||||
return SharedResult.ok(undefined);
|
||||
} catch (error) {
|
||||
if (error instanceof BusinessRuleViolationError || error instanceof EntityNotFoundError) {
|
||||
throw error;
|
||||
}
|
||||
this.logger.error('Failed to join team due to an unexpected error', { error, command });
|
||||
throw error;
|
||||
this.logger.error('Failed to join team due to an unexpected error', error instanceof Error ? error : new Error('Unknown error'));
|
||||
return SharedResult.err({ code: 'REPOSITORY_ERROR', details: { message: error instanceof Error ? error.message : 'Unknown error' } });
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user