refactor core presenters
This commit is contained in:
@@ -4,14 +4,13 @@ import { LeagueMembership, type MembershipRole, type MembershipStatus } from '..
|
||||
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 { JoinLeagueOutputPort } from '../ports/output/JoinLeagueOutputPort';
|
||||
|
||||
import type { JoinLeagueCommandDTO } from '../dto/JoinLeagueCommandDTO';
|
||||
|
||||
type JoinLeagueErrorCode = 'ALREADY_MEMBER' | 'REPOSITORY_ERROR';
|
||||
|
||||
type JoinLeagueApplicationError = ApplicationErrorCode<JoinLeagueErrorCode, { message: string }>;
|
||||
|
||||
export class JoinLeagueUseCase implements AsyncUseCase<JoinLeagueCommandDTO, LeagueMembership, JoinLeagueErrorCode> {
|
||||
export class JoinLeagueUseCase implements AsyncUseCase<JoinLeagueCommandDTO, JoinLeagueOutputPort, JoinLeagueErrorCode> {
|
||||
constructor(
|
||||
private readonly membershipRepository: ILeagueMembershipRepository,
|
||||
private readonly logger: Logger,
|
||||
@@ -24,7 +23,7 @@ export class JoinLeagueUseCase implements AsyncUseCase<JoinLeagueCommandDTO, Lea
|
||||
* - 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<SharedResult<LeagueMembership, JoinLeagueApplicationError>> {
|
||||
async execute(command: JoinLeagueCommandDTO): Promise<SharedResult<JoinLeagueOutputPort, ApplicationErrorCode<JoinLeagueErrorCode>>> {
|
||||
this.logger.debug('Attempting to join league', { command });
|
||||
const { leagueId, driverId } = command;
|
||||
|
||||
@@ -32,7 +31,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 });
|
||||
return SharedResult.err({ code: 'ALREADY_MEMBER', details: { message: 'Already a member or have a pending request' } });
|
||||
return SharedResult.err({ code: 'ALREADY_MEMBER' });
|
||||
}
|
||||
|
||||
const membership = LeagueMembership.create({
|
||||
@@ -44,10 +43,14 @@ export class JoinLeagueUseCase implements AsyncUseCase<JoinLeagueCommandDTO, Lea
|
||||
|
||||
const savedMembership = await this.membershipRepository.saveMembership(membership);
|
||||
this.logger.info('Successfully joined league', { membershipId: savedMembership.id });
|
||||
return SharedResult.ok(savedMembership);
|
||||
return SharedResult.ok({
|
||||
membershipId: savedMembership.id,
|
||||
leagueId: savedMembership.leagueId.toString(),
|
||||
status: savedMembership.status.toString(),
|
||||
});
|
||||
} catch (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' } });
|
||||
return SharedResult.err({ code: 'REPOSITORY_ERROR' });
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user