more seeds

This commit is contained in:
2025-12-27 11:58:35 +01:00
parent 91612e4256
commit 3efa978ee0
25 changed files with 806 additions and 55 deletions

View File

@@ -10,10 +10,13 @@ import type { ITeamRepository } from '@core/racing/domain/repositories/ITeamRepo
import type { ITeamMembershipRepository } from '@core/racing/domain/repositories/ITeamMembershipRepository';
import type { ISponsorRepository } from '@core/racing/domain/repositories/ISponsorRepository';
import type { ISeasonRepository } from '@core/racing/domain/repositories/ISeasonRepository';
import type { ILeagueScoringConfigRepository } from '@core/racing/domain/repositories/ILeagueScoringConfigRepository';
import type { ISeasonSponsorshipRepository } from '@core/racing/domain/repositories/ISeasonSponsorshipRepository';
import type { ISponsorshipRequestRepository } from '@core/racing/domain/repositories/ISponsorshipRequestRepository';
import type { ILeagueWalletRepository } from '@core/racing/domain/repositories/ILeagueWalletRepository';
import type { ITransactionRepository } from '@core/racing/domain/repositories/ITransactionRepository';
import type { Season } from '@core/racing/domain/entities/season/Season';
import { getLeagueScoringPresetById } from './LeagueScoringPresets';
import type { IProtestRepository } from '@core/racing/domain/repositories/IProtestRepository';
import type { IPenaltyRepository } from '@core/racing/domain/repositories/IPenaltyRepository';
import type { IFeedRepository } from '@core/social/domain/repositories/IFeedRepository';
@@ -24,6 +27,7 @@ export type RacingSeedDependencies = {
driverRepository: IDriverRepository;
leagueRepository: ILeagueRepository;
seasonRepository: ISeasonRepository;
leagueScoringConfigRepository: ILeagueScoringConfigRepository;
seasonSponsorshipRepository: ISeasonSponsorshipRepository;
sponsorshipRequestRepository: ISponsorshipRequestRepository;
leagueWalletRepository: ILeagueWalletRepository;
@@ -51,7 +55,8 @@ export class SeedRacingData {
async execute(): Promise<void> {
const existingDrivers = await this.seedDeps.driverRepository.findAll();
if (existingDrivers.length > 0) {
this.logger.info('[Bootstrap] Racing seed skipped (drivers already exist)');
this.logger.info('[Bootstrap] Racing seed skipped (drivers already exist), ensuring scoring configs');
await this.ensureScoringConfigsForExistingData();
return;
}
@@ -90,6 +95,26 @@ export class SeedRacingData {
}
}
const activeSeasons = seed.seasons.filter((season) => season.status === 'active');
for (const season of activeSeasons) {
const presetId = this.selectScoringPresetIdForSeason(season);
const preset = getLeagueScoringPresetById(presetId);
if (!preset) {
this.logger.warn(
`[Bootstrap] Scoring preset not found (presetId=${presetId}, seasonId=${season.id}, leagueId=${season.leagueId})`,
);
continue;
}
const scoringConfig = preset.createConfig({ seasonId: season.id });
try {
await this.seedDeps.leagueScoringConfigRepository.save(scoringConfig);
} catch {
// ignore duplicates
}
}
for (const sponsorship of seed.seasonSponsorships) {
try {
@@ -239,4 +264,64 @@ export class SeedRacingData {
`[Bootstrap] Seeded racing data: drivers=${seed.drivers.length}, leagues=${seed.leagues.length}, races=${seed.races.length}`,
);
}
private async ensureScoringConfigsForExistingData(): Promise<void> {
const leagues = await this.seedDeps.leagueRepository.findAll();
for (const league of leagues) {
const seasons = await this.seedDeps.seasonRepository.findByLeagueId(league.id.toString());
const activeSeasons = seasons.filter((season) => season.status === 'active');
for (const season of activeSeasons) {
const existing = await this.seedDeps.leagueScoringConfigRepository.findBySeasonId(season.id);
if (existing) continue;
const presetId = this.selectScoringPresetIdForSeason(season);
const preset = getLeagueScoringPresetById(presetId);
if (!preset) {
this.logger.warn(
`[Bootstrap] Scoring preset not found (presetId=${presetId}, seasonId=${season.id}, leagueId=${season.leagueId})`,
);
continue;
}
const scoringConfig = preset.createConfig({ seasonId: season.id });
try {
await this.seedDeps.leagueScoringConfigRepository.save(scoringConfig);
} catch {
// ignore duplicates
}
}
}
}
private selectScoringPresetIdForSeason(season: Season): string {
if (season.leagueId === 'league-5' && season.status === 'active') {
return 'sprint-main-driver';
}
if (season.leagueId === 'league-3') {
return season.id.endsWith('-b') ? 'sprint-main-team' : 'club-default-nations';
}
const match = /^league-(\d+)$/.exec(season.leagueId);
const leagueNumber = match ? Number(match[1]) : undefined;
if (leagueNumber !== undefined) {
switch (leagueNumber % 4) {
case 0:
return 'sprint-main-team';
case 1:
return 'endurance-main-trophy';
case 2:
return 'sprint-main-driver';
case 3:
return 'club-default-nations';
}
}
return 'club-default';
}
}