Files
gridpilot.gg/apps/website/lib/services/drivers/DriverService.ts
2026-01-16 01:00:03 +01:00

113 lines
4.2 KiB
TypeScript

import { DriversApiClient } from '@/lib/api/drivers/DriversApiClient';
import type { CompleteOnboardingInputDTO } from '@/lib/types/generated/CompleteOnboardingInputDTO';
import type { GetDriverOutputDTO } from '@/lib/types/generated/GetDriverOutputDTO';
import type { CompleteOnboardingOutputDTO } from '@/lib/types/generated/CompleteOnboardingOutputDTO';
import type { DriverDTO } from '@/lib/types/generated/DriverDTO';
import type { GetDriverProfileOutputDTO } from '@/lib/types/generated/GetDriverProfileOutputDTO';
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';
/**
* Driver Service - DTO Only
*
* Returns raw API DTOs. No ViewModels or UX logic.
* All client-side presentation logic must be handled by hooks/components.
*/
export class DriverService implements Service {
private readonly apiClient: DriversApiClient;
constructor() {
const baseUrl = getWebsiteApiBaseUrl();
const logger = new ConsoleLogger();
const errorReporter = new EnhancedErrorReporter(logger);
this.apiClient = new DriversApiClient(baseUrl, errorReporter, logger);
}
/**
* Get driver leaderboard (returns DTO)
*/
async getDriverLeaderboard(): Promise<Result<unknown, DomainError>> {
try {
const data = await this.apiClient.getLeaderboard();
return Result.ok(data);
} catch (error: unknown) {
return Result.err({ type: 'serverError', message: (error as Error).message || 'Failed to get leaderboard' });
}
}
/**
* Complete driver onboarding (returns DTO)
*/
async completeDriverOnboarding(input: CompleteOnboardingInputDTO): Promise<Result<CompleteOnboardingOutputDTO, DomainError>> {
try {
const data = await this.apiClient.completeOnboarding(input);
return Result.ok(data);
} catch (error: unknown) {
return Result.err({ type: 'serverError', message: (error as Error).message || 'Failed to complete onboarding' });
}
}
/**
* Get current driver (returns DTO)
*/
async getCurrentDriver(): Promise<Result<DriverDTO | null, DomainError>> {
try {
const data = await this.apiClient.getCurrent();
return Result.ok(data);
} catch (error: unknown) {
return Result.err({ type: 'serverError', message: (error as Error).message || 'Failed to get current driver' });
}
}
/**
* Get driver profile (returns DTO)
*/
async getDriverProfile(driverId: string): Promise<Result<GetDriverProfileOutputDTO, DomainError>> {
try {
const data = await this.apiClient.getDriverProfile(driverId);
return Result.ok(data);
} catch (error: unknown) {
return Result.err({ type: 'serverError', message: (error as Error).message || 'Failed to get driver profile' });
}
}
/**
* Update current driver profile (returns DTO)
*/
async updateProfile(updates: { bio?: string; country?: string }): Promise<Result<DriverDTO, DomainError>> {
try {
const data = await this.apiClient.updateProfile(updates);
return Result.ok(data);
} catch (error: unknown) {
return Result.err({ type: 'serverError', message: (error as Error).message || 'Failed to update profile' });
}
}
/**
* Find driver by ID (returns DTO)
*/
async findById(id: string): Promise<Result<GetDriverOutputDTO | null, DomainError>> {
try {
const data = await this.apiClient.getDriver(id);
return Result.ok(data);
} catch (error: unknown) {
return Result.err({ type: 'serverError', message: (error as Error).message || 'Failed to find driver' });
}
}
/**
* Find multiple drivers by IDs (returns DTOs)
*/
async findByIds(ids: string[]): Promise<Result<GetDriverOutputDTO[], DomainError>> {
try {
const drivers = await Promise.all(ids.map(id => this.apiClient.getDriver(id)));
return Result.ok(drivers.filter((d): d is GetDriverOutputDTO => d !== null));
} catch (error: unknown) {
return Result.err({ type: 'serverError', message: (error as Error).message || 'Failed to find drivers' });
}
}
}