87 lines
2.8 KiB
TypeScript
87 lines
2.8 KiB
TypeScript
import { DriversApiClient } from "@/lib/api/drivers/DriversApiClient";
|
|
import { CompleteOnboardingInputDTO } from "@/lib/types/generated/CompleteOnboardingInputDTO";
|
|
import { DriverProfileDTO } from "@/lib/types/generated/DriverProfileDTO";
|
|
import { CompleteOnboardingViewModel } from "@/lib/view-models/CompleteOnboardingViewModel";
|
|
import { DriverLeaderboardViewModel } from "@/lib/view-models/DriverLeaderboardViewModel";
|
|
import { DriverViewModel } from "@/lib/view-models/DriverViewModel";
|
|
import { DriverProfileViewModel } from "@/lib/view-models/DriverProfileViewModel";
|
|
|
|
// TODO: Create proper DriverDTO in generated types
|
|
type DriverDTO = {
|
|
id: string;
|
|
name: string;
|
|
avatarUrl?: string;
|
|
iracingId?: string;
|
|
rating?: number;
|
|
};
|
|
|
|
/**
|
|
* Driver Service
|
|
*
|
|
* Orchestrates driver operations by coordinating API calls and view model creation.
|
|
* All dependencies are injected via constructor.
|
|
*/
|
|
export class DriverService {
|
|
constructor(
|
|
private readonly apiClient: DriversApiClient
|
|
) {}
|
|
|
|
/**
|
|
* Get driver leaderboard with view model transformation
|
|
*/
|
|
async getDriverLeaderboard(): Promise<DriverLeaderboardViewModel> {
|
|
const dto = await this.apiClient.getLeaderboard();
|
|
return new DriverLeaderboardViewModel(dto);
|
|
}
|
|
|
|
/**
|
|
* Complete driver onboarding with view model transformation
|
|
*/
|
|
async completeDriverOnboarding(input: CompleteOnboardingInputDTO): Promise<CompleteOnboardingViewModel> {
|
|
const dto = await this.apiClient.completeOnboarding(input);
|
|
return new CompleteOnboardingViewModel(dto);
|
|
}
|
|
|
|
/**
|
|
* Get current driver with view model transformation
|
|
*/
|
|
async getCurrentDriver(): Promise<DriverViewModel | null> {
|
|
const dto = await this.apiClient.getCurrent();
|
|
if (!dto) {
|
|
return null;
|
|
}
|
|
return new DriverViewModel(dto);
|
|
}
|
|
|
|
/**
|
|
* Get driver profile with full details and view model transformation
|
|
*/
|
|
async getDriverProfile(driverId: string): Promise<DriverProfileViewModel> {
|
|
const dto = await this.apiClient.getDriverProfile(driverId);
|
|
return new DriverProfileViewModel(dto);
|
|
}
|
|
|
|
/**
|
|
* Update current driver profile with view model transformation
|
|
*/
|
|
async updateProfile(updates: { bio?: string; country?: string }): Promise<DriverProfileViewModel> {
|
|
const dto = await this.apiClient.updateProfile(updates);
|
|
// After updating, get the full profile again to return updated view model
|
|
return this.getDriverProfile(dto.id);
|
|
}
|
|
|
|
/**
|
|
* Find driver by ID
|
|
*/
|
|
async findById(id: string): Promise<DriverDTO | null> {
|
|
return this.apiClient.getDriver(id);
|
|
}
|
|
|
|
/**
|
|
* Find multiple drivers by IDs
|
|
*/
|
|
async findByIds(ids: string[]): Promise<DriverDTO[]> {
|
|
const drivers = await Promise.all(ids.map(id => this.apiClient.getDriver(id)));
|
|
return drivers.filter((d): d is DriverDTO => d !== null);
|
|
}
|
|
} |