74 lines
2.9 KiB
TypeScript
74 lines
2.9 KiB
TypeScript
import { getWebsiteApiBaseUrl } from '@/lib/config/apiBaseUrl';
|
|
import { Result } from '@/lib/contracts/Result';
|
|
import { Service, type DomainError } from '@/lib/contracts/services/Service';
|
|
import { SponsorsApiClient } from '@/lib/gateways/api/sponsors/SponsorsApiClient';
|
|
import { EnhancedErrorReporter } from '@/lib/infrastructure/EnhancedErrorReporter';
|
|
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
|
|
import type { SponsorDashboardDTO } from '@/lib/types/generated/SponsorDashboardDTO';
|
|
import type { SponsorSponsorshipsDTO } from '@/lib/types/generated/SponsorSponsorshipsDTO';
|
|
import { SponsorViewModel } from '@/lib/view-models/SponsorViewModel';
|
|
import { injectable } from 'inversify';
|
|
|
|
@injectable()
|
|
export class SponsorService implements Service {
|
|
private readonly apiClient: SponsorsApiClient;
|
|
|
|
constructor() {
|
|
const baseUrl = getWebsiteApiBaseUrl();
|
|
const logger = new ConsoleLogger();
|
|
const errorReporter = new EnhancedErrorReporter(logger);
|
|
this.apiClient = new SponsorsApiClient(baseUrl, errorReporter, logger);
|
|
}
|
|
|
|
async getAllSponsors(): Promise<SponsorViewModel[]> {
|
|
const data = await this.apiClient.getAll();
|
|
return data.sponsors.map(s => new SponsorViewModel(s));
|
|
}
|
|
|
|
async getSponsorDashboard(sponsorId: string): Promise<Result<SponsorDashboardDTO, DomainError>> {
|
|
try {
|
|
const data = await this.apiClient.getDashboard(sponsorId);
|
|
if (!data) return Result.err({ type: 'notFound', message: 'Sponsor dashboard not found' });
|
|
return Result.ok(data);
|
|
} catch (error) {
|
|
return Result.err({ type: 'serverError', message: error instanceof Error ? error.message : 'Unknown error' });
|
|
}
|
|
}
|
|
|
|
async getSponsorSponsorships(sponsorId: string): Promise<Result<SponsorSponsorshipsDTO, DomainError>> {
|
|
try {
|
|
const data = await this.apiClient.getSponsorships(sponsorId);
|
|
if (!data) return Result.err({ type: 'notFound', message: 'Sponsor sponsorships not found' });
|
|
return Result.ok(data);
|
|
} catch (error) {
|
|
return Result.err({ type: 'serverError', message: error instanceof Error ? error.message : 'Unknown error' });
|
|
}
|
|
}
|
|
|
|
async createSponsor(input: any): Promise<any> {
|
|
return this.apiClient.create(input);
|
|
}
|
|
|
|
async getSponsorshipPricing(): Promise<any> {
|
|
return this.apiClient.getPricing();
|
|
}
|
|
|
|
async getAvailableLeagues(): Promise<Result<any[], DomainError>> {
|
|
try {
|
|
const data = await this.apiClient.getAvailableLeagues();
|
|
return Result.ok(data);
|
|
} catch (error) {
|
|
return Result.err({ type: 'serverError', message: error instanceof Error ? error.message : 'Unknown error' });
|
|
}
|
|
}
|
|
|
|
async getLeagueDetail(leagueId: string): Promise<Result<any, DomainError>> {
|
|
try {
|
|
const data = await this.apiClient.getLeagueDetail(leagueId);
|
|
return Result.ok(data);
|
|
} catch (error) {
|
|
return Result.err({ type: 'serverError', message: error instanceof Error ? error.message : 'Unknown error' });
|
|
}
|
|
}
|
|
}
|