website refactor
This commit is contained in:
@@ -7,74 +7,100 @@ import { DomainError, Service } from '@/lib/contracts/services/Service';
|
||||
import { getWebsiteApiBaseUrl } from '@/lib/config/apiBaseUrl';
|
||||
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
|
||||
import { EnhancedErrorReporter } from '@/lib/infrastructure/EnhancedErrorReporter';
|
||||
import { ProtestViewModel } from '@/lib/view-models/ProtestViewModel';
|
||||
import { RaceViewModel } from '@/lib/view-models/RaceViewModel';
|
||||
import { ProtestDriverViewModel } from '@/lib/view-models/ProtestDriverViewModel';
|
||||
|
||||
/**
|
||||
* Protest Service - DTO Only
|
||||
*
|
||||
* Returns raw API DTOs. No ViewModels or UX logic.
|
||||
* All client-side presentation logic must be handled by hooks/components.
|
||||
* @server-safe
|
||||
*/
|
||||
export class ProtestService implements Service {
|
||||
private readonly apiClient: ProtestsApiClient;
|
||||
|
||||
constructor() {
|
||||
const baseUrl = getWebsiteApiBaseUrl();
|
||||
const logger = new ConsoleLogger();
|
||||
const errorReporter = new EnhancedErrorReporter(logger);
|
||||
this.apiClient = new ProtestsApiClient(baseUrl, errorReporter, logger);
|
||||
constructor(apiClient?: ProtestsApiClient) {
|
||||
if (apiClient) {
|
||||
this.apiClient = apiClient;
|
||||
} else {
|
||||
const baseUrl = getWebsiteApiBaseUrl();
|
||||
const logger = new ConsoleLogger();
|
||||
const errorReporter = new EnhancedErrorReporter(logger);
|
||||
this.apiClient = new ProtestsApiClient(baseUrl, errorReporter, logger);
|
||||
}
|
||||
}
|
||||
|
||||
async getLeagueProtests(leagueId: string): Promise<Result<unknown, DomainError>> {
|
||||
async getLeagueProtests(leagueId: string): Promise<any> {
|
||||
try {
|
||||
const data = await this.apiClient.getLeagueProtests(leagueId);
|
||||
return Result.ok(data);
|
||||
const protests = (data as any).protests || [];
|
||||
return {
|
||||
...data,
|
||||
protests: protests.map((p: any) => new ProtestViewModel(p)),
|
||||
};
|
||||
} catch (error: unknown) {
|
||||
return Result.err({ type: 'serverError', message: (error as Error).message || 'Failed to get league protests' });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async getProtestById(leagueId: string, protestId: string): Promise<Result<unknown, DomainError>> {
|
||||
async getProtestById(leagueId: string, protestId: string): Promise<any> {
|
||||
try {
|
||||
const data = await this.apiClient.getLeagueProtest(leagueId, protestId);
|
||||
return Result.ok(data);
|
||||
const protests = (data as any).protests || [];
|
||||
const protest = protests.find((p: any) => p.id === protestId);
|
||||
if (!protest) return null;
|
||||
|
||||
const raceData = (data as any).racesById?.[protest.raceId] || (data as any).races?.[0] || Object.values((data as any).racesById || {})[0];
|
||||
const protestingDriverData = (data as any).driversById?.[protest.protestingDriverId] || (data as any).drivers?.[0] || Object.values((data as any).driversById || {})[0];
|
||||
const accusedDriverData = (data as any).driversById?.[protest.accusedDriverId] || (data as any).drivers?.[1] || Object.values((data as any).driversById || {})[1];
|
||||
|
||||
return {
|
||||
protest: new ProtestViewModel(protest),
|
||||
race: raceData ? new RaceViewModel(raceData) : null,
|
||||
protestingDriver: protestingDriverData ? new ProtestDriverViewModel(protestingDriverData) : null,
|
||||
accusedDriver: accusedDriverData ? new ProtestDriverViewModel(accusedDriverData) : null,
|
||||
};
|
||||
} catch (error: unknown) {
|
||||
return Result.err({ type: 'serverError', message: (error as Error).message || 'Failed to get protest' });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async applyPenalty(input: ApplyPenaltyCommandDTO): Promise<Result<void, DomainError>> {
|
||||
async applyPenalty(input: ApplyPenaltyCommandDTO): Promise<any> {
|
||||
try {
|
||||
await this.apiClient.applyPenalty(input);
|
||||
return Result.ok(undefined);
|
||||
const res = await this.apiClient.applyPenalty(input);
|
||||
return (res as any)?.value || res;
|
||||
} catch (error: unknown) {
|
||||
return Result.err({ type: 'serverError', message: (error as Error).message || 'Failed to apply penalty' });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async requestDefense(input: RequestProtestDefenseCommandDTO): Promise<Result<void, DomainError>> {
|
||||
async requestDefense(input: RequestProtestDefenseCommandDTO): Promise<any> {
|
||||
try {
|
||||
await this.apiClient.requestDefense(input);
|
||||
return Result.ok(undefined);
|
||||
const res = await this.apiClient.requestDefense(input);
|
||||
return (res as any)?.value || res;
|
||||
} catch (error: unknown) {
|
||||
return Result.err({ type: 'serverError', message: (error as Error).message || 'Failed to request defense' });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async reviewProtest(input: ReviewProtestCommandDTO): Promise<Result<void, DomainError>> {
|
||||
async reviewProtest(input: ReviewProtestCommandDTO): Promise<any> {
|
||||
try {
|
||||
await this.apiClient.reviewProtest(input);
|
||||
return Result.ok(undefined);
|
||||
const res = await this.apiClient.reviewProtest(input);
|
||||
return (res as any)?.value || res;
|
||||
} catch (error: unknown) {
|
||||
return Result.err({ type: 'serverError', message: (error as Error).message || 'Failed to review protest' });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async findByRaceId(raceId: string): Promise<Result<unknown, DomainError>> {
|
||||
async findByRaceId(raceId: string): Promise<any> {
|
||||
try {
|
||||
const data = await this.apiClient.getRaceProtests(raceId);
|
||||
return Result.ok(data);
|
||||
const res = await this.apiClient.getRaceProtests(raceId);
|
||||
const data = (res as any).value || res;
|
||||
return data.protests;
|
||||
} catch (error: unknown) {
|
||||
return Result.err({ type: 'serverError', message: (error as Error).message || 'Failed to find protests' });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user