refactor
This commit is contained in:
@@ -1,13 +1,17 @@
|
||||
import type { Logger } from '@core/shared/application';
|
||||
import type {
|
||||
ILeagueMembershipRepository,
|
||||
} from '@core/racing/domain/repositories/ILeagueMembershipRepository';
|
||||
import type { ILeagueMembershipRepository } from '@core/racing/domain/repositories/ILeagueMembershipRepository';
|
||||
import { LeagueMembership, type MembershipRole, type MembershipStatus } from '../../domain/entities/LeagueMembership';
|
||||
import type { AsyncUseCase } from '@core/shared/application';
|
||||
import { Result as SharedResult } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
import type { JoinLeagueCommandDTO } from '../dto/JoinLeagueCommandDTO';
|
||||
import { BusinessRuleViolationError } from '../errors/RacingApplicationError';
|
||||
|
||||
export class JoinLeagueUseCase implements AsyncUseCase<JoinLeagueCommandDTO, LeagueMembership> {
|
||||
|
||||
type JoinLeagueErrorCode = 'ALREADY_MEMBER' | 'REPOSITORY_ERROR';
|
||||
|
||||
type JoinLeagueApplicationError = ApplicationErrorCode<JoinLeagueErrorCode, { message: string }>;
|
||||
|
||||
export class JoinLeagueUseCase implements AsyncUseCase<JoinLeagueCommandDTO, LeagueMembership, JoinLeagueErrorCode> {
|
||||
constructor(
|
||||
private readonly membershipRepository: ILeagueMembershipRepository,
|
||||
private readonly logger: Logger,
|
||||
@@ -17,10 +21,10 @@ export class JoinLeagueUseCase implements AsyncUseCase<JoinLeagueCommandDTO, Lea
|
||||
* Joins a driver to a league as an active member.
|
||||
*
|
||||
* Mirrors the behavior of the legacy joinLeague function:
|
||||
* - Throws when membership already exists for this league/driver.
|
||||
* - Returns error when membership already exists for this league/driver.
|
||||
* - Creates a new active membership with role "member" and current timestamp.
|
||||
*/
|
||||
async execute(command: JoinLeagueCommandDTO): Promise<LeagueMembership> {
|
||||
async execute(command: JoinLeagueCommandDTO): Promise<SharedResult<LeagueMembership, JoinLeagueApplicationError>> {
|
||||
this.logger.debug('Attempting to join league', { command });
|
||||
const { leagueId, driverId } = command;
|
||||
|
||||
@@ -28,7 +32,7 @@ export class JoinLeagueUseCase implements AsyncUseCase<JoinLeagueCommandDTO, Lea
|
||||
const existing = await this.membershipRepository.getMembership(leagueId, driverId);
|
||||
if (existing) {
|
||||
this.logger.warn('Driver already a member or has pending request', { leagueId, driverId });
|
||||
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 membership = LeagueMembership.create({
|
||||
@@ -40,12 +44,10 @@ export class JoinLeagueUseCase implements AsyncUseCase<JoinLeagueCommandDTO, Lea
|
||||
|
||||
const savedMembership = await this.membershipRepository.saveMembership(membership);
|
||||
this.logger.info('Successfully joined league', { membershipId: savedMembership.id });
|
||||
return savedMembership;
|
||||
return SharedResult.ok(savedMembership);
|
||||
} catch (error) {
|
||||
if (!(error instanceof BusinessRuleViolationError)) {
|
||||
this.logger.error('Failed to join league due to an unexpected error', { command, error: error.message });
|
||||
}
|
||||
throw error;
|
||||
this.logger.error('Failed to join league 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