website refactor
This commit is contained in:
@@ -1,28 +1,26 @@
|
||||
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 { ConsoleErrorReporter } from '@/lib/infrastructure/logging/ConsoleErrorReporter';
|
||||
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
|
||||
import type { GetDriverProfileOutputDTO } from '@/lib/types/generated/GetDriverProfileOutputDTO';
|
||||
|
||||
type DriverProfileServiceError = 'notFound' | 'unauthorized' | 'serverError' | 'unknown';
|
||||
|
||||
export class DriverProfileService implements Service {
|
||||
async getDriverProfile(driverId: string): Promise<Result<GetDriverProfileOutputDTO, DriverProfileServiceError>> {
|
||||
private apiClient: DriversApiClient;
|
||||
|
||||
constructor() {
|
||||
const baseUrl = getWebsiteApiBaseUrl();
|
||||
const logger = new ConsoleLogger();
|
||||
const errorReporter = new ConsoleErrorReporter();
|
||||
this.apiClient = new DriversApiClient(baseUrl, errorReporter, logger);
|
||||
}
|
||||
|
||||
async getDriverProfile(driverId: string): Promise<Result<GetDriverProfileOutputDTO, DriverProfileServiceError>> {
|
||||
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);
|
||||
const dto = await this.apiClient.getDriverProfile(driverId);
|
||||
|
||||
if (!dto.currentDriver) {
|
||||
return Result.err('notFound');
|
||||
@@ -40,8 +38,6 @@ export class DriverProfileService implements Service {
|
||||
return Result.err('notFound');
|
||||
}
|
||||
|
||||
logger.error('DriverProfileService failed', error instanceof Error ? error : undefined, { error: errorAny });
|
||||
|
||||
if (errorAny.statusCode && errorAny.statusCode >= 500) {
|
||||
return Result.err('serverError');
|
||||
}
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
import { RacesApiClient } from '@/lib/api/races/RacesApiClient';
|
||||
import { Result } from '@/lib/contracts/Result';
|
||||
import { DomainError } from '@/lib/contracts/services/Service';
|
||||
import { DomainError, Service } from '@/lib/contracts/services/Service';
|
||||
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
|
||||
import { ConsoleErrorReporter } from '@/lib/infrastructure/logging/ConsoleErrorReporter';
|
||||
import { getWebsiteApiBaseUrl } from '@/lib/config/apiBaseUrl';
|
||||
import { ApiError } from '@/lib/api/base/ApiError';
|
||||
import type { RacesPageDataDTO } from '@/lib/types/generated/RacesPageDataDTO';
|
||||
import type { RaceDetailDTO } from '@/lib/api/races/RacesApiClient';
|
||||
import type { RaceResultsDetailDTO } from '@/lib/types/generated/RaceResultsDetailDTO';
|
||||
import type { RaceWithSOFDTO } from '@/lib/types/generated/RaceWithSOFDTO';
|
||||
|
||||
/**
|
||||
* Races Service
|
||||
@@ -12,7 +16,7 @@ import { ApiError } from '@/lib/api/base/ApiError';
|
||||
* Orchestration service for race-related operations.
|
||||
* Returns raw API DTOs. No ViewModels or UX logic.
|
||||
*/
|
||||
export class RacesService {
|
||||
export class RacesService implements Service {
|
||||
private apiClient: RacesApiClient;
|
||||
|
||||
constructor() {
|
||||
@@ -28,21 +32,12 @@ export class RacesService {
|
||||
* Get races page data
|
||||
* Returns races for the main races page
|
||||
*/
|
||||
async getRacesPageData(): Promise<Result<any, DomainError>> {
|
||||
async getRacesPageData(): Promise<Result<RacesPageDataDTO, DomainError>> {
|
||||
try {
|
||||
const data = await this.apiClient.getPageData();
|
||||
return Result.ok(data);
|
||||
} catch (error) {
|
||||
if (error instanceof ApiError) {
|
||||
return Result.err({
|
||||
type: this.mapApiErrorType(error.type),
|
||||
message: error.message
|
||||
});
|
||||
}
|
||||
return Result.err({
|
||||
type: 'unknown',
|
||||
message: 'Failed to fetch races page data'
|
||||
});
|
||||
return Result.err(this.mapError(error, 'Failed to fetch races page data'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,21 +45,12 @@ export class RacesService {
|
||||
* Get race detail
|
||||
* Returns detailed information for a specific race
|
||||
*/
|
||||
async getRaceDetail(raceId: string, driverId: string): Promise<Result<any, DomainError>> {
|
||||
async getRaceDetail(raceId: string, driverId: string): Promise<Result<RaceDetailDTO, DomainError>> {
|
||||
try {
|
||||
const data = await this.apiClient.getDetail(raceId, driverId);
|
||||
return Result.ok(data);
|
||||
} catch (error) {
|
||||
if (error instanceof ApiError) {
|
||||
return Result.err({
|
||||
type: this.mapApiErrorType(error.type),
|
||||
message: error.message
|
||||
});
|
||||
}
|
||||
return Result.err({
|
||||
type: 'unknown',
|
||||
message: 'Failed to fetch race detail'
|
||||
});
|
||||
return Result.err(this.mapError(error, 'Failed to fetch race detail'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,21 +58,12 @@ export class RacesService {
|
||||
* Get race results detail
|
||||
* Returns results for a specific race
|
||||
*/
|
||||
async getRaceResultsDetail(raceId: string): Promise<Result<any, DomainError>> {
|
||||
async getRaceResultsDetail(raceId: string): Promise<Result<RaceResultsDetailDTO, DomainError>> {
|
||||
try {
|
||||
const data = await this.apiClient.getResultsDetail(raceId);
|
||||
return Result.ok(data);
|
||||
} catch (error) {
|
||||
if (error instanceof ApiError) {
|
||||
return Result.err({
|
||||
type: this.mapApiErrorType(error.type),
|
||||
message: error.message
|
||||
});
|
||||
}
|
||||
return Result.err({
|
||||
type: 'unknown',
|
||||
message: 'Failed to fetch race results'
|
||||
});
|
||||
return Result.err(this.mapError(error, 'Failed to fetch race results'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,21 +71,12 @@ export class RacesService {
|
||||
* Get race with strength of field
|
||||
* Returns race data with SOF calculation
|
||||
*/
|
||||
async getRaceWithSOF(raceId: string): Promise<Result<any, DomainError>> {
|
||||
async getRaceWithSOF(raceId: string): Promise<Result<RaceWithSOFDTO, DomainError>> {
|
||||
try {
|
||||
const data = await this.apiClient.getWithSOF(raceId);
|
||||
return Result.ok(data);
|
||||
} catch (error) {
|
||||
if (error instanceof ApiError) {
|
||||
return Result.err({
|
||||
type: this.mapApiErrorType(error.type),
|
||||
message: error.message
|
||||
});
|
||||
}
|
||||
return Result.err({
|
||||
type: 'unknown',
|
||||
message: 'Failed to fetch race SOF'
|
||||
});
|
||||
return Result.err(this.mapError(error, 'Failed to fetch race SOF'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,24 +84,28 @@ export class RacesService {
|
||||
* Get all races for the all races page
|
||||
* Returns all races with pagination support
|
||||
*/
|
||||
async getAllRacesPageData(): Promise<Result<any, DomainError>> {
|
||||
async getAllRacesPageData(): Promise<Result<RacesPageDataDTO, DomainError>> {
|
||||
try {
|
||||
const data = await this.apiClient.getPageData();
|
||||
return Result.ok(data);
|
||||
} catch (error) {
|
||||
if (error instanceof ApiError) {
|
||||
return Result.err({
|
||||
type: this.mapApiErrorType(error.type),
|
||||
message: error.message
|
||||
});
|
||||
}
|
||||
return Result.err({
|
||||
type: 'unknown',
|
||||
message: 'Failed to fetch all races'
|
||||
});
|
||||
return Result.err(this.mapError(error, 'Failed to fetch all races'));
|
||||
}
|
||||
}
|
||||
|
||||
private mapError(error: unknown, defaultMessage: string): DomainError {
|
||||
if (error instanceof ApiError) {
|
||||
return {
|
||||
type: this.mapApiErrorType(error.type),
|
||||
message: error.message
|
||||
};
|
||||
}
|
||||
return {
|
||||
type: 'unknown',
|
||||
message: defaultMessage
|
||||
};
|
||||
}
|
||||
|
||||
private mapApiErrorType(apiErrorType: string): DomainError['type'] {
|
||||
switch (apiErrorType) {
|
||||
case 'NOT_FOUND':
|
||||
@@ -150,4 +122,4 @@ export class RacesService {
|
||||
return 'unknown';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user