website refactor

This commit is contained in:
2026-01-17 22:55:03 +01:00
parent 64d9e7fd16
commit 69d4cce7f1
64 changed files with 1146 additions and 1014 deletions

View File

@@ -16,31 +16,29 @@ describe('MembershipFeeService', () => {
service = new MembershipFeeService(mockApiClient);
});
describe('getMembershipFees', () => {
it('should call apiClient.getMembershipFees with correct leagueId and return fee and payments', async () => {
describe('getMembershipFee', () => {
it('should call apiClient.getMembershipFees with correct leagueId and return fee', async () => {
const leagueId = 'league-123';
const mockFee: MembershipFeeDto = { id: 'fee-1', leagueId: 'league-123', seasonId: undefined, type: 'season', amount: 100, enabled: true, createdAt: new Date(), updatedAt: new Date() };
const mockPayments: any[] = [];
const mockOutput = { fee: mockFee, payments: mockPayments };
mockApiClient.getMembershipFees.mockResolvedValue(mockOutput);
const mockFee: any = { id: 'fee-1', leagueId: 'league-123', amount: 100 };
const mockOutput = { fee: mockFee, payments: [] };
mockApiClient.getMembershipFees.mockResolvedValue(mockOutput as any);
const result = await service.getMembershipFees(leagueId);
const result = await service.getMembershipFee(leagueId);
expect(mockApiClient.getMembershipFees).toHaveBeenCalledWith({ leagueId });
expect(result.fee).toBeInstanceOf(MembershipFeeViewModel);
expect(result.fee!.id).toEqual('fee-1');
expect(result.payments).toEqual([]);
expect(result).toBeInstanceOf(MembershipFeeViewModel);
expect(result!.id).toEqual('fee-1');
});
it('should return null fee when no fee is returned', async () => {
it('should return null when no fee is returned', async () => {
const leagueId = 'league-456';
const mockOutput = { fee: null, payments: [] };
mockApiClient.getMembershipFees.mockResolvedValue(mockOutput);
mockApiClient.getMembershipFees.mockResolvedValue(mockOutput as any);
const result = await service.getMembershipFees(leagueId);
const result = await service.getMembershipFee(leagueId);
expect(mockApiClient.getMembershipFees).toHaveBeenCalledWith({ leagueId });
expect(result.fee).toBeNull();
expect(result).toBeNull();
});
});
});

View File

@@ -0,0 +1,29 @@
import { injectable, unmanaged } from 'inversify';
import { PaymentsApiClient } from '@/lib/api/payments/PaymentsApiClient';
import { MembershipFeeViewModel } from '@/lib/view-models/MembershipFeeViewModel';
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 MembershipFeeService 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 getMembershipFee(leagueId: string): Promise<MembershipFeeViewModel | null> {
const data = await this.apiClient.getMembershipFees({ leagueId });
if (!data.fee) return null;
return new MembershipFeeViewModel(data.fee);
}
}

View File

@@ -1,16 +1,65 @@
import { Result } from '@/lib/contracts/Result';
import { DomainError, Service } from '@/lib/contracts/services/Service';
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';
/**
* Payment Service - DTO Only
*
* Returns raw API DTOs. No ViewModels or UX logic.
* All client-side presentation logic must be handled by hooks/components.
*/
@injectable()
export class PaymentService implements Service {
constructor() {}
private readonly apiClient: PaymentsApiClient;
async getPaymentById(paymentId: string): Promise<Result<{ id: string; amount: number }, DomainError>> {
return Result.ok({ id: paymentId, amount: 0 });
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 });
}
}

View File

@@ -1,16 +1,28 @@
import { Result } from '@/lib/contracts/Result';
import { DomainError, Service } from '@/lib/contracts/services/Service';
import { injectable, unmanaged } from 'inversify';
import { PaymentsApiClient } from '@/lib/api/payments/PaymentsApiClient';
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';
/**
* Wallet Service - DTO Only
*
* Returns raw API DTOs. No ViewModels or UX logic.
* All client-side presentation logic must be handled by hooks/components.
*/
@injectable()
export class WalletService implements Service {
constructor() {}
private readonly apiClient: PaymentsApiClient;
async getWalletBalance(_: string): Promise<Result<{ balance: number; currency: string }, DomainError>> {
return Result.ok({ balance: 0, currency: 'USD' });
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 getWallet(leagueId: string): Promise<WalletViewModel> {
const data = await this.apiClient.getWallet({ leagueId });
return new WalletViewModel({ ...data.wallet, transactions: data.transactions });
}
}