Files
gridpilot.gg/apps/website/lib/services/races/RaceService.ts
2026-01-24 12:47:49 +01:00

106 lines
3.5 KiB
TypeScript

import { getWebsiteApiBaseUrl } from '@/lib/config/apiBaseUrl';
import { Result } from '@/lib/contracts/Result';
import { DomainError, Service } from '@/lib/contracts/services/Service';
import { ApiError } from '@/lib/gateways/api/base/ApiError';
import { RacesApiClient } from '@/lib/gateways/api/races/RacesApiClient';
import { ConsoleErrorReporter } from '@/lib/infrastructure/logging/ConsoleErrorReporter';
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
import { injectable } from 'inversify';
/**
* Race Service - DTO Only
*
* Returns raw API DTOs. No ViewModels or UX logic.
* All client-side presentation logic must be handled by hooks/components.
*/
@injectable()
export class RaceService implements Service {
private apiClient: RacesApiClient;
constructor() {
const baseUrl = getWebsiteApiBaseUrl();
const logger = new ConsoleLogger();
const errorReporter = new ConsoleErrorReporter();
this.apiClient = new RacesApiClient(baseUrl, errorReporter, logger);
}
async getRaceById(raceId: string): Promise<Result<unknown, DomainError>> {
try {
// This would need a driverId, but for now we'll use a placeholder
const data = await this.apiClient.getDetail(raceId, 'placeholder-driver-id');
return Result.ok(data);
} catch (error: unknown) {
return Result.err(this.mapError(error, 'Failed to fetch race by ID'));
}
}
async getRacesByLeagueId(leagueId: string): Promise<Result<unknown, DomainError>> {
try {
const data = await this.apiClient.getPageData(leagueId);
return Result.ok(data);
} catch (error: unknown) {
return Result.err(this.mapError(error, 'Failed to fetch races by league ID'));
}
}
async getAllRacesPageData(): Promise<Result<any, DomainError>> {
try {
const data = await this.apiClient.getPageData();
return Result.ok(data);
} catch (error: unknown) {
return Result.err(this.mapError(error, 'Failed to fetch all races page data'));
}
}
async findByLeagueId(leagueId: string): Promise<Result<unknown[], DomainError>> {
try {
const result = await this.apiClient.getPageData(leagueId);
return Result.ok(result.races || []);
} catch (error: unknown) {
return Result.err(this.mapError(error, 'Failed to find races by league ID'));
}
}
async registerForRace(_: string, __: string, ___: string): Promise<Result<void, DomainError>> {
return Result.err({ type: 'notImplemented', message: 'registerForRace' });
}
async withdrawFromRace(_: string, __: string): Promise<Result<void, DomainError>> {
return Result.err({ type: 'notImplemented', message: 'withdrawFromRace' });
}
async fileProtest(__: unknown): Promise<Result<void, DomainError>> {
return Result.err({ type: 'notImplemented', message: 'fileProtest' });
}
private mapError(error: unknown, defaultMessage: string): DomainError {
if (error instanceof ApiError) {
return {
type: this.mapApiErrorType(error.type),
message: error.message
};
}
return {
type: 'unknown',
message: defaultMessage
};
}
private mapApiErrorType(apiErrorType: string): DomainError['type'] {
switch (apiErrorType) {
case 'NOT_FOUND':
return 'notFound';
case 'AUTH_ERROR':
return 'unauthorized';
case 'VALIDATION_ERROR':
return 'validation';
case 'SERVER_ERROR':
return 'serverError';
case 'NETWORK_ERROR':
return 'networkError';
default:
return 'unknown';
}
}
}