46 lines
1.8 KiB
TypeScript
46 lines
1.8 KiB
TypeScript
import { DashboardApiClient } from '@/lib/api/dashboard/DashboardApiClient';
|
|
import { DashboardOverviewDTO } from '@/lib/types/generated/DashboardOverviewDTO';
|
|
import { Result } from '@/lib/contracts/Result';
|
|
import { DomainError } from '@/lib/contracts/services/Service';
|
|
import { ConsoleErrorReporter } from '@/lib/infrastructure/logging/ConsoleErrorReporter';
|
|
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
|
|
|
|
/**
|
|
* DashboardService
|
|
*
|
|
* Pure service that creates its own API client and returns Result types.
|
|
* No business logic, only data fetching and error mapping.
|
|
*/
|
|
export class DashboardService {
|
|
private apiClient: DashboardApiClient;
|
|
|
|
constructor() {
|
|
const baseUrl = process.env.NEXT_PUBLIC_API_URL || '';
|
|
const errorReporter = new ConsoleErrorReporter();
|
|
const logger = new ConsoleLogger();
|
|
this.apiClient = new DashboardApiClient(baseUrl, errorReporter, logger);
|
|
}
|
|
|
|
async getDashboardOverview(): Promise<Result<DashboardOverviewDTO, DomainError>> {
|
|
try {
|
|
const dto = await this.apiClient.getDashboardOverview();
|
|
return Result.ok(dto);
|
|
} catch (error) {
|
|
console.error('DashboardService.getDashboardOverview failed:', error);
|
|
|
|
if (error instanceof Error) {
|
|
if (error.message.includes('403') || error.message.includes('401')) {
|
|
return Result.err({ type: 'unauthorized', message: error.message });
|
|
}
|
|
if (error.message.includes('404')) {
|
|
return Result.err({ type: 'notFound', message: error.message });
|
|
}
|
|
if (error.message.includes('5') || error.message.includes('server')) {
|
|
return Result.err({ type: 'serverError', message: error.message });
|
|
}
|
|
}
|
|
|
|
return Result.err({ type: 'unknown', message: 'Dashboard fetch failed' });
|
|
}
|
|
}
|
|
} |