website refactor
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
import { DriversApiClient } from '@/lib/api/drivers/DriversApiClient';
|
||||
import { getWebsiteApiBaseUrl } from '@/lib/config/apiBaseUrl';
|
||||
import { isProductionEnvironment } from '@/lib/config/env';
|
||||
import { Result } from '@/lib/contracts/Result';
|
||||
import type { Service } from '@/lib/contracts/services/Service';
|
||||
import { EnhancedErrorReporter } from '@/lib/infrastructure/EnhancedErrorReporter';
|
||||
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
|
||||
import type { GetDriverProfileOutputDTO } from '@/lib/types/generated/GetDriverProfileOutputDTO';
|
||||
|
||||
type DriverProfilePageServiceError = 'notFound' | 'unauthorized' | 'serverError' | 'unknown';
|
||||
|
||||
/**
|
||||
* DriverProfilePageService
|
||||
*
|
||||
* Service for the driver profile page.
|
||||
* Returns raw API DTOs. No ViewModels or UX logic.
|
||||
*/
|
||||
export class DriverProfilePageService implements Service {
|
||||
async getDriverProfile(driverId: string): Promise<Result<GetDriverProfileOutputDTO, DriverProfilePageServiceError>> {
|
||||
const logger = new ConsoleLogger();
|
||||
|
||||
try {
|
||||
const baseUrl = getWebsiteApiBaseUrl();
|
||||
const errorReporter = new EnhancedErrorReporter(logger, {
|
||||
showUserNotifications: true,
|
||||
logToConsole: true,
|
||||
reportToExternal: isProductionEnvironment(),
|
||||
});
|
||||
|
||||
const apiClient = new DriversApiClient(baseUrl, errorReporter, logger);
|
||||
const dto = await 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');
|
||||
}
|
||||
|
||||
logger.error('DriverProfilePageService failed', error instanceof Error ? error : undefined, { error: errorAny });
|
||||
|
||||
if (errorAny.statusCode && errorAny.statusCode >= 500) {
|
||||
return Result.err('serverError');
|
||||
}
|
||||
|
||||
return Result.err('unknown');
|
||||
}
|
||||
}
|
||||
}
|
||||
58
apps/website/lib/services/drivers/DriversPageService.ts
Normal file
58
apps/website/lib/services/drivers/DriversPageService.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { DriversApiClient } from '@/lib/api/drivers/DriversApiClient';
|
||||
import { getWebsiteApiBaseUrl } from '@/lib/config/apiBaseUrl';
|
||||
import { isProductionEnvironment } from '@/lib/config/env';
|
||||
import { Result } from '@/lib/contracts/Result';
|
||||
import type { Service } from '@/lib/contracts/services/Service';
|
||||
import { EnhancedErrorReporter } from '@/lib/infrastructure/EnhancedErrorReporter';
|
||||
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
|
||||
import type { DriversLeaderboardDTO } from '@/lib/types/generated/DriversLeaderboardDTO';
|
||||
|
||||
type DriversPageServiceError = 'notFound' | 'serverError' | 'unknown';
|
||||
|
||||
/**
|
||||
* DriversPageService
|
||||
*
|
||||
* Service for the drivers listing page.
|
||||
* Returns raw API DTOs. No ViewModels or UX logic.
|
||||
*/
|
||||
export class DriversPageService implements Service {
|
||||
async getLeaderboard(): Promise<Result<DriversLeaderboardDTO, DriversPageServiceError>> {
|
||||
const logger = new ConsoleLogger();
|
||||
|
||||
try {
|
||||
const baseUrl = getWebsiteApiBaseUrl();
|
||||
const errorReporter = new EnhancedErrorReporter(logger, {
|
||||
showUserNotifications: true,
|
||||
logToConsole: true,
|
||||
reportToExternal: isProductionEnvironment(),
|
||||
});
|
||||
|
||||
const apiClient = new DriversApiClient(baseUrl, errorReporter, logger);
|
||||
const result = await apiClient.getLeaderboard();
|
||||
|
||||
if (!result || !result.drivers) {
|
||||
return Result.err('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 Result.ok(dto);
|
||||
} catch (error) {
|
||||
const errorAny = error as { statusCode?: number; message?: string };
|
||||
|
||||
logger.error('DriversPageService failed', error instanceof Error ? error : undefined, { error: errorAny });
|
||||
|
||||
if (errorAny.statusCode && errorAny.statusCode >= 500) {
|
||||
return Result.err('serverError');
|
||||
}
|
||||
|
||||
return Result.err('unknown');
|
||||
}
|
||||
}
|
||||
}
|
||||
17
apps/website/lib/services/drivers/LiveryService.ts
Normal file
17
apps/website/lib/services/drivers/LiveryService.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { Result } from '@/lib/contracts/Result';
|
||||
import type { Service } from '@/lib/contracts/services/Service';
|
||||
|
||||
/**
|
||||
* Livery Service
|
||||
*
|
||||
* Currently not implemented - returns NotImplemented errors for all endpoints.
|
||||
*/
|
||||
export class LiveryService implements Service {
|
||||
async getLiveries(): Promise<Result<void, 'NOT_IMPLEMENTED'>> {
|
||||
return Result.err('NOT_IMPLEMENTED');
|
||||
}
|
||||
|
||||
async uploadLivery(): Promise<Result<void, 'NOT_IMPLEMENTED'>> {
|
||||
return Result.err('NOT_IMPLEMENTED');
|
||||
}
|
||||
}
|
||||
17
apps/website/lib/services/drivers/SettingsService.ts
Normal file
17
apps/website/lib/services/drivers/SettingsService.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { Result } from '@/lib/contracts/Result';
|
||||
import type { Service } from '@/lib/contracts/services/Service';
|
||||
|
||||
/**
|
||||
* Settings Service
|
||||
*
|
||||
* Currently not implemented - returns NotImplemented errors for all endpoints.
|
||||
*/
|
||||
export class SettingsService implements Service {
|
||||
async getSettings(): Promise<Result<void, 'NOT_IMPLEMENTED'>> {
|
||||
return Result.err('NOT_IMPLEMENTED');
|
||||
}
|
||||
|
||||
async updateSettings(): Promise<Result<void, 'NOT_IMPLEMENTED'>> {
|
||||
return Result.err('NOT_IMPLEMENTED');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user