integration tests
This commit is contained in:
64
core/dashboard/application/dto/DashboardDTO.ts
Normal file
64
core/dashboard/application/dto/DashboardDTO.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
/**
|
||||
* Dashboard DTO (Data Transfer Object)
|
||||
*
|
||||
* Represents the complete dashboard data structure returned to the client.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Driver statistics section
|
||||
*/
|
||||
export interface DriverStatisticsDTO {
|
||||
rating: number;
|
||||
rank: number;
|
||||
starts: number;
|
||||
wins: number;
|
||||
podiums: number;
|
||||
leagues: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Upcoming race section
|
||||
*/
|
||||
export interface UpcomingRaceDTO {
|
||||
trackName: string;
|
||||
carType: string;
|
||||
scheduledDate: string;
|
||||
timeUntilRace: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Championship standing section
|
||||
*/
|
||||
export interface ChampionshipStandingDTO {
|
||||
leagueName: string;
|
||||
position: number;
|
||||
points: number;
|
||||
totalDrivers: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recent activity section
|
||||
*/
|
||||
export interface RecentActivityDTO {
|
||||
type: 'race_result' | 'league_invitation' | 'achievement' | 'other';
|
||||
description: string;
|
||||
timestamp: string;
|
||||
status: 'success' | 'info' | 'warning' | 'error';
|
||||
}
|
||||
|
||||
/**
|
||||
* Dashboard DTO
|
||||
*
|
||||
* Complete dashboard data structure for a driver.
|
||||
*/
|
||||
export interface DashboardDTO {
|
||||
driver: {
|
||||
id: string;
|
||||
name: string;
|
||||
avatar?: string;
|
||||
};
|
||||
statistics: DriverStatisticsDTO;
|
||||
upcomingRaces: UpcomingRaceDTO[];
|
||||
championshipStandings: ChampionshipStandingDTO[];
|
||||
recentActivity: RecentActivityDTO[];
|
||||
}
|
||||
43
core/dashboard/application/ports/DashboardEventPublisher.ts
Normal file
43
core/dashboard/application/ports/DashboardEventPublisher.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* Dashboard Event Publisher Port
|
||||
*
|
||||
* Defines the interface for publishing dashboard-related events.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Dashboard accessed event
|
||||
*/
|
||||
export interface DashboardAccessedEvent {
|
||||
type: 'dashboard_accessed';
|
||||
driverId: string;
|
||||
timestamp: Date;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dashboard error event
|
||||
*/
|
||||
export interface DashboardErrorEvent {
|
||||
type: 'dashboard_error';
|
||||
driverId: string;
|
||||
error: string;
|
||||
timestamp: Date;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dashboard Event Publisher Interface
|
||||
*
|
||||
* Publishes events related to dashboard operations.
|
||||
*/
|
||||
export interface DashboardEventPublisher {
|
||||
/**
|
||||
* Publish a dashboard accessed event
|
||||
* @param event - The event to publish
|
||||
*/
|
||||
publishDashboardAccessed(event: DashboardAccessedEvent): Promise<void>;
|
||||
|
||||
/**
|
||||
* Publish a dashboard error event
|
||||
* @param event - The event to publish
|
||||
*/
|
||||
publishDashboardError(event: DashboardErrorEvent): Promise<void>;
|
||||
}
|
||||
9
core/dashboard/application/ports/DashboardQuery.ts
Normal file
9
core/dashboard/application/ports/DashboardQuery.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* Dashboard Query
|
||||
*
|
||||
* Query object for fetching dashboard data.
|
||||
*/
|
||||
|
||||
export interface DashboardQuery {
|
||||
driverId: string;
|
||||
}
|
||||
107
core/dashboard/application/ports/DashboardRepository.ts
Normal file
107
core/dashboard/application/ports/DashboardRepository.ts
Normal file
@@ -0,0 +1,107 @@
|
||||
/**
|
||||
* Dashboard Repository Port
|
||||
*
|
||||
* Defines the interface for accessing dashboard-related data.
|
||||
* This is a read-only repository for dashboard data aggregation.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Driver data for dashboard display
|
||||
*/
|
||||
export interface DriverData {
|
||||
id: string;
|
||||
name: string;
|
||||
avatar?: string;
|
||||
rating: number;
|
||||
rank: number;
|
||||
starts: number;
|
||||
wins: number;
|
||||
podiums: number;
|
||||
leagues: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Race data for upcoming races section
|
||||
*/
|
||||
export interface RaceData {
|
||||
id: string;
|
||||
trackName: string;
|
||||
carType: string;
|
||||
scheduledDate: Date;
|
||||
timeUntilRace?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* League standing data for championship standings section
|
||||
*/
|
||||
export interface LeagueStandingData {
|
||||
leagueId: string;
|
||||
leagueName: string;
|
||||
position: number;
|
||||
points: number;
|
||||
totalDrivers: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Activity data for recent activity feed
|
||||
*/
|
||||
export interface ActivityData {
|
||||
id: string;
|
||||
type: 'race_result' | 'league_invitation' | 'achievement' | 'other';
|
||||
description: string;
|
||||
timestamp: Date;
|
||||
status: 'success' | 'info' | 'warning' | 'error';
|
||||
}
|
||||
|
||||
/**
|
||||
* Friend data for social section
|
||||
*/
|
||||
export interface FriendData {
|
||||
id: string;
|
||||
name: string;
|
||||
avatar?: string;
|
||||
rating: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dashboard Repository Interface
|
||||
*
|
||||
* Provides access to all data needed for the dashboard.
|
||||
* Each method returns data for a specific driver.
|
||||
*/
|
||||
export interface DashboardRepository {
|
||||
/**
|
||||
* Find a driver by ID
|
||||
* @param driverId - The driver ID
|
||||
* @returns Driver data or null if not found
|
||||
*/
|
||||
findDriverById(driverId: string): Promise<DriverData | null>;
|
||||
|
||||
/**
|
||||
* Get upcoming races for a driver
|
||||
* @param driverId - The driver ID
|
||||
* @returns Array of upcoming races
|
||||
*/
|
||||
getUpcomingRaces(driverId: string): Promise<RaceData[]>;
|
||||
|
||||
/**
|
||||
* Get league standings for a driver
|
||||
* @param driverId - The driver ID
|
||||
* @returns Array of league standings
|
||||
*/
|
||||
getLeagueStandings(driverId: string): Promise<LeagueStandingData[]>;
|
||||
|
||||
/**
|
||||
* Get recent activity for a driver
|
||||
* @param driverId - The driver ID
|
||||
* @returns Array of recent activities
|
||||
*/
|
||||
getRecentActivity(driverId: string): Promise<ActivityData[]>;
|
||||
|
||||
/**
|
||||
* Get friends for a driver
|
||||
* @param driverId - The driver ID
|
||||
* @returns Array of friends
|
||||
*/
|
||||
getFriends(driverId: string): Promise<FriendData[]>;
|
||||
}
|
||||
18
core/dashboard/application/presenters/DashboardPresenter.ts
Normal file
18
core/dashboard/application/presenters/DashboardPresenter.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* Dashboard Presenter
|
||||
*
|
||||
* Transforms dashboard data into DTO format for presentation.
|
||||
*/
|
||||
|
||||
import { DashboardDTO } from '../dto/DashboardDTO';
|
||||
|
||||
export class DashboardPresenter {
|
||||
/**
|
||||
* Present dashboard data as DTO
|
||||
* @param data - Dashboard data
|
||||
* @returns Dashboard DTO
|
||||
*/
|
||||
present(data: DashboardDTO): DashboardDTO {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
130
core/dashboard/application/use-cases/GetDashboardUseCase.ts
Normal file
130
core/dashboard/application/use-cases/GetDashboardUseCase.ts
Normal file
@@ -0,0 +1,130 @@
|
||||
/**
|
||||
* Get Dashboard Use Case
|
||||
*
|
||||
* Orchestrates the retrieval of dashboard data for a driver.
|
||||
* Aggregates data from multiple repositories and returns a unified dashboard view.
|
||||
*/
|
||||
|
||||
import { DashboardRepository } from '../ports/DashboardRepository';
|
||||
import { DashboardQuery } from '../ports/DashboardQuery';
|
||||
import { DashboardDTO } from '../dto/DashboardDTO';
|
||||
import { DashboardEventPublisher } from '../ports/DashboardEventPublisher';
|
||||
import { DriverNotFoundError } from '../../domain/errors/DriverNotFoundError';
|
||||
import { ValidationError } from '../../../shared/errors/ValidationError';
|
||||
|
||||
export interface GetDashboardUseCasePorts {
|
||||
driverRepository: DashboardRepository;
|
||||
raceRepository: DashboardRepository;
|
||||
leagueRepository: DashboardRepository;
|
||||
activityRepository: DashboardRepository;
|
||||
eventPublisher: DashboardEventPublisher;
|
||||
}
|
||||
|
||||
export class GetDashboardUseCase {
|
||||
constructor(private readonly ports: GetDashboardUseCasePorts) {}
|
||||
|
||||
async execute(query: DashboardQuery): Promise<DashboardDTO> {
|
||||
// Validate input
|
||||
this.validateQuery(query);
|
||||
|
||||
// Find driver
|
||||
const driver = await this.ports.driverRepository.findDriverById(query.driverId);
|
||||
if (!driver) {
|
||||
throw new DriverNotFoundError(query.driverId);
|
||||
}
|
||||
|
||||
// Fetch all data in parallel
|
||||
const [upcomingRaces, leagueStandings, recentActivity] = await Promise.all([
|
||||
this.ports.raceRepository.getUpcomingRaces(query.driverId),
|
||||
this.ports.leagueRepository.getLeagueStandings(query.driverId),
|
||||
this.ports.activityRepository.getRecentActivity(query.driverId),
|
||||
]);
|
||||
|
||||
// Limit upcoming races to 3
|
||||
const limitedRaces = upcomingRaces
|
||||
.sort((a, b) => a.scheduledDate.getTime() - b.scheduledDate.getTime())
|
||||
.slice(0, 3);
|
||||
|
||||
// Sort recent activity by timestamp (newest first)
|
||||
const sortedActivity = recentActivity
|
||||
.sort((a, b) => b.timestamp.getTime() - a.timestamp.getTime());
|
||||
|
||||
// Transform to DTO
|
||||
const driverDto: DashboardDTO['driver'] = {
|
||||
id: driver.id,
|
||||
name: driver.name,
|
||||
};
|
||||
if (driver.avatar) {
|
||||
driverDto.avatar = driver.avatar;
|
||||
}
|
||||
|
||||
const result: DashboardDTO = {
|
||||
driver: driverDto,
|
||||
statistics: {
|
||||
rating: driver.rating,
|
||||
rank: driver.rank,
|
||||
starts: driver.starts,
|
||||
wins: driver.wins,
|
||||
podiums: driver.podiums,
|
||||
leagues: driver.leagues,
|
||||
},
|
||||
upcomingRaces: limitedRaces.map(race => ({
|
||||
trackName: race.trackName,
|
||||
carType: race.carType,
|
||||
scheduledDate: race.scheduledDate.toISOString(),
|
||||
timeUntilRace: race.timeUntilRace || this.calculateTimeUntilRace(race.scheduledDate),
|
||||
})),
|
||||
championshipStandings: leagueStandings.map(standing => ({
|
||||
leagueName: standing.leagueName,
|
||||
position: standing.position,
|
||||
points: standing.points,
|
||||
totalDrivers: standing.totalDrivers,
|
||||
})),
|
||||
recentActivity: sortedActivity.map(activity => ({
|
||||
type: activity.type,
|
||||
description: activity.description,
|
||||
timestamp: activity.timestamp.toISOString(),
|
||||
status: activity.status,
|
||||
})),
|
||||
};
|
||||
|
||||
// Publish event
|
||||
await this.ports.eventPublisher.publishDashboardAccessed({
|
||||
type: 'dashboard_accessed',
|
||||
driverId: query.driverId,
|
||||
timestamp: new Date(),
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private validateQuery(query: DashboardQuery): void {
|
||||
if (!query.driverId || typeof query.driverId !== 'string') {
|
||||
throw new ValidationError('Driver ID must be a valid string');
|
||||
}
|
||||
if (query.driverId.trim().length === 0) {
|
||||
throw new ValidationError('Driver ID cannot be empty');
|
||||
}
|
||||
}
|
||||
|
||||
private calculateTimeUntilRace(scheduledDate: Date): string {
|
||||
const now = new Date();
|
||||
const diff = scheduledDate.getTime() - now.getTime();
|
||||
|
||||
if (diff <= 0) {
|
||||
return 'Race started';
|
||||
}
|
||||
|
||||
const days = Math.floor(diff / (1000 * 60 * 60 * 24));
|
||||
const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
|
||||
const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
|
||||
|
||||
if (days > 0) {
|
||||
return `${days} day${days > 1 ? 's' : ''} ${hours} hour${hours > 1 ? 's' : ''}`;
|
||||
}
|
||||
if (hours > 0) {
|
||||
return `${hours} hour${hours > 1 ? 's' : ''} ${minutes} minute${minutes > 1 ? 's' : ''}`;
|
||||
}
|
||||
return `${minutes} minute${minutes > 1 ? 's' : ''}`;
|
||||
}
|
||||
}
|
||||
16
core/dashboard/domain/errors/DriverNotFoundError.ts
Normal file
16
core/dashboard/domain/errors/DriverNotFoundError.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* Driver Not Found Error
|
||||
*
|
||||
* Thrown when a driver with the specified ID cannot be found.
|
||||
*/
|
||||
|
||||
export class DriverNotFoundError extends Error {
|
||||
readonly type = 'domain';
|
||||
readonly context = 'dashboard';
|
||||
readonly kind = 'not_found';
|
||||
|
||||
constructor(driverId: string) {
|
||||
super(`Driver with ID "${driverId}" not found`);
|
||||
this.name = 'DriverNotFoundError';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user