refactor use cases
This commit is contained in:
@@ -1,65 +1,120 @@
|
||||
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
|
||||
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application';
|
||||
import type { Driver } from '../../domain/entities/Driver';
|
||||
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
|
||||
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
|
||||
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
|
||||
import type { IStandingRepository } from '../../domain/repositories/IStandingRepository';
|
||||
import type { League } from '../../domain/entities/League';
|
||||
import type { Driver } from '../../domain/entities/Driver';
|
||||
|
||||
export type GetLeagueOwnerSummaryInput = {
|
||||
export interface GetLeagueOwnerSummaryInput {
|
||||
leagueId: string;
|
||||
};
|
||||
}
|
||||
|
||||
export type GetLeagueOwnerSummaryResult = {
|
||||
export type GetLeagueOwnerSummaryErrorCode = 'LEAGUE_NOT_FOUND' | 'OWNER_NOT_FOUND' | 'REPOSITORY_ERROR';
|
||||
|
||||
export interface GetLeagueOwnerSummaryResult {
|
||||
league: League;
|
||||
owner: Driver;
|
||||
rating: number;
|
||||
rank: number;
|
||||
};
|
||||
|
||||
export type GetLeagueOwnerSummaryErrorCode =
|
||||
| 'LEAGUE_NOT_FOUND'
|
||||
| 'OWNER_NOT_FOUND'
|
||||
| 'REPOSITORY_ERROR';
|
||||
totalMembers: number;
|
||||
activeMembers: number;
|
||||
rating: number | null;
|
||||
rank: number | null;
|
||||
}
|
||||
|
||||
export class GetLeagueOwnerSummaryUseCase {
|
||||
constructor(
|
||||
private readonly leagueRepository: ILeagueRepository,
|
||||
private readonly driverRepository: IDriverRepository,
|
||||
private readonly output: UseCaseOutputPort<GetLeagueOwnerSummaryResult>,
|
||||
private readonly leagueMembershipRepository: ILeagueMembershipRepository,
|
||||
private readonly standingRepository: IStandingRepository,
|
||||
) {}
|
||||
|
||||
async execute(
|
||||
input: GetLeagueOwnerSummaryInput,
|
||||
): Promise<Result<void, ApplicationErrorCode<GetLeagueOwnerSummaryErrorCode, { message: string }>>> {
|
||||
): Promise<Result<GetLeagueOwnerSummaryResult, ApplicationErrorCode<GetLeagueOwnerSummaryErrorCode, { message: string }>>> {
|
||||
try {
|
||||
const league = await this.leagueRepository.findById(input.leagueId);
|
||||
|
||||
if (!league) {
|
||||
return Result.err({ code: 'LEAGUE_NOT_FOUND', details: { message: 'League not found' } });
|
||||
return Result.err({
|
||||
code: 'LEAGUE_NOT_FOUND',
|
||||
details: { message: 'League not found' },
|
||||
});
|
||||
}
|
||||
|
||||
const ownerId = league.ownerId.toString();
|
||||
const owner = await this.driverRepository.findById(ownerId);
|
||||
const owner = await this.driverRepository.findById(league.ownerId.toString());
|
||||
|
||||
if (!owner) {
|
||||
return Result.err({ code: 'OWNER_NOT_FOUND', details: { message: 'League owner not found' } });
|
||||
return Result.err({
|
||||
code: 'OWNER_NOT_FOUND',
|
||||
details: { message: 'League owner not found' },
|
||||
});
|
||||
}
|
||||
|
||||
this.output.present({
|
||||
const members = await this.leagueMembershipRepository.getLeagueMembers(input.leagueId);
|
||||
const totalMembers = members.length;
|
||||
const activeMembers = members.filter(m => m.status.toString() === 'active').length;
|
||||
|
||||
// Calculate rating and rank for the owner
|
||||
let rating: number | null = null;
|
||||
let rank: number | null = null;
|
||||
|
||||
try {
|
||||
// Get standing for the owner in this league
|
||||
const ownerStanding = await this.standingRepository.findByDriverIdAndLeagueId(owner.id.toString(), league.id.toString());
|
||||
|
||||
if (ownerStanding) {
|
||||
// Calculate rating from standing
|
||||
const baseRating = 1000;
|
||||
const pointsBonus = ownerStanding.points.toNumber() * 2;
|
||||
const positionBonus = Math.max(0, 50 - (ownerStanding.position.toNumber() * 2));
|
||||
const winBonus = ownerStanding.wins * 100;
|
||||
|
||||
rating = Math.round(baseRating + pointsBonus + positionBonus + winBonus);
|
||||
|
||||
// Calculate rank among all drivers in this league
|
||||
const leagueStandings = await this.standingRepository.findByLeagueId(league.id.toString());
|
||||
const driverStats = new Map<string, { rating: number }>();
|
||||
|
||||
for (const standing of leagueStandings) {
|
||||
const driverId = standing.driverId.toString();
|
||||
const standingBaseRating = 1000;
|
||||
const standingPointsBonus = standing.points.toNumber() * 2;
|
||||
const standingPositionBonus = Math.max(0, 50 - (standing.position.toNumber() * 2));
|
||||
const standingWinBonus = standing.wins * 100;
|
||||
|
||||
const standingRating = Math.round(standingBaseRating + standingPointsBonus + standingPositionBonus + standingWinBonus);
|
||||
driverStats.set(driverId, { rating: standingRating });
|
||||
}
|
||||
|
||||
const rankings = Array.from(driverStats.entries())
|
||||
.sort(([, a], [, b]) => b.rating - a.rating)
|
||||
.map(([driverId], index) => ({ driverId, rank: index + 1 }));
|
||||
|
||||
const ownerRanking = rankings.find(r => r.driverId === owner.id.toString());
|
||||
rank = ownerRanking ? ownerRanking.rank : null;
|
||||
}
|
||||
} catch (error) {
|
||||
// If rating calculation fails, continue with null values
|
||||
console.error('Failed to calculate rating/rank:', error);
|
||||
}
|
||||
|
||||
return Result.ok({
|
||||
league,
|
||||
owner,
|
||||
rating: 0,
|
||||
rank: 0,
|
||||
totalMembers,
|
||||
activeMembers,
|
||||
rating,
|
||||
rank,
|
||||
});
|
||||
} catch (error: unknown) {
|
||||
const message = error instanceof Error ? error.message : 'Failed to get league owner summary';
|
||||
|
||||
return Result.ok(undefined);
|
||||
} catch (error) {
|
||||
const message =
|
||||
error instanceof Error && error.message
|
||||
? error.message
|
||||
: 'Failed to fetch league owner summary';
|
||||
|
||||
return Result.err({ code: 'REPOSITORY_ERROR', details: { message } });
|
||||
return Result.err({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: { message },
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user