Files
gridpilot.gg/apps/website/lib/page-queries/page-queries/DriverProfilePageQuery.ts
2026-01-14 10:51:05 +01:00

56 lines
1.9 KiB
TypeScript

import type { PageQueryResult } from '@/lib/contracts/page-queries/PageQueryResult';
import { DriverProfilePageService } from '@/lib/services/drivers/DriverProfilePageService';
import { DriverProfileViewModelBuilder } from '@/lib/builders/view-models/DriverProfileViewModelBuilder';
import type { DriverProfileViewModel } from '@/lib/view-models/DriverProfileViewModel';
/**
* DriverProfilePageQuery
*
* Server-side data fetcher for the driver profile page.
* Returns a discriminated union with all possible page states.
* Uses Service for data access and ViewModelBuilder for transformation.
*/
export class DriverProfilePageQuery {
/**
* Execute the driver profile page query
*
* @param driverId - The driver ID to fetch profile for
* @returns PageQueryResult with discriminated union of states
*/
static async execute(driverId: string | null): Promise<PageQueryResult<DriverProfileViewModel>> {
// Handle missing driver ID
if (!driverId) {
return { status: 'notFound' };
}
try {
// Manual wiring: construct dependencies explicitly
const service = new DriverProfilePageService();
const result = await service.getDriverProfile(driverId);
if (result.isErr()) {
const error = result.getError();
if (error === 'notFound') {
return { status: 'notFound' };
}
if (error === 'unauthorized') {
return { status: 'error', errorId: 'UNAUTHORIZED' };
}
return { status: 'error', errorId: 'DRIVER_PROFILE_FETCH_FAILED' };
}
// Build ViewModel from DTO
const dto = result.unwrap();
const viewModel = DriverProfileViewModelBuilder.build(dto);
return { status: 'ok', dto: viewModel };
} catch (error) {
console.error('DriverProfilePageQuery failed:', error);
return { status: 'error', errorId: 'DRIVER_PROFILE_FETCH_FAILED' };
}
}
}