view models

This commit is contained in:
2025-12-18 13:48:35 +01:00
parent cc2553876a
commit 91adbb9c83
71 changed files with 3119 additions and 359 deletions

View File

@@ -1,8 +1,8 @@
import { describe, it, expect, vi, Mocked } from 'vitest';
import { AnalyticsService } from './AnalyticsService';
import { AnalyticsApiClient } from '../../api/analytics/AnalyticsApiClient';
import { RecordPageViewInputViewModel } from '../../view-models/RecordPageViewInputViewModel';
import { RecordEngagementInputViewModel } from '../../view-models/RecordEngagementInputViewModel';
import { RecordPageViewOutputViewModel } from '../../view-models/RecordPageViewOutputViewModel';
import { RecordEngagementOutputViewModel } from '../../view-models/RecordEngagementOutputViewModel';
describe('AnalyticsService', () => {
let mockApiClient: Mocked<AnalyticsApiClient>;
@@ -19,10 +19,10 @@ describe('AnalyticsService', () => {
describe('recordPageView', () => {
it('should call apiClient.recordPageView with correct input', async () => {
const input = new RecordPageViewInputViewModel({
const input = {
path: '/dashboard',
userId: 'user-123',
});
};
const expectedOutput = { pageViewId: 'pv-123' };
mockApiClient.recordPageView.mockResolvedValue(expectedOutput);
@@ -33,13 +33,14 @@ describe('AnalyticsService', () => {
path: '/dashboard',
userId: 'user-123',
});
expect(result).toEqual(expectedOutput);
expect(result).toBeInstanceOf(RecordPageViewOutputViewModel);
expect(result.pageViewId).toEqual('pv-123');
});
it('should call apiClient.recordPageView without userId when not provided', async () => {
const input = new RecordPageViewInputViewModel({
const input = {
path: '/home',
});
};
const expectedOutput = { pageViewId: 'pv-456' };
mockApiClient.recordPageView.mockResolvedValue(expectedOutput);
@@ -49,17 +50,18 @@ describe('AnalyticsService', () => {
expect(mockApiClient.recordPageView).toHaveBeenCalledWith({
path: '/home',
});
expect(result).toEqual(expectedOutput);
expect(result).toBeInstanceOf(RecordPageViewOutputViewModel);
expect(result.pageViewId).toEqual('pv-456');
});
});
describe('recordEngagement', () => {
it('should call apiClient.recordEngagement with correct input', async () => {
const input = new RecordEngagementInputViewModel({
const input = {
eventType: 'button_click',
userId: 'user-123',
metadata: { buttonId: 'submit', page: '/form' },
});
};
const expectedOutput = { eventId: 'event-123', engagementWeight: 1.5 };
mockApiClient.recordEngagement.mockResolvedValue(expectedOutput);
@@ -71,13 +73,15 @@ describe('AnalyticsService', () => {
userId: 'user-123',
metadata: { buttonId: 'submit', page: '/form' },
});
expect(result).toEqual(expectedOutput);
expect(result).toBeInstanceOf(RecordEngagementOutputViewModel);
expect(result.eventId).toEqual('event-123');
expect(result.engagementWeight).toEqual(1.5);
});
it('should call apiClient.recordEngagement without optional fields', async () => {
const input = new RecordEngagementInputViewModel({
const input = {
eventType: 'page_load',
});
};
const expectedOutput = { eventId: 'event-456', engagementWeight: 0.5 };
mockApiClient.recordEngagement.mockResolvedValue(expectedOutput);
@@ -87,7 +91,9 @@ describe('AnalyticsService', () => {
expect(mockApiClient.recordEngagement).toHaveBeenCalledWith({
eventType: 'page_load',
});
expect(result).toEqual(expectedOutput);
expect(result).toBeInstanceOf(RecordEngagementOutputViewModel);
expect(result.eventId).toEqual('event-456');
expect(result.engagementWeight).toEqual(0.5);
});
});
});

View File

@@ -1,7 +1,18 @@
import { AnalyticsApiClient } from '../../api/analytics/AnalyticsApiClient';
import { RecordPageViewOutputDTO, RecordEngagementOutputDTO } from '../../types/generated';
import { RecordPageViewInputViewModel } from '../../view-models/RecordPageViewInputViewModel';
import { RecordEngagementInputViewModel } from '../../view-models/RecordEngagementInputViewModel';
import { RecordPageViewOutputViewModel } from '../../view-models/RecordPageViewOutputViewModel';
import { RecordEngagementOutputViewModel } from '../../view-models/RecordEngagementOutputViewModel';
// TODO: Create proper DTOs in generated types
interface RecordPageViewInputDTO {
path: string;
userId?: string;
}
interface RecordEngagementInputDTO {
eventType: string;
userId?: string;
metadata?: Record<string, unknown>;
}
/**
* Analytics Service
@@ -15,31 +26,18 @@ export class AnalyticsService {
) {}
/**
* Record a page view
*/
async recordPageView(input: RecordPageViewInputViewModel): Promise<RecordPageViewOutputDTO> {
const apiInput: { path: string; userId?: string } = {
path: input.path,
};
if (input.userId) {
apiInput.userId = input.userId;
}
return await this.apiClient.recordPageView(apiInput);
}
* Record a page view
*/
async recordPageView(input: RecordPageViewInputDTO): Promise<RecordPageViewOutputViewModel> {
const result = await this.apiClient.recordPageView(input);
return new RecordPageViewOutputViewModel(result);
}
/**
* Record an engagement event
*/
async recordEngagement(input: RecordEngagementInputViewModel): Promise<RecordEngagementOutputDTO> {
const apiInput: { eventType: string; userId?: string; metadata?: Record<string, unknown> } = {
eventType: input.eventType,
};
if (input.userId) {
apiInput.userId = input.userId;
}
if (input.metadata) {
apiInput.metadata = input.metadata;
}
return await this.apiClient.recordEngagement(apiInput);
}
* Record an engagement event
*/
async recordEngagement(input: RecordEngagementInputDTO): Promise<RecordEngagementOutputViewModel> {
const result = await this.apiClient.recordEngagement(input);
return new RecordEngagementOutputViewModel(result);
}
}

View File

@@ -60,21 +60,4 @@ describe('DashboardService', () => {
});
});
describe('getDashboardOverview', () => {
it('should call getDashboardData and return the result', async () => {
const dto = {
totalUsers: 200,
activeUsers: 100,
totalRaces: 40,
totalLeagues: 10,
};
mockApiClient.getDashboardData.mockResolvedValue(dto);
const result = await service.getDashboardOverview();
expect(mockApiClient.getDashboardData).toHaveBeenCalled();
expect(result).toBeInstanceOf(AnalyticsDashboardViewModel);
expect(result.totalUsers).toBe(200);
});
});
});

View File

@@ -1,5 +1,6 @@
import { AnalyticsDashboardViewModel } from '@/lib/view-models/AnalyticsDashboardViewModel';
import { AnalyticsMetricsViewModel } from '@/lib/view-models/AnalyticsMetricsViewModel';
import { AnalyticsApiClient } from '../../api/analytics/AnalyticsApiClient';
import { AnalyticsDashboardViewModel, AnalyticsMetricsViewModel } from '../../view-models';
/**
* Dashboard Service