view models
This commit is contained in:
@@ -1,347 +0,0 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { SponsorService } from './SponsorService';
|
||||
import type { SponsorsApiClient } from '../../api/sponsors/SponsorsApiClient';
|
||||
import type { SponsorListPresenter } from '../../presenters/SponsorListPresenter';
|
||||
import type { SponsorDashboardPresenter } from '../../presenters/SponsorDashboardPresenter';
|
||||
import type { SponsorSponsorshipsPresenter } from '../../presenters/SponsorSponsorshipsPresenter';
|
||||
import type {
|
||||
GetSponsorsOutputDto,
|
||||
SponsorDashboardDto,
|
||||
SponsorSponsorshipsDto,
|
||||
CreateSponsorInputDto,
|
||||
CreateSponsorOutputDto,
|
||||
GetEntitySponsorshipPricingResultDto,
|
||||
} from '../../dtos';
|
||||
import type { SponsorViewModel, SponsorDashboardViewModel, SponsorSponsorshipsViewModel } from '../../view-models';
|
||||
|
||||
describe('SponsorService', () => {
|
||||
let service: SponsorService;
|
||||
let mockApiClient: SponsorsApiClient;
|
||||
let mockSponsorListPresenter: SponsorListPresenter;
|
||||
let mockSponsorDashboardPresenter: SponsorDashboardPresenter;
|
||||
let mockSponsorSponsorshipsPresenter: SponsorSponsorshipsPresenter;
|
||||
|
||||
beforeEach(() => {
|
||||
mockApiClient = {
|
||||
getAll: vi.fn(),
|
||||
getDashboard: vi.fn(),
|
||||
getSponsorships: vi.fn(),
|
||||
create: vi.fn(),
|
||||
getPricing: vi.fn(),
|
||||
} as unknown as SponsorsApiClient;
|
||||
|
||||
mockSponsorListPresenter = {
|
||||
present: vi.fn(),
|
||||
} as unknown as SponsorListPresenter;
|
||||
|
||||
mockSponsorDashboardPresenter = {
|
||||
present: vi.fn(),
|
||||
} as unknown as SponsorDashboardPresenter;
|
||||
|
||||
mockSponsorSponsorshipsPresenter = {
|
||||
present: vi.fn(),
|
||||
} as unknown as SponsorSponsorshipsPresenter;
|
||||
|
||||
service = new SponsorService(
|
||||
mockApiClient,
|
||||
mockSponsorListPresenter,
|
||||
mockSponsorDashboardPresenter,
|
||||
mockSponsorSponsorshipsPresenter
|
||||
);
|
||||
});
|
||||
|
||||
describe('constructor', () => {
|
||||
it('should create instance with injected dependencies', () => {
|
||||
expect(service).toBeInstanceOf(SponsorService);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getAllSponsors', () => {
|
||||
it('should fetch all sponsors from API and transform via presenter', async () => {
|
||||
// Arrange
|
||||
const mockDto: GetSponsorsOutputDto = {
|
||||
sponsors: [
|
||||
{
|
||||
id: 'sponsor-1',
|
||||
name: 'Sponsor Alpha',
|
||||
logoUrl: 'https://example.com/logo1.png',
|
||||
websiteUrl: 'https://alpha.com',
|
||||
},
|
||||
{
|
||||
id: 'sponsor-2',
|
||||
name: 'Sponsor Beta',
|
||||
logoUrl: 'https://example.com/logo2.png',
|
||||
websiteUrl: 'https://beta.com',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const mockViewModels: SponsorViewModel[] = [
|
||||
{
|
||||
id: 'sponsor-1',
|
||||
name: 'Sponsor Alpha',
|
||||
logoUrl: 'https://example.com/logo1.png',
|
||||
websiteUrl: 'https://alpha.com',
|
||||
} as SponsorViewModel,
|
||||
{
|
||||
id: 'sponsor-2',
|
||||
name: 'Sponsor Beta',
|
||||
logoUrl: 'https://example.com/logo2.png',
|
||||
websiteUrl: 'https://beta.com',
|
||||
} as SponsorViewModel,
|
||||
];
|
||||
|
||||
vi.mocked(mockApiClient.getAll).mockResolvedValue(mockDto);
|
||||
vi.mocked(mockSponsorListPresenter.present).mockReturnValue(mockViewModels);
|
||||
|
||||
// Act
|
||||
const result = await service.getAllSponsors();
|
||||
|
||||
// Assert
|
||||
expect(mockApiClient.getAll).toHaveBeenCalled();
|
||||
expect(mockSponsorListPresenter.present).toHaveBeenCalledWith(mockDto);
|
||||
expect(result).toEqual(mockViewModels);
|
||||
});
|
||||
|
||||
it('should handle empty sponsors list', async () => {
|
||||
// Arrange
|
||||
const mockDto: GetSponsorsOutputDto = {
|
||||
sponsors: [],
|
||||
};
|
||||
|
||||
const mockViewModels: SponsorViewModel[] = [];
|
||||
|
||||
vi.mocked(mockApiClient.getAll).mockResolvedValue(mockDto);
|
||||
vi.mocked(mockSponsorListPresenter.present).mockReturnValue(mockViewModels);
|
||||
|
||||
// Act
|
||||
const result = await service.getAllSponsors();
|
||||
|
||||
// Assert
|
||||
expect(mockApiClient.getAll).toHaveBeenCalled();
|
||||
expect(mockSponsorListPresenter.present).toHaveBeenCalledWith(mockDto);
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it('should propagate errors from API client', async () => {
|
||||
// Arrange
|
||||
const error = new Error('Failed to fetch sponsors');
|
||||
vi.mocked(mockApiClient.getAll).mockRejectedValue(error);
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.getAllSponsors()).rejects.toThrow('Failed to fetch sponsors');
|
||||
expect(mockApiClient.getAll).toHaveBeenCalled();
|
||||
expect(mockSponsorListPresenter.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('getSponsorDashboard', () => {
|
||||
it('should fetch sponsor dashboard and transform via presenter', async () => {
|
||||
// Arrange
|
||||
const sponsorId = 'sponsor-123';
|
||||
|
||||
const mockDto: SponsorDashboardDto = {
|
||||
sponsorId,
|
||||
sponsorName: 'Sponsor Alpha',
|
||||
totalSponsorships: 10,
|
||||
activeSponsorships: 7,
|
||||
totalInvestment: 50000,
|
||||
};
|
||||
|
||||
const mockViewModel: SponsorDashboardViewModel = {
|
||||
sponsorId,
|
||||
sponsorName: 'Sponsor Alpha',
|
||||
totalSponsorships: 10,
|
||||
activeSponsorships: 7,
|
||||
totalInvestment: 50000,
|
||||
} as SponsorDashboardViewModel;
|
||||
|
||||
vi.mocked(mockApiClient.getDashboard).mockResolvedValue(mockDto);
|
||||
vi.mocked(mockSponsorDashboardPresenter.present).mockReturnValue(mockViewModel);
|
||||
|
||||
// Act
|
||||
const result = await service.getSponsorDashboard(sponsorId);
|
||||
|
||||
// Assert
|
||||
expect(mockApiClient.getDashboard).toHaveBeenCalledWith(sponsorId);
|
||||
expect(mockSponsorDashboardPresenter.present).toHaveBeenCalledWith(mockDto);
|
||||
expect(result).toEqual(mockViewModel);
|
||||
});
|
||||
|
||||
it('should return null when dashboard is not found', async () => {
|
||||
// Arrange
|
||||
const sponsorId = 'non-existent';
|
||||
|
||||
vi.mocked(mockApiClient.getDashboard).mockResolvedValue(null);
|
||||
|
||||
// Act
|
||||
const result = await service.getSponsorDashboard(sponsorId);
|
||||
|
||||
// Assert
|
||||
expect(mockApiClient.getDashboard).toHaveBeenCalledWith(sponsorId);
|
||||
expect(mockSponsorDashboardPresenter.present).not.toHaveBeenCalled();
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('should propagate errors from API client', async () => {
|
||||
// Arrange
|
||||
const sponsorId = 'sponsor-123';
|
||||
const error = new Error('Failed to fetch dashboard');
|
||||
vi.mocked(mockApiClient.getDashboard).mockRejectedValue(error);
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.getSponsorDashboard(sponsorId)).rejects.toThrow('Failed to fetch dashboard');
|
||||
expect(mockApiClient.getDashboard).toHaveBeenCalledWith(sponsorId);
|
||||
expect(mockSponsorDashboardPresenter.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('getSponsorSponsorships', () => {
|
||||
it('should fetch sponsor sponsorships and transform via presenter', async () => {
|
||||
// Arrange
|
||||
const sponsorId = 'sponsor-123';
|
||||
|
||||
const mockDto: SponsorSponsorshipsDto = {
|
||||
sponsorId,
|
||||
sponsorName: 'Sponsor Alpha',
|
||||
sponsorships: [
|
||||
{
|
||||
id: 'sponsorship-1',
|
||||
leagueId: 'league-1',
|
||||
leagueName: 'League One',
|
||||
seasonId: 'season-1',
|
||||
tier: 'main',
|
||||
status: 'active',
|
||||
amount: 10000,
|
||||
currency: 'USD',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const mockViewModel: SponsorSponsorshipsViewModel = {
|
||||
sponsorId,
|
||||
sponsorName: 'Sponsor Alpha',
|
||||
sponsorships: [],
|
||||
} as SponsorSponsorshipsViewModel;
|
||||
|
||||
vi.mocked(mockApiClient.getSponsorships).mockResolvedValue(mockDto);
|
||||
vi.mocked(mockSponsorSponsorshipsPresenter.present).mockReturnValue(mockViewModel);
|
||||
|
||||
// Act
|
||||
const result = await service.getSponsorSponsorships(sponsorId);
|
||||
|
||||
// Assert
|
||||
expect(mockApiClient.getSponsorships).toHaveBeenCalledWith(sponsorId);
|
||||
expect(mockSponsorSponsorshipsPresenter.present).toHaveBeenCalledWith(mockDto);
|
||||
expect(result).toEqual(mockViewModel);
|
||||
});
|
||||
|
||||
it('should return null when sponsorships are not found', async () => {
|
||||
// Arrange
|
||||
const sponsorId = 'non-existent';
|
||||
|
||||
vi.mocked(mockApiClient.getSponsorships).mockResolvedValue(null);
|
||||
|
||||
// Act
|
||||
const result = await service.getSponsorSponsorships(sponsorId);
|
||||
|
||||
// Assert
|
||||
expect(mockApiClient.getSponsorships).toHaveBeenCalledWith(sponsorId);
|
||||
expect(mockSponsorSponsorshipsPresenter.present).not.toHaveBeenCalled();
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('should propagate errors from API client', async () => {
|
||||
// Arrange
|
||||
const sponsorId = 'sponsor-123';
|
||||
const error = new Error('Failed to fetch sponsorships');
|
||||
vi.mocked(mockApiClient.getSponsorships).mockRejectedValue(error);
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.getSponsorSponsorships(sponsorId)).rejects.toThrow('Failed to fetch sponsorships');
|
||||
expect(mockApiClient.getSponsorships).toHaveBeenCalledWith(sponsorId);
|
||||
expect(mockSponsorSponsorshipsPresenter.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('createSponsor', () => {
|
||||
it('should create a new sponsor', async () => {
|
||||
// Arrange
|
||||
const input: CreateSponsorInputDto = {
|
||||
name: 'New Sponsor',
|
||||
logoUrl: 'https://example.com/logo.png',
|
||||
websiteUrl: 'https://newsponsor.com',
|
||||
userId: 'user-123',
|
||||
};
|
||||
|
||||
const mockOutput: CreateSponsorOutputDto = {
|
||||
sponsorId: 'sponsor-new',
|
||||
success: true,
|
||||
};
|
||||
|
||||
vi.mocked(mockApiClient.create).mockResolvedValue(mockOutput);
|
||||
|
||||
// Act
|
||||
const result = await service.createSponsor(input);
|
||||
|
||||
// Assert
|
||||
expect(mockApiClient.create).toHaveBeenCalledWith(input);
|
||||
expect(result).toEqual(mockOutput);
|
||||
});
|
||||
|
||||
it('should propagate errors from API client', async () => {
|
||||
// Arrange
|
||||
const input: CreateSponsorInputDto = {
|
||||
name: 'New Sponsor',
|
||||
userId: 'user-123',
|
||||
};
|
||||
const error = new Error('Failed to create sponsor');
|
||||
vi.mocked(mockApiClient.create).mockRejectedValue(error);
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.createSponsor(input)).rejects.toThrow('Failed to create sponsor');
|
||||
expect(mockApiClient.create).toHaveBeenCalledWith(input);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getSponsorshipPricing', () => {
|
||||
it('should fetch sponsorship pricing', async () => {
|
||||
// Arrange
|
||||
const mockPricing: GetEntitySponsorshipPricingResultDto = {
|
||||
pricingItems: [
|
||||
{
|
||||
tier: 'main',
|
||||
price: 10000,
|
||||
currency: 'USD',
|
||||
benefits: ['Logo placement', 'Race announcements'],
|
||||
},
|
||||
{
|
||||
tier: 'secondary',
|
||||
price: 5000,
|
||||
currency: 'USD',
|
||||
benefits: ['Logo placement'],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
vi.mocked(mockApiClient.getPricing).mockResolvedValue(mockPricing);
|
||||
|
||||
// Act
|
||||
const result = await service.getSponsorshipPricing();
|
||||
|
||||
// Assert
|
||||
expect(mockApiClient.getPricing).toHaveBeenCalled();
|
||||
expect(result).toEqual(mockPricing);
|
||||
});
|
||||
|
||||
it('should propagate errors from API client', async () => {
|
||||
// Arrange
|
||||
const error = new Error('Failed to fetch pricing');
|
||||
vi.mocked(mockApiClient.getPricing).mockRejectedValue(error);
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.getSponsorshipPricing()).rejects.toThrow('Failed to fetch pricing');
|
||||
expect(mockApiClient.getPricing).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,58 +1,57 @@
|
||||
import type { SponsorsApiClient } from '../../api/sponsors/SponsorsApiClient';
|
||||
import type { SponsorListPresenter } from '../../presenters/SponsorListPresenter';
|
||||
import type { SponsorDashboardPresenter } from '../../presenters/SponsorDashboardPresenter';
|
||||
import type { SponsorSponsorshipsPresenter } from '../../presenters/SponsorSponsorshipsPresenter';
|
||||
import type { SponsorViewModel, SponsorDashboardViewModel, SponsorSponsorshipsViewModel } from '../../view-models';
|
||||
import type { CreateSponsorInputDto, CreateSponsorOutputDto, GetEntitySponsorshipPricingResultDto } from '../../dtos';
|
||||
import { SponsorViewModel, SponsorDashboardViewModel, SponsorSponsorshipsViewModel } from '../../view-models';
|
||||
import type { CreateSponsorInputDTO } from '../../types/generated';
|
||||
|
||||
// TODO: Move these types to apps/website/lib/types/generated when available
|
||||
type CreateSponsorOutputDto = { id: string; name: string };
|
||||
type GetEntitySponsorshipPricingResultDto = { pricing: Array<{ entityType: string; price: number }> };
|
||||
type SponsorDTO = { id: string; name: string; logoUrl?: string; websiteUrl?: string };
|
||||
|
||||
/**
|
||||
* Sponsor Service
|
||||
*
|
||||
* Orchestrates sponsor operations by coordinating API calls and presentation logic.
|
||||
* Orchestrates sponsor operations by coordinating API calls and view model creation.
|
||||
* All dependencies are injected via constructor.
|
||||
*/
|
||||
export class SponsorService {
|
||||
constructor(
|
||||
private readonly apiClient: SponsorsApiClient,
|
||||
private readonly sponsorListPresenter: SponsorListPresenter,
|
||||
private readonly sponsorDashboardPresenter: SponsorDashboardPresenter,
|
||||
private readonly sponsorSponsorshipsPresenter: SponsorSponsorshipsPresenter
|
||||
private readonly apiClient: SponsorsApiClient
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Get all sponsors with presentation transformation
|
||||
* Get all sponsors with view model transformation
|
||||
*/
|
||||
async getAllSponsors(): Promise<SponsorViewModel[]> {
|
||||
const dto = await this.apiClient.getAll();
|
||||
return this.sponsorListPresenter.present(dto);
|
||||
return dto.sponsors.map((sponsor: SponsorDTO) => new SponsorViewModel(sponsor));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get sponsor dashboard with presentation transformation
|
||||
* Get sponsor dashboard with view model transformation
|
||||
*/
|
||||
async getSponsorDashboard(sponsorId: string): Promise<SponsorDashboardViewModel | null> {
|
||||
const dto = await this.apiClient.getDashboard(sponsorId);
|
||||
if (!dto) {
|
||||
return null;
|
||||
}
|
||||
return this.sponsorDashboardPresenter.present(dto);
|
||||
return new SponsorDashboardViewModel(dto);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get sponsor sponsorships with presentation transformation
|
||||
* Get sponsor sponsorships with view model transformation
|
||||
*/
|
||||
async getSponsorSponsorships(sponsorId: string): Promise<SponsorSponsorshipsViewModel | null> {
|
||||
const dto = await this.apiClient.getSponsorships(sponsorId);
|
||||
if (!dto) {
|
||||
return null;
|
||||
}
|
||||
return this.sponsorSponsorshipsPresenter.present(dto);
|
||||
return new SponsorSponsorshipsViewModel(dto);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new sponsor
|
||||
*/
|
||||
async createSponsor(input: CreateSponsorInputDto): Promise<CreateSponsorOutputDto> {
|
||||
async createSponsor(input: CreateSponsorInputDTO): Promise<CreateSponsorOutputDto> {
|
||||
return await this.apiClient.create(input);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,334 +0,0 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { SponsorshipService } from './SponsorshipService';
|
||||
import type { SponsorsApiClient } from '../../api/sponsors/SponsorsApiClient';
|
||||
import type { SponsorshipPricingPresenter } from '../../presenters/SponsorshipPricingPresenter';
|
||||
import type { SponsorSponsorshipsPresenter } from '../../presenters/SponsorSponsorshipsPresenter';
|
||||
import type {
|
||||
GetEntitySponsorshipPricingResultDto,
|
||||
SponsorSponsorshipsDto,
|
||||
} from '../../dtos';
|
||||
import type { SponsorshipPricingViewModel, SponsorSponsorshipsViewModel } from '../../view-models';
|
||||
|
||||
describe('SponsorshipService', () => {
|
||||
let service: SponsorshipService;
|
||||
let mockApiClient: SponsorsApiClient;
|
||||
let mockSponsorshipPricingPresenter: SponsorshipPricingPresenter;
|
||||
let mockSponsorSponsorshipsPresenter: SponsorSponsorshipsPresenter;
|
||||
|
||||
beforeEach(() => {
|
||||
mockApiClient = {
|
||||
getPricing: vi.fn(),
|
||||
getSponsorships: vi.fn(),
|
||||
} as unknown as SponsorsApiClient;
|
||||
|
||||
mockSponsorshipPricingPresenter = {
|
||||
present: vi.fn(),
|
||||
} as unknown as SponsorshipPricingPresenter;
|
||||
|
||||
mockSponsorSponsorshipsPresenter = {
|
||||
present: vi.fn(),
|
||||
} as unknown as SponsorSponsorshipsPresenter;
|
||||
|
||||
service = new SponsorshipService(
|
||||
mockApiClient,
|
||||
mockSponsorshipPricingPresenter,
|
||||
mockSponsorSponsorshipsPresenter
|
||||
);
|
||||
});
|
||||
|
||||
describe('constructor', () => {
|
||||
it('should create instance with injected dependencies', () => {
|
||||
expect(service).toBeInstanceOf(SponsorshipService);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getSponsorshipPricing', () => {
|
||||
it('should fetch sponsorship pricing from API and transform via presenter', async () => {
|
||||
// Arrange
|
||||
const mockDto: GetEntitySponsorshipPricingResultDto = {
|
||||
mainSlotPrice: 10000,
|
||||
secondarySlotPrice: 5000,
|
||||
currency: 'USD',
|
||||
};
|
||||
|
||||
const mockViewModel: SponsorshipPricingViewModel = {
|
||||
mainSlotPrice: 10000,
|
||||
secondarySlotPrice: 5000,
|
||||
currency: 'USD',
|
||||
formattedMainSlotPrice: 'USD 10,000',
|
||||
formattedSecondarySlotPrice: 'USD 5,000',
|
||||
priceDifference: 5000,
|
||||
formattedPriceDifference: 'USD 5,000',
|
||||
secondaryDiscountPercentage: 50,
|
||||
} as SponsorshipPricingViewModel;
|
||||
|
||||
vi.mocked(mockApiClient.getPricing).mockResolvedValue(mockDto);
|
||||
vi.mocked(mockSponsorshipPricingPresenter.present).mockReturnValue(mockViewModel);
|
||||
|
||||
// Act
|
||||
const result = await service.getSponsorshipPricing();
|
||||
|
||||
// Assert
|
||||
expect(mockApiClient.getPricing).toHaveBeenCalled();
|
||||
expect(mockSponsorshipPricingPresenter.present).toHaveBeenCalledWith(mockDto);
|
||||
expect(result).toEqual(mockViewModel);
|
||||
});
|
||||
|
||||
it('should handle different currencies', async () => {
|
||||
// Arrange
|
||||
const mockDto: GetEntitySponsorshipPricingResultDto = {
|
||||
mainSlotPrice: 8000,
|
||||
secondarySlotPrice: 4000,
|
||||
currency: 'EUR',
|
||||
};
|
||||
|
||||
const mockViewModel: SponsorshipPricingViewModel = {
|
||||
mainSlotPrice: 8000,
|
||||
secondarySlotPrice: 4000,
|
||||
currency: 'EUR',
|
||||
formattedMainSlotPrice: 'EUR 8,000',
|
||||
formattedSecondarySlotPrice: 'EUR 4,000',
|
||||
priceDifference: 4000,
|
||||
formattedPriceDifference: 'EUR 4,000',
|
||||
secondaryDiscountPercentage: 50,
|
||||
} as SponsorshipPricingViewModel;
|
||||
|
||||
vi.mocked(mockApiClient.getPricing).mockResolvedValue(mockDto);
|
||||
vi.mocked(mockSponsorshipPricingPresenter.present).mockReturnValue(mockViewModel);
|
||||
|
||||
// Act
|
||||
const result = await service.getSponsorshipPricing();
|
||||
|
||||
// Assert
|
||||
expect(mockApiClient.getPricing).toHaveBeenCalled();
|
||||
expect(mockSponsorshipPricingPresenter.present).toHaveBeenCalledWith(mockDto);
|
||||
expect(result).toEqual(mockViewModel);
|
||||
expect(result.currency).toBe('EUR');
|
||||
});
|
||||
|
||||
it('should propagate errors from API client', async () => {
|
||||
// Arrange
|
||||
const error = new Error('Failed to fetch pricing');
|
||||
vi.mocked(mockApiClient.getPricing).mockRejectedValue(error);
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.getSponsorshipPricing()).rejects.toThrow('Failed to fetch pricing');
|
||||
expect(mockApiClient.getPricing).toHaveBeenCalled();
|
||||
expect(mockSponsorshipPricingPresenter.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should handle zero prices', async () => {
|
||||
// Arrange
|
||||
const mockDto: GetEntitySponsorshipPricingResultDto = {
|
||||
mainSlotPrice: 0,
|
||||
secondarySlotPrice: 0,
|
||||
currency: 'USD',
|
||||
};
|
||||
|
||||
const mockViewModel: SponsorshipPricingViewModel = {
|
||||
mainSlotPrice: 0,
|
||||
secondarySlotPrice: 0,
|
||||
currency: 'USD',
|
||||
formattedMainSlotPrice: 'USD 0',
|
||||
formattedSecondarySlotPrice: 'USD 0',
|
||||
priceDifference: 0,
|
||||
formattedPriceDifference: 'USD 0',
|
||||
secondaryDiscountPercentage: 0,
|
||||
} as SponsorshipPricingViewModel;
|
||||
|
||||
vi.mocked(mockApiClient.getPricing).mockResolvedValue(mockDto);
|
||||
vi.mocked(mockSponsorshipPricingPresenter.present).mockReturnValue(mockViewModel);
|
||||
|
||||
// Act
|
||||
const result = await service.getSponsorshipPricing();
|
||||
|
||||
// Assert
|
||||
expect(mockApiClient.getPricing).toHaveBeenCalled();
|
||||
expect(mockSponsorshipPricingPresenter.present).toHaveBeenCalledWith(mockDto);
|
||||
expect(result).toEqual(mockViewModel);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getSponsorSponsorships', () => {
|
||||
it('should fetch sponsor sponsorships and transform via presenter', async () => {
|
||||
// Arrange
|
||||
const sponsorId = 'sponsor-123';
|
||||
|
||||
const mockDto: SponsorSponsorshipsDto = {
|
||||
sponsorId,
|
||||
sponsorName: 'Sponsor Alpha',
|
||||
sponsorships: [
|
||||
{
|
||||
id: 'sponsorship-1',
|
||||
leagueId: 'league-1',
|
||||
leagueName: 'League One',
|
||||
seasonId: 'season-1',
|
||||
tier: 'main',
|
||||
status: 'active',
|
||||
amount: 10000,
|
||||
currency: 'USD',
|
||||
},
|
||||
{
|
||||
id: 'sponsorship-2',
|
||||
leagueId: 'league-2',
|
||||
leagueName: 'League Two',
|
||||
seasonId: 'season-2',
|
||||
tier: 'secondary',
|
||||
status: 'active',
|
||||
amount: 5000,
|
||||
currency: 'USD',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const mockViewModel: SponsorSponsorshipsViewModel = {
|
||||
sponsorId,
|
||||
sponsorName: 'Sponsor Alpha',
|
||||
sponsorships: [],
|
||||
totalCount: 2,
|
||||
activeCount: 2,
|
||||
hasSponsorships: true,
|
||||
totalInvestment: 15000,
|
||||
formattedTotalInvestment: 'USD 15,000',
|
||||
} as SponsorSponsorshipsViewModel;
|
||||
|
||||
vi.mocked(mockApiClient.getSponsorships).mockResolvedValue(mockDto);
|
||||
vi.mocked(mockSponsorSponsorshipsPresenter.present).mockReturnValue(mockViewModel);
|
||||
|
||||
// Act
|
||||
const result = await service.getSponsorSponsorships(sponsorId);
|
||||
|
||||
// Assert
|
||||
expect(mockApiClient.getSponsorships).toHaveBeenCalledWith(sponsorId);
|
||||
expect(mockSponsorSponsorshipsPresenter.present).toHaveBeenCalledWith(mockDto);
|
||||
expect(result).toEqual(mockViewModel);
|
||||
});
|
||||
|
||||
it('should return null when sponsorships are not found', async () => {
|
||||
// Arrange
|
||||
const sponsorId = 'non-existent';
|
||||
|
||||
vi.mocked(mockApiClient.getSponsorships).mockResolvedValue(null);
|
||||
|
||||
// Act
|
||||
const result = await service.getSponsorSponsorships(sponsorId);
|
||||
|
||||
// Assert
|
||||
expect(mockApiClient.getSponsorships).toHaveBeenCalledWith(sponsorId);
|
||||
expect(mockSponsorSponsorshipsPresenter.present).not.toHaveBeenCalled();
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('should handle empty sponsorships list', async () => {
|
||||
// Arrange
|
||||
const sponsorId = 'sponsor-123';
|
||||
|
||||
const mockDto: SponsorSponsorshipsDto = {
|
||||
sponsorId,
|
||||
sponsorName: 'Sponsor Alpha',
|
||||
sponsorships: [],
|
||||
};
|
||||
|
||||
const mockViewModel: SponsorSponsorshipsViewModel = {
|
||||
sponsorId,
|
||||
sponsorName: 'Sponsor Alpha',
|
||||
sponsorships: [],
|
||||
totalCount: 0,
|
||||
activeCount: 0,
|
||||
hasSponsorships: false,
|
||||
totalInvestment: 0,
|
||||
formattedTotalInvestment: 'USD 0',
|
||||
} as SponsorSponsorshipsViewModel;
|
||||
|
||||
vi.mocked(mockApiClient.getSponsorships).mockResolvedValue(mockDto);
|
||||
vi.mocked(mockSponsorSponsorshipsPresenter.present).mockReturnValue(mockViewModel);
|
||||
|
||||
// Act
|
||||
const result = await service.getSponsorSponsorships(sponsorId);
|
||||
|
||||
// Assert
|
||||
expect(mockApiClient.getSponsorships).toHaveBeenCalledWith(sponsorId);
|
||||
expect(mockSponsorSponsorshipsPresenter.present).toHaveBeenCalledWith(mockDto);
|
||||
expect(result).toEqual(mockViewModel);
|
||||
expect(result?.totalCount).toBe(0);
|
||||
});
|
||||
|
||||
it('should propagate errors from API client', async () => {
|
||||
// Arrange
|
||||
const sponsorId = 'sponsor-123';
|
||||
const error = new Error('Failed to fetch sponsorships');
|
||||
vi.mocked(mockApiClient.getSponsorships).mockRejectedValue(error);
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.getSponsorSponsorships(sponsorId)).rejects.toThrow('Failed to fetch sponsorships');
|
||||
expect(mockApiClient.getSponsorships).toHaveBeenCalledWith(sponsorId);
|
||||
expect(mockSponsorSponsorshipsPresenter.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should handle multiple sponsorship tiers', async () => {
|
||||
// Arrange
|
||||
const sponsorId = 'sponsor-456';
|
||||
|
||||
const mockDto: SponsorSponsorshipsDto = {
|
||||
sponsorId,
|
||||
sponsorName: 'Sponsor Beta',
|
||||
sponsorships: [
|
||||
{
|
||||
id: 'sponsorship-1',
|
||||
leagueId: 'league-1',
|
||||
leagueName: 'League One',
|
||||
seasonId: 'season-1',
|
||||
tier: 'main',
|
||||
status: 'active',
|
||||
amount: 10000,
|
||||
currency: 'USD',
|
||||
},
|
||||
{
|
||||
id: 'sponsorship-2',
|
||||
leagueId: 'league-2',
|
||||
leagueName: 'League Two',
|
||||
seasonId: 'season-2',
|
||||
tier: 'secondary',
|
||||
status: 'pending',
|
||||
amount: 5000,
|
||||
currency: 'USD',
|
||||
},
|
||||
{
|
||||
id: 'sponsorship-3',
|
||||
leagueId: 'league-3',
|
||||
leagueName: 'League Three',
|
||||
seasonId: 'season-3',
|
||||
tier: 'main',
|
||||
status: 'expired',
|
||||
amount: 10000,
|
||||
currency: 'USD',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const mockViewModel: SponsorSponsorshipsViewModel = {
|
||||
sponsorId,
|
||||
sponsorName: 'Sponsor Beta',
|
||||
sponsorships: [],
|
||||
totalCount: 3,
|
||||
activeCount: 1,
|
||||
hasSponsorships: true,
|
||||
totalInvestment: 25000,
|
||||
formattedTotalInvestment: 'USD 25,000',
|
||||
} as SponsorSponsorshipsViewModel;
|
||||
|
||||
vi.mocked(mockApiClient.getSponsorships).mockResolvedValue(mockDto);
|
||||
vi.mocked(mockSponsorSponsorshipsPresenter.present).mockReturnValue(mockViewModel);
|
||||
|
||||
// Act
|
||||
const result = await service.getSponsorSponsorships(sponsorId);
|
||||
|
||||
// Assert
|
||||
expect(mockApiClient.getSponsorships).toHaveBeenCalledWith(sponsorId);
|
||||
expect(mockSponsorSponsorshipsPresenter.present).toHaveBeenCalledWith(mockDto);
|
||||
expect(result).toEqual(mockViewModel);
|
||||
expect(result?.totalCount).toBe(3);
|
||||
expect(result?.activeCount).toBe(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,44 +1,40 @@
|
||||
import type { SponsorsApiClient } from '../../api/sponsors/SponsorsApiClient';
|
||||
import type { SponsorshipPricingPresenter } from '../../presenters/SponsorshipPricingPresenter';
|
||||
import type { SponsorSponsorshipsPresenter } from '../../presenters/SponsorSponsorshipsPresenter';
|
||||
import type {
|
||||
import {
|
||||
SponsorshipPricingViewModel,
|
||||
SponsorSponsorshipsViewModel
|
||||
} from '../../view-models';
|
||||
import type {
|
||||
GetEntitySponsorshipPricingResultDto,
|
||||
SponsorSponsorshipsDto
|
||||
} from '../../dtos';
|
||||
import type { SponsorSponsorshipsDTO } from '../../types/generated';
|
||||
|
||||
// TODO: Move these types to apps/website/lib/types/generated when available
|
||||
type GetEntitySponsorshipPricingResultDto = { pricing: Array<{ entityType: string; price: number }> };
|
||||
|
||||
/**
|
||||
* Sponsorship Service
|
||||
*
|
||||
* Orchestrates sponsorship operations by coordinating API calls and presentation logic.
|
||||
* Orchestrates sponsorship operations by coordinating API calls and view model creation.
|
||||
* All dependencies are injected via constructor.
|
||||
*/
|
||||
export class SponsorshipService {
|
||||
constructor(
|
||||
private readonly apiClient: SponsorsApiClient,
|
||||
private readonly sponsorshipPricingPresenter: SponsorshipPricingPresenter,
|
||||
private readonly sponsorSponsorshipsPresenter: SponsorSponsorshipsPresenter
|
||||
private readonly apiClient: SponsorsApiClient
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Get sponsorship pricing with presentation transformation
|
||||
* Get sponsorship pricing with view model transformation
|
||||
*/
|
||||
async getSponsorshipPricing(): Promise<SponsorshipPricingViewModel> {
|
||||
const dto = await this.apiClient.getPricing();
|
||||
return this.sponsorshipPricingPresenter.present(dto);
|
||||
return new SponsorshipPricingViewModel(dto);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get sponsor sponsorships with presentation transformation
|
||||
* Get sponsor sponsorships with view model transformation
|
||||
*/
|
||||
async getSponsorSponsorships(sponsorId: string): Promise<SponsorSponsorshipsViewModel | null> {
|
||||
const dto = await this.apiClient.getSponsorships(sponsorId);
|
||||
if (!dto) {
|
||||
return null;
|
||||
}
|
||||
return this.sponsorSponsorshipsPresenter.present(dto);
|
||||
return new SponsorSponsorshipsViewModel(dto);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user