Files
gridpilot.gg/apps/website/lib/services/payments/PaymentService.ts
2026-01-17 22:55:03 +01:00

66 lines
2.8 KiB
TypeScript

import { injectable, unmanaged } from 'inversify';
import { PaymentsApiClient } from '@/lib/api/payments/PaymentsApiClient';
import { PaymentViewModel } from '@/lib/view-models/PaymentViewModel';
import { MembershipFeeViewModel } from '@/lib/view-models/MembershipFeeViewModel';
import { PrizeViewModel } from '@/lib/view-models/PrizeViewModel';
import { WalletViewModel } from '@/lib/view-models/WalletViewModel';
import { getWebsiteApiBaseUrl } from '@/lib/config/apiBaseUrl';
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
import { EnhancedErrorReporter } from '@/lib/infrastructure/EnhancedErrorReporter';
import { Service } from '@/lib/contracts/services/Service';
@injectable()
export class PaymentService implements Service {
private readonly apiClient: PaymentsApiClient;
constructor(@unmanaged() apiClient?: PaymentsApiClient) {
if (apiClient) {
this.apiClient = apiClient;
} else {
const baseUrl = getWebsiteApiBaseUrl();
const logger = new ConsoleLogger();
const errorReporter = new EnhancedErrorReporter(logger);
this.apiClient = new PaymentsApiClient(baseUrl, errorReporter, logger);
}
}
async getPayments(leagueId?: string, payerId?: string): Promise<PaymentViewModel[]> {
const query: any = {};
if (leagueId) query.leagueId = leagueId;
if (payerId) query.payerId = payerId;
const data = await this.apiClient.getPayments(Object.keys(query).length > 0 ? query : undefined);
return data.payments.map(p => new PaymentViewModel(p));
}
async getPayment(id: string): Promise<PaymentViewModel> {
const data = await this.apiClient.getPayments();
const payment = data.payments.find(p => p.id === id);
if (!payment) throw new Error(`Payment with ID ${id} not found`);
return new PaymentViewModel(payment);
}
async createPayment(input: any): Promise<PaymentViewModel> {
const data = await this.apiClient.createPayment(input);
return new PaymentViewModel(data.payment);
}
async getMembershipFees(leagueId: string): Promise<MembershipFeeViewModel | null> {
const data = await this.apiClient.getMembershipFees({ leagueId });
if (!data.fee) return null;
return new MembershipFeeViewModel(data.fee);
}
async getPrizes(leagueId?: string, seasonId?: string): Promise<PrizeViewModel[]> {
const query: any = {};
if (leagueId) query.leagueId = leagueId;
if (seasonId) query.seasonId = seasonId;
const data = await this.apiClient.getPrizes(Object.keys(query).length > 0 ? query : undefined);
return data.prizes.map(p => new PrizeViewModel(p));
}
async getWallet(leagueId: string): Promise<WalletViewModel> {
const data = await this.apiClient.getWallet({ leagueId });
return new WalletViewModel({ ...data.wallet, transactions: data.transactions });
}
}