Files
gridpilot.gg/apps/website/lib/services/drivers/DriverProfileService.ts
2026-01-14 23:46:04 +01:00

49 lines
1.8 KiB
TypeScript

import { DriversApiClient } from '@/lib/api/drivers/DriversApiClient';
import { getWebsiteApiBaseUrl } from '@/lib/config/apiBaseUrl';
import { Result } from '@/lib/contracts/Result';
import type { Service } from '@/lib/contracts/services/Service';
import { ConsoleErrorReporter } from '@/lib/infrastructure/logging/ConsoleErrorReporter';
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
import type { GetDriverProfileOutputDTO } from '@/lib/types/generated/GetDriverProfileOutputDTO';
type DriverProfileServiceError = 'notFound' | 'unauthorized' | 'serverError' | 'unknown';
export class DriverProfileService implements Service {
private apiClient: DriversApiClient;
constructor() {
const baseUrl = getWebsiteApiBaseUrl();
const logger = new ConsoleLogger();
const errorReporter = new ConsoleErrorReporter();
this.apiClient = new DriversApiClient(baseUrl, errorReporter, logger);
}
async getDriverProfile(driverId: string): Promise<Result<GetDriverProfileOutputDTO, DriverProfileServiceError>> {
try {
const dto = await this.apiClient.getDriverProfile(driverId);
if (!dto.currentDriver) {
return Result.err('notFound');
}
return Result.ok(dto);
} catch (error) {
const errorAny = error as { statusCode?: number; message?: string };
if (errorAny.statusCode === 401 || errorAny.message?.toLowerCase().includes('unauthorized')) {
return Result.err('unauthorized');
}
if (errorAny.statusCode === 404 || errorAny.message?.toLowerCase().includes('not found')) {
return Result.err('notFound');
}
if (errorAny.statusCode && errorAny.statusCode >= 500) {
return Result.err('serverError');
}
return Result.err('unknown');
}
}
}