seed data

This commit is contained in:
2025-12-30 18:33:15 +01:00
parent 83371ea839
commit 92226800df
306 changed files with 1753 additions and 501 deletions

View File

@@ -4,6 +4,7 @@ import type { ITeamRepository } from '../../domain/repositories/ITeamRepository'
import type { ITeamMembershipRepository } from '../../domain/repositories/ITeamMembershipRepository';
import type { ITeamStatsRepository } from '../../domain/repositories/ITeamStatsRepository';
import type { IMediaRepository } from '../../domain/repositories/IMediaRepository';
import type { IResultRepository } from '../../domain/repositories/IResultRepository';
import type { Logger } from '@core/shared/application';
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
@@ -34,12 +35,26 @@ describe('GetAllTeamsUseCase', () => {
const mockTeamStatsRepo: ITeamStatsRepository = {
getTeamStats: vi.fn(),
getTeamStatsSync: vi.fn(),
saveTeamStats: vi.fn(),
getAllStats: vi.fn(),
clear: vi.fn(),
};
const mockResultRepo: IResultRepository = {
findAll: vi.fn(),
findById: vi.fn(),
findByRaceId: vi.fn(),
findByDriverId: vi.fn(),
findByDriverIdAndLeagueId: vi.fn(),
create: vi.fn(),
createMany: vi.fn(),
update: vi.fn(),
delete: vi.fn(),
deleteByRaceId: vi.fn(),
exists: vi.fn(),
existsByRaceId: vi.fn(),
};
const mockMediaRepo: IMediaRepository = {
getDriverAvatar: vi.fn(),
getTeamLogo: vi.fn(),
@@ -71,6 +86,7 @@ describe('GetAllTeamsUseCase', () => {
mockTeamMembershipRepo,
mockTeamStatsRepo,
mockMediaRepo,
mockResultRepo,
mockLogger,
output,
);
@@ -138,6 +154,7 @@ describe('GetAllTeamsUseCase', () => {
mockTeamMembershipRepo,
mockTeamStatsRepo,
mockMediaRepo,
mockResultRepo,
mockLogger,
output,
);
@@ -164,6 +181,7 @@ describe('GetAllTeamsUseCase', () => {
mockTeamMembershipRepo,
mockTeamStatsRepo,
mockMediaRepo,
mockResultRepo,
mockLogger,
output,
);

View File

@@ -2,6 +2,7 @@ import type { ITeamRepository } from '../../domain/repositories/ITeamRepository'
import type { ITeamMembershipRepository } from '../../domain/repositories/ITeamMembershipRepository';
import type { ITeamStatsRepository } from '../../domain/repositories/ITeamStatsRepository';
import type { IMediaRepository } from '../../domain/repositories/IMediaRepository';
import type { IResultRepository } from '../../domain/repositories/IResultRepository';
import type { Logger } from '@core/shared/application';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
@@ -28,6 +29,8 @@ export interface TeamSummary {
languages?: string[];
logoUrl?: string;
rating?: number;
category?: string | undefined;
isRecruiting: boolean;
}
export interface GetAllTeamsResult {
@@ -44,6 +47,7 @@ export class GetAllTeamsUseCase {
private readonly teamMembershipRepository: ITeamMembershipRepository,
private readonly teamStatsRepository: ITeamStatsRepository,
private readonly mediaRepository: IMediaRepository,
private readonly resultRepository: IResultRepository,
private readonly logger: Logger,
private readonly output: UseCaseOutputPort<GetAllTeamsResult>,
) {}
@@ -60,9 +64,48 @@ export class GetAllTeamsUseCase {
const enrichedTeams: TeamSummary[] = await Promise.all(
teams.map(async (team) => {
const memberCount = await this.teamMembershipRepository.countByTeamId(team.id);
const stats = await this.teamStatsRepository.getTeamStats(team.id);
const logoUrl = await this.mediaRepository.getTeamLogo(team.id);
// Try to get pre-computed stats first
let stats = await this.teamStatsRepository.getTeamStats(team.id);
// If no pre-computed stats, compute them on-the-fly from results
if (!stats) {
this.logger.debug(`Computing stats for team ${team.id} on-the-fly`);
const teamMemberships = await this.teamMembershipRepository.getTeamMembers(team.id);
const teamMemberIds = teamMemberships.map(m => m.driverId.toString());
const allResults = await this.resultRepository.findAll();
const teamResults = allResults.filter(r => teamMemberIds.includes(r.driverId.toString()));
const wins = teamResults.filter(r => r.position.toNumber() === 1).length;
const totalRaces = teamResults.length;
// Calculate rating
const baseRating = 1000;
const winBonus = wins * 50;
const raceBonus = Math.min(totalRaces * 5, 200);
const rating = Math.round(baseRating + winBonus + raceBonus);
// Determine performance level
let performanceLevel: 'beginner' | 'intermediate' | 'advanced' | 'pro';
if (wins >= 20) performanceLevel = 'pro';
else if (wins >= 10) performanceLevel = 'advanced';
else if (wins >= 5) performanceLevel = 'intermediate';
else performanceLevel = 'beginner';
stats = {
logoUrl: await this.mediaRepository.getTeamLogo(team.id) || '',
performanceLevel,
specialization: 'mixed',
region: 'International',
languages: ['en'],
totalWins: wins,
totalRaces,
rating,
};
}
return {
id: team.id,
name: team.name.props,
@@ -72,17 +115,16 @@ export class GetAllTeamsUseCase {
leagues: team.leagues.map(l => l.toString()),
createdAt: team.createdAt.toDate(),
memberCount,
// Add stats fields
...(stats ? {
totalWins: stats.totalWins,
totalRaces: stats.totalRaces,
performanceLevel: stats.performanceLevel,
specialization: stats.specialization,
region: stats.region,
languages: stats.languages,
logoUrl: logoUrl || stats.logoUrl,
rating: stats.rating,
} : {}),
totalWins: stats!.totalWins,
totalRaces: stats!.totalRaces,
performanceLevel: stats!.performanceLevel,
specialization: stats!.specialization,
region: stats!.region,
languages: stats!.languages,
logoUrl: logoUrl || stats!.logoUrl,
rating: stats!.rating,
category: team.category,
isRecruiting: team.isRecruiting,
};
}),
);
@@ -104,4 +146,4 @@ export class GetAllTeamsUseCase {
});
}
}
}
}

View File

@@ -20,6 +20,7 @@ export class Driver implements IEntity<string> {
readonly country: CountryCode;
readonly bio: DriverBio | undefined;
readonly joinedAt: JoinedAt;
readonly category: string | undefined;
private constructor(props: {
id: string;
@@ -28,6 +29,7 @@ export class Driver implements IEntity<string> {
country: CountryCode;
bio?: DriverBio;
joinedAt: JoinedAt;
category?: string;
}) {
this.id = props.id;
this.iracingId = props.iracingId;
@@ -35,6 +37,7 @@ export class Driver implements IEntity<string> {
this.country = props.country;
this.bio = props.bio;
this.joinedAt = props.joinedAt;
this.category = props.category;
}
/**
@@ -47,19 +50,36 @@ export class Driver implements IEntity<string> {
country: string;
bio?: string;
joinedAt?: Date;
category?: string;
}): Driver {
if (!props.id || props.id.trim().length === 0) {
throw new RacingDomainValidationError('Driver ID is required');
}
return new Driver({
const driverProps: {
id: string;
iracingId: IRacingId;
name: DriverName;
country: CountryCode;
bio?: DriverBio;
joinedAt: JoinedAt;
category?: string;
} = {
id: props.id,
iracingId: IRacingId.create(props.iracingId),
name: DriverName.create(props.name),
country: CountryCode.create(props.country),
...(props.bio !== undefined ? { bio: DriverBio.create(props.bio) } : {}),
joinedAt: JoinedAt.create(props.joinedAt ?? new Date()),
});
};
if (props.bio !== undefined) {
driverProps.bio = DriverBio.create(props.bio);
}
if (props.category !== undefined) {
driverProps.category = props.category;
}
return new Driver(driverProps);
}
static rehydrate(props: {
@@ -69,15 +89,32 @@ export class Driver implements IEntity<string> {
country: string;
bio?: string;
joinedAt: Date;
category?: string;
}): Driver {
return new Driver({
const driverProps: {
id: string;
iracingId: IRacingId;
name: DriverName;
country: CountryCode;
bio?: DriverBio;
joinedAt: JoinedAt;
category?: string;
} = {
id: props.id,
iracingId: IRacingId.create(props.iracingId),
name: DriverName.create(props.name),
country: CountryCode.create(props.country),
...(props.bio !== undefined ? { bio: DriverBio.create(props.bio) } : {}),
joinedAt: JoinedAt.create(props.joinedAt),
});
};
if (props.bio !== undefined) {
driverProps.bio = DriverBio.create(props.bio);
}
if (props.category !== undefined) {
driverProps.category = props.category;
}
return new Driver(driverProps);
}
/**
@@ -87,18 +124,36 @@ export class Driver implements IEntity<string> {
name: string;
country: string;
bio: string | undefined;
category: string | undefined;
}>): Driver {
const nextName = 'name' in props ? DriverName.create(props.name!) : this.name;
const nextCountry = 'country' in props ? CountryCode.create(props.country!) : this.country;
const nextBio = 'bio' in props ? (props.bio ? DriverBio.create(props.bio) : undefined) : this.bio;
const nextCategory = 'category' in props ? props.category : this.category;
return new Driver({
const driverProps: {
id: string;
iracingId: IRacingId;
name: DriverName;
country: CountryCode;
bio?: DriverBio;
joinedAt: JoinedAt;
category?: string;
} = {
id: this.id,
iracingId: this.iracingId,
name: nextName,
country: nextCountry,
...(nextBio !== undefined ? { bio: nextBio } : {}),
joinedAt: this.joinedAt,
});
};
if (nextBio !== undefined) {
driverProps.bio = nextBio;
}
if (nextCategory !== undefined) {
driverProps.category = nextCategory;
}
return new Driver(driverProps);
}
}

View File

@@ -96,6 +96,7 @@ export class League implements IEntity<LeagueId> {
readonly description: LeagueDescription;
readonly ownerId: LeagueOwnerId;
readonly settings: LeagueSettings;
readonly category?: string | undefined;
readonly createdAt: LeagueCreatedAt;
readonly socialLinks: LeagueSocialLinks | undefined;
@@ -109,6 +110,7 @@ export class League implements IEntity<LeagueId> {
description: LeagueDescription;
ownerId: LeagueOwnerId;
settings: LeagueSettings;
category?: string | undefined;
createdAt: LeagueCreatedAt;
socialLinks?: LeagueSocialLinks;
participantCount: ParticipantCount;
@@ -119,6 +121,7 @@ export class League implements IEntity<LeagueId> {
this.description = props.description;
this.ownerId = props.ownerId;
this.settings = props.settings;
this.category = props.category;
this.createdAt = props.createdAt;
this.socialLinks = props.socialLinks;
this._participantCount = props.participantCount;
@@ -135,6 +138,7 @@ export class League implements IEntity<LeagueId> {
description: string;
ownerId: string;
settings?: Partial<LeagueSettings>;
category?: string | undefined;
createdAt?: Date;
socialLinks?: {
discordUrl?: string;
@@ -245,6 +249,7 @@ export class League implements IEntity<LeagueId> {
description,
ownerId,
settings: finalSettings,
category: props.category,
createdAt,
...(socialLinks !== undefined ? { socialLinks } : {}),
participantCount,
@@ -258,6 +263,7 @@ export class League implements IEntity<LeagueId> {
description: string;
ownerId: string;
settings: LeagueSettings;
category?: string | undefined;
createdAt: Date;
participantCount: number;
socialLinks?: {
@@ -286,6 +292,7 @@ export class League implements IEntity<LeagueId> {
description,
ownerId,
settings: props.settings,
category: props.category,
createdAt,
...(socialLinks !== undefined ? { socialLinks } : {}),
participantCount,
@@ -343,6 +350,7 @@ export class League implements IEntity<LeagueId> {
description: string;
ownerId: string;
settings: LeagueSettings;
category?: string | undefined;
socialLinks?: {
discordUrl?: string;
youtubeUrl?: string;
@@ -414,6 +422,7 @@ export class League implements IEntity<LeagueId> {
description,
ownerId,
settings: newSettings,
category: props.category ?? this.category,
createdAt: this.createdAt,
...(socialLinks !== undefined ? { socialLinks } : {}),
participantCount: this._participantCount,
@@ -447,6 +456,7 @@ export class League implements IEntity<LeagueId> {
description: this.description,
ownerId: this.ownerId,
settings: this.settings,
category: this.category,
createdAt: this.createdAt,
...(this.socialLinks !== undefined ? { socialLinks: this.socialLinks } : {}),
participantCount: newCount,
@@ -470,6 +480,7 @@ export class League implements IEntity<LeagueId> {
description: this.description,
ownerId: this.ownerId,
settings: this.settings,
category: this.category,
createdAt: this.createdAt,
...(this.socialLinks !== undefined ? { socialLinks: this.socialLinks } : {}),
participantCount: newCount,

View File

@@ -22,6 +22,8 @@ export class Team implements IEntity<string> {
readonly description: TeamDescription;
readonly ownerId: DriverId;
readonly leagues: LeagueId[];
readonly category: string | undefined;
readonly isRecruiting: boolean;
readonly createdAt: TeamCreatedAt;
private constructor(props: {
@@ -31,6 +33,8 @@ export class Team implements IEntity<string> {
description: TeamDescription;
ownerId: DriverId;
leagues: LeagueId[];
category: string | undefined;
isRecruiting: boolean;
createdAt: TeamCreatedAt;
}) {
this.id = props.id;
@@ -39,6 +43,8 @@ export class Team implements IEntity<string> {
this.description = props.description;
this.ownerId = props.ownerId;
this.leagues = props.leagues;
this.category = props.category;
this.isRecruiting = props.isRecruiting;
this.createdAt = props.createdAt;
}
@@ -52,6 +58,8 @@ export class Team implements IEntity<string> {
description: string;
ownerId: string;
leagues: string[];
category?: string;
isRecruiting?: boolean;
createdAt?: Date;
}): Team {
if (!props.id || props.id.trim().length === 0) {
@@ -69,6 +77,8 @@ export class Team implements IEntity<string> {
description: TeamDescription.create(props.description),
ownerId: DriverId.create(props.ownerId),
leagues: props.leagues.map(leagueId => LeagueId.create(leagueId)),
category: props.category,
isRecruiting: props.isRecruiting ?? false,
createdAt: TeamCreatedAt.create(props.createdAt ?? new Date()),
});
}
@@ -80,6 +90,8 @@ export class Team implements IEntity<string> {
description: string;
ownerId: string;
leagues: string[];
category?: string;
isRecruiting: boolean;
createdAt: Date;
}): Team {
if (!props.id || props.id.trim().length === 0) {
@@ -97,6 +109,8 @@ export class Team implements IEntity<string> {
description: TeamDescription.create(props.description),
ownerId: DriverId.create(props.ownerId),
leagues: props.leagues.map(leagueId => LeagueId.create(leagueId)),
category: props.category,
isRecruiting: props.isRecruiting,
createdAt: TeamCreatedAt.create(props.createdAt),
});
}
@@ -110,12 +124,16 @@ export class Team implements IEntity<string> {
description: string;
ownerId: string;
leagues: string[];
category: string | undefined;
isRecruiting: boolean;
}>): Team {
const nextName = 'name' in props ? TeamName.create(props.name!) : this.name;
const nextTag = 'tag' in props ? TeamTag.create(props.tag!) : this.tag;
const nextDescription = 'description' in props ? TeamDescription.create(props.description!) : this.description;
const nextOwnerId = 'ownerId' in props ? DriverId.create(props.ownerId!) : this.ownerId;
const nextLeagues = 'leagues' in props ? props.leagues!.map(leagueId => LeagueId.create(leagueId)) : this.leagues;
const nextCategory = 'category' in props ? props.category : this.category;
const nextIsRecruiting = 'isRecruiting' in props ? props.isRecruiting! : this.isRecruiting;
return new Team({
id: this.id,
@@ -124,6 +142,8 @@ export class Team implements IEntity<string> {
description: nextDescription,
ownerId: nextOwnerId,
leagues: nextLeagues,
category: nextCategory,
isRecruiting: nextIsRecruiting,
createdAt: this.createdAt,
});
}

View File

@@ -14,6 +14,7 @@ export interface TeamStats {
totalWins: number;
totalRaces: number;
rating: number;
category?: string;
}
export interface ITeamStatsRepository {
@@ -22,11 +23,6 @@ export interface ITeamStatsRepository {
*/
getTeamStats(teamId: string): Promise<TeamStats | null>;
/**
* Get stats for a specific team (synchronous)
*/
getTeamStatsSync(teamId: string): TeamStats | null;
/**
* Save stats for a specific team
*/
@@ -41,4 +37,4 @@ export interface ITeamStatsRepository {
* Clear all stats
*/
clear(): Promise<void>;
}
}