62 lines
2.2 KiB
TypeScript
62 lines
2.2 KiB
TypeScript
import { PenaltiesApiClient } from '@/lib/api/penalties/PenaltiesApiClient';
|
|
import type { PenaltyTypesReferenceDTO } from '@/lib/types/PenaltyTypesReferenceDTO';
|
|
import { Result } from '@/lib/contracts/Result';
|
|
import { DomainError, Service } from '@/lib/contracts/services/Service';
|
|
import { getWebsiteApiBaseUrl } from '@/lib/config/apiBaseUrl';
|
|
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
|
|
import { EnhancedErrorReporter } from '@/lib/infrastructure/EnhancedErrorReporter';
|
|
|
|
/**
|
|
* Penalty Service
|
|
*
|
|
* Orchestrates penalty operations by coordinating API calls and view model creation.
|
|
* All dependencies are injected via constructor.
|
|
*/
|
|
export class PenaltyService implements Service {
|
|
private readonly apiClient: PenaltiesApiClient;
|
|
|
|
constructor() {
|
|
const baseUrl = getWebsiteApiBaseUrl();
|
|
const logger = new ConsoleLogger();
|
|
const errorReporter = new EnhancedErrorReporter(logger);
|
|
this.apiClient = new PenaltiesApiClient(baseUrl, errorReporter, logger);
|
|
}
|
|
|
|
/**
|
|
* Find penalties by race ID
|
|
*/
|
|
async findByRaceId(raceId: string): Promise<Result<unknown[], DomainError>> {
|
|
try {
|
|
const dto = await this.apiClient.getRacePenalties(raceId);
|
|
return Result.ok(dto.penalties);
|
|
} catch (error: unknown) {
|
|
return Result.err({ type: 'serverError', message: (error as Error).message || 'Failed to find penalties' });
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get allowed penalty types and semantics
|
|
*/
|
|
async getPenaltyTypesReference(): Promise<Result<PenaltyTypesReferenceDTO, DomainError>> {
|
|
try {
|
|
const data = await this.apiClient.getPenaltyTypesReference();
|
|
return Result.ok(data);
|
|
} catch (error: unknown) {
|
|
return Result.err({ type: 'serverError', message: (error as Error).message || 'Failed to get penalty types' });
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Apply a penalty
|
|
*/
|
|
async applyPenalty(input: unknown): Promise<Result<void, DomainError>> {
|
|
try {
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
await this.apiClient.applyPenalty(input as any);
|
|
return Result.ok(undefined);
|
|
} catch (error: unknown) {
|
|
return Result.err({ type: 'serverError', message: (error as Error).message || 'Failed to apply penalty' });
|
|
}
|
|
}
|
|
}
|