50 lines
1.8 KiB
TypeScript
50 lines
1.8 KiB
TypeScript
import { DriversApiClient } from '@/lib/api/drivers/DriversApiClient';
|
|
import type { PageQueryResult } from '@/lib/contracts/page-queries/PageQueryResult';
|
|
import { ConsoleErrorReporter } from '@/lib/infrastructure/logging/ConsoleErrorReporter';
|
|
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
|
|
import type { DriversLeaderboardDTO } from '@/lib/types/generated/DriversLeaderboardDTO';
|
|
|
|
/**
|
|
* DriversPageQuery
|
|
*
|
|
* Server-side data fetcher for the drivers listing page.
|
|
* Returns a discriminated union with all possible page states.
|
|
* API DTO is already JSON-serializable.
|
|
*/
|
|
export class DriversPageQuery {
|
|
/**
|
|
* Execute the drivers page query
|
|
*
|
|
* @returns PageQueryResult with discriminated union of states
|
|
*/
|
|
static async execute(): Promise<PageQueryResult<DriversLeaderboardDTO>> {
|
|
try {
|
|
// Manual wiring: construct dependencies explicitly
|
|
const errorReporter = new ConsoleErrorReporter();
|
|
const logger = new ConsoleLogger();
|
|
const baseUrl = process.env.NEXT_PUBLIC_API_URL || '';
|
|
|
|
const apiClient = new DriversApiClient(baseUrl, errorReporter, logger);
|
|
|
|
const result = await apiClient.getLeaderboard();
|
|
|
|
if (!result || !result.drivers) {
|
|
return { status: 'notFound' };
|
|
}
|
|
|
|
// Transform to the expected DTO format
|
|
const dto: DriversLeaderboardDTO = {
|
|
drivers: result.drivers,
|
|
totalRaces: result.drivers.reduce((sum, driver) => sum + driver.racesCompleted, 0),
|
|
totalWins: result.drivers.reduce((sum, driver) => sum + driver.wins, 0),
|
|
activeCount: result.drivers.filter(driver => driver.isActive).length,
|
|
};
|
|
|
|
return { status: 'ok', dto };
|
|
|
|
} catch (error) {
|
|
console.error('DriversPageQuery failed:', error);
|
|
return { status: 'error', errorId: 'DRIVERS_FETCH_FAILED' };
|
|
}
|
|
}
|
|
} |