refactor
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type {
|
||||
ILeagueMembershipRepository,
|
||||
} from '@core/racing/domain/repositories/ILeagueMembershipRepository';
|
||||
import type { ILeagueRepository } from '@core/racing/domain/repositories/ILeagueRepository';
|
||||
import type {
|
||||
LeagueMembership,
|
||||
MembershipRole,
|
||||
} from '@core/racing/domain/entities/LeagueMembership';
|
||||
|
||||
@@ -13,31 +14,33 @@ export interface TransferLeagueOwnershipCommandDTO {
|
||||
newOwnerId: string;
|
||||
}
|
||||
|
||||
type TransferLeagueOwnershipErrorCode = 'LEAGUE_NOT_FOUND' | 'NOT_CURRENT_OWNER' | 'NEW_OWNER_NOT_ACTIVE_MEMBER';
|
||||
|
||||
export class TransferLeagueOwnershipUseCase {
|
||||
constructor(
|
||||
private readonly leagueRepository: ILeagueRepository,
|
||||
private readonly membershipRepository: ILeagueMembershipRepository
|
||||
) {}
|
||||
|
||||
async execute(command: TransferLeagueOwnershipCommandDTO): Promise<void> {
|
||||
async execute(command: TransferLeagueOwnershipCommandDTO): Promise<Result<void, ApplicationErrorCode<TransferLeagueOwnershipErrorCode>>> {
|
||||
const { leagueId, currentOwnerId, newOwnerId } = command;
|
||||
|
||||
const league = await this.leagueRepository.findById(leagueId);
|
||||
if (!league) {
|
||||
throw new Error('League not found');
|
||||
return Result.err({ code: 'LEAGUE_NOT_FOUND' });
|
||||
}
|
||||
|
||||
if (league.ownerId !== currentOwnerId) {
|
||||
throw new Error('Only the current owner can transfer ownership');
|
||||
return Result.err({ code: 'NOT_CURRENT_OWNER' });
|
||||
}
|
||||
|
||||
const newOwnerMembership = await this.membershipRepository.getMembership(leagueId, newOwnerId);
|
||||
if (!newOwnerMembership || newOwnerMembership.status !== 'active') {
|
||||
throw new Error('New owner must be an active member of the league');
|
||||
return Result.err({ code: 'NEW_OWNER_NOT_ACTIVE_MEMBER' });
|
||||
}
|
||||
|
||||
const currentOwnerMembership = await this.membershipRepository.getMembership(leagueId, currentOwnerId);
|
||||
|
||||
|
||||
await this.membershipRepository.saveMembership({
|
||||
...newOwnerMembership,
|
||||
role: 'owner' as MembershipRole,
|
||||
@@ -52,5 +55,7 @@ export class TransferLeagueOwnershipUseCase {
|
||||
|
||||
const updatedLeague = league.update({ ownerId: newOwnerId });
|
||||
await this.leagueRepository.update(updatedLeague);
|
||||
|
||||
return Result.ok(undefined);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user