Files
gridpilot.gg/apps/website/lib/services/payments/PaymentService.ts
2025-12-18 13:48:35 +01:00

90 lines
2.9 KiB
TypeScript

import { MembershipFeeViewModel } from '@/lib/view-models/MembershipFeeViewModel';
import { PaymentViewModel } from '@/lib/view-models/PaymentViewModel';
import { PrizeViewModel } from '@/lib/view-models/PrizeViewModel';
import { WalletViewModel } from '@/lib/view-models/WalletViewModel';
import type { PaymentsApiClient } from '../../api/payments/PaymentsApiClient';
import type { PaymentDTO } from '../../types/generated/PaymentDto';
import type { PrizeDto } from '../../types/generated/PrizeDto';
// TODO: Move these types to apps/website/lib/types/generated when available
type CreatePaymentInputDto = {
type: 'sponsorship' | 'membership_fee';
amount: number;
payerId: string;
payerType: 'sponsor' | 'driver';
leagueId: string;
seasonId?: string;
};
/**
* Payment Service
*
* Orchestrates payment operations by coordinating API calls and view model creation.
* All dependencies are injected via constructor.
*/
export class PaymentService {
constructor(
private readonly apiClient: PaymentsApiClient
) {}
/**
* Get all payments with optional filters
*/
async getPayments(leagueId?: string, driverId?: string): Promise<PaymentViewModel[]> {
const dto = await this.apiClient.getPayments(leagueId, driverId);
return dto.payments.map((payment: PaymentDTO) => new PaymentViewModel(payment));
}
/**
* Get single payment by ID
*/
async getPayment(paymentId: string): Promise<PaymentViewModel> {
// Note: Assuming the API returns a single payment from the list
const dto = await this.apiClient.getPayments();
const payment = dto.payments.find((p: PaymentDTO) => p.id === paymentId);
if (!payment) {
throw new Error(`Payment with ID ${paymentId} not found`);
}
return new PaymentViewModel(payment);
}
/**
* Create a new payment
*/
async createPayment(input: CreatePaymentInputDto): Promise<PaymentViewModel> {
const dto = await this.apiClient.createPayment(input);
return new PaymentViewModel(dto.payment);
}
/**
* Get membership fees for a league
*/
async getMembershipFees(leagueId: string): Promise<MembershipFeeViewModel | null> {
const dto = await this.apiClient.getMembershipFees(leagueId);
return dto.fee ? new MembershipFeeViewModel(dto.fee) : null;
}
/**
* Get prizes with optional filters
*/
async getPrizes(leagueId?: string, seasonId?: string): Promise<PrizeViewModel[]> {
const dto = await this.apiClient.getPrizes(leagueId, seasonId);
return dto.prizes.map((prize: PrizeDto) => new PrizeViewModel(prize));
}
/**
* Get wallet for a driver
*/
async getWallet(driverId: string): Promise<WalletViewModel> {
const dto = await this.apiClient.getWallet(driverId);
return new WalletViewModel({ ...dto.wallet, transactions: dto.transactions });
}
/**
* Get payment history for a user (driver)
*/
async getPaymentHistory(driverId: string): Promise<PaymentViewModel[]> {
return await this.getPayments(undefined, driverId);
}
}