website refactor
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import { injectable } from 'inversify';
|
||||
import { DashboardApiClient } from '@/lib/api/dashboard/DashboardApiClient';
|
||||
import { AnalyticsApiClient } from '@/lib/api/analytics/AnalyticsApiClient';
|
||||
import { DashboardOverviewDTO } from '@/lib/types/generated/DashboardOverviewDTO';
|
||||
import { GetAnalyticsMetricsOutputDTO } from '@/lib/types/generated/GetAnalyticsMetricsOutputDTO';
|
||||
import { Result } from '@/lib/contracts/Result';
|
||||
import { DomainError, Service } from '@/lib/contracts/services/Service';
|
||||
import { ConsoleErrorReporter } from '@/lib/infrastructure/logging/ConsoleErrorReporter';
|
||||
@@ -17,12 +19,14 @@ import { getWebsiteApiBaseUrl } from '@/lib/config/apiBaseUrl';
|
||||
@injectable()
|
||||
export class DashboardService implements Service {
|
||||
private apiClient: DashboardApiClient;
|
||||
private analyticsApiClient: AnalyticsApiClient;
|
||||
|
||||
constructor() {
|
||||
const baseUrl = getWebsiteApiBaseUrl();
|
||||
const errorReporter = new ConsoleErrorReporter();
|
||||
const logger = new ConsoleLogger();
|
||||
this.apiClient = new DashboardApiClient(baseUrl, errorReporter, logger);
|
||||
this.analyticsApiClient = new AnalyticsApiClient(baseUrl, errorReporter, logger);
|
||||
}
|
||||
|
||||
async getDashboardOverview(): Promise<Result<DashboardOverviewDTO, DomainError>> {
|
||||
@@ -30,29 +34,40 @@ export class DashboardService implements Service {
|
||||
const dto = await this.apiClient.getDashboardOverview();
|
||||
return Result.ok(dto);
|
||||
} catch (error) {
|
||||
// Convert ApiError to DomainError
|
||||
if (error instanceof ApiError) {
|
||||
switch (error.type) {
|
||||
case 'NOT_FOUND':
|
||||
return Result.err({ type: 'notFound', message: error.message });
|
||||
case 'AUTH_ERROR':
|
||||
return Result.err({ type: 'unauthorized', message: error.message });
|
||||
case 'SERVER_ERROR':
|
||||
return Result.err({ type: 'serverError', message: error.message });
|
||||
case 'NETWORK_ERROR':
|
||||
case 'TIMEOUT_ERROR':
|
||||
return Result.err({ type: 'networkError', message: error.message });
|
||||
default:
|
||||
return Result.err({ type: 'unknown', message: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
// Handle non-ApiError cases
|
||||
if (error instanceof Error) {
|
||||
return Result.err({ type: 'unknown', message: error.message });
|
||||
}
|
||||
|
||||
return Result.err({ type: 'unknown', message: 'Dashboard fetch failed' });
|
||||
return this.handleError(error, 'Dashboard fetch failed');
|
||||
}
|
||||
}
|
||||
|
||||
async getAnalyticsMetrics(): Promise<Result<GetAnalyticsMetricsOutputDTO, DomainError>> {
|
||||
try {
|
||||
const dto = await this.analyticsApiClient.getAnalyticsMetrics();
|
||||
return Result.ok(dto);
|
||||
} catch (error) {
|
||||
return this.handleError(error, 'Analytics metrics fetch failed');
|
||||
}
|
||||
}
|
||||
|
||||
private handleError(error: unknown, defaultMessage: string): Result<any, DomainError> {
|
||||
if (error instanceof ApiError) {
|
||||
switch (error.type) {
|
||||
case 'NOT_FOUND':
|
||||
return Result.err({ type: 'notFound', message: error.message });
|
||||
case 'AUTH_ERROR':
|
||||
return Result.err({ type: 'unauthorized', message: error.message });
|
||||
case 'SERVER_ERROR':
|
||||
return Result.err({ type: 'serverError', message: error.message });
|
||||
case 'NETWORK_ERROR':
|
||||
case 'TIMEOUT_ERROR':
|
||||
return Result.err({ type: 'networkError', message: error.message });
|
||||
default:
|
||||
return Result.err({ type: 'unknown', message: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
if (error instanceof Error) {
|
||||
return Result.err({ type: 'unknown', message: error.message });
|
||||
}
|
||||
|
||||
return Result.err({ type: 'unknown', message: defaultMessage });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user