Files
gridpilot.gg/core/racing/application/use-cases/DeleteLeagueSeasonScheduleRaceUseCase.ts
2026-01-16 19:46:49 +01:00

78 lines
2.4 KiB
TypeScript

import type { Logger } from '@core/shared/domain/Logger';
import { Result } from '@core/shared/domain/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import { RaceRepository } from '../../domain/repositories/RaceRepository';
import { SeasonRepository } from '../../domain/repositories/SeasonRepository';
export interface DeleteLeagueSeasonScheduleRaceInput {
leagueId: string;
seasonId: string;
raceId: string;
}
export type DeleteLeagueSeasonScheduleRaceResult = {
success: true;
};
export type DeleteLeagueSeasonScheduleRaceErrorCode =
| 'SEASON_NOT_FOUND'
| 'RACE_NOT_FOUND'
| 'REPOSITORY_ERROR';
export class DeleteLeagueSeasonScheduleRaceUseCase {
constructor(
private readonly seasonRepository: SeasonRepository,
private readonly raceRepository: RaceRepository,
private readonly logger: Logger,
) {}
async execute(
input: DeleteLeagueSeasonScheduleRaceInput,
): Promise<
Result<
DeleteLeagueSeasonScheduleRaceResult,
ApplicationErrorCode<DeleteLeagueSeasonScheduleRaceErrorCode, { message: string }>
>
> {
this.logger.debug('Deleting league season schedule race', {
leagueId: input.leagueId,
seasonId: input.seasonId,
raceId: input.raceId,
});
try {
const season = await this.seasonRepository.findById(input.seasonId);
if (!season || season.leagueId !== input.leagueId) {
return Result.err({
code: 'SEASON_NOT_FOUND',
details: { message: 'Season not found for league' },
});
}
const race = await this.raceRepository.findById(input.raceId);
if (!race || race.leagueId !== input.leagueId) {
return Result.err({
code: 'RACE_NOT_FOUND',
details: { message: 'Race not found for league' },
});
}
await this.raceRepository.delete(input.raceId);
const result: DeleteLeagueSeasonScheduleRaceResult = { success: true };
return Result.ok(result);
} catch (err) {
const error = err instanceof Error ? err : new Error('Unknown error');
this.logger.error('Failed to delete league season schedule race', error, {
leagueId: input.leagueId,
seasonId: input.seasonId,
raceId: input.raceId,
});
return Result.err({
code: 'REPOSITORY_ERROR',
details: { message: error.message },
});
}
}
}