refactor dashboard module
This commit is contained in:
@@ -2,8 +2,6 @@ import { Provider } from '@nestjs/common';
|
||||
|
||||
// Import core interfaces
|
||||
import type { Logger } from '@core/shared/application/Logger';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
import type { DashboardOverviewResult } from '@core/racing/application/use-cases/DashboardOverviewUseCase';
|
||||
import { IDriverRepository } from '@core/racing/domain/repositories/IDriverRepository';
|
||||
import { IRaceRepository } from '@core/racing/domain/repositories/IRaceRepository';
|
||||
import { IResultRepository } from '@core/racing/domain/repositories/IResultRepository';
|
||||
@@ -141,7 +139,6 @@ export const DashboardProviders: Provider[] = [
|
||||
feedRepo: IFeedRepository,
|
||||
socialRepo: ISocialGraphRepository,
|
||||
imageService: ImageServicePort,
|
||||
output: UseCaseOutputPort<DashboardOverviewResult>,
|
||||
) =>
|
||||
new DashboardOverviewUseCase(
|
||||
driverRepo,
|
||||
@@ -155,7 +152,6 @@ export const DashboardProviders: Provider[] = [
|
||||
socialRepo,
|
||||
async (driverId: string) => imageService.getDriverAvatar(driverId),
|
||||
() => null,
|
||||
output,
|
||||
),
|
||||
inject: [
|
||||
DRIVER_REPOSITORY_TOKEN,
|
||||
@@ -168,7 +164,6 @@ export const DashboardProviders: Provider[] = [
|
||||
FEED_REPOSITORY_TOKEN,
|
||||
SOCIAL_GRAPH_REPOSITORY_TOKEN,
|
||||
IMAGE_SERVICE_TOKEN,
|
||||
DASHBOARD_OVERVIEW_OUTPUT_PORT_TOKEN,
|
||||
],
|
||||
},
|
||||
];
|
||||
@@ -7,14 +7,15 @@ import { DashboardOverviewPresenter } from './presenters/DashboardOverviewPresen
|
||||
import type { Logger } from '@core/shared/application/Logger';
|
||||
|
||||
// Tokens
|
||||
import { DASHBOARD_OVERVIEW_OUTPUT_PORT_TOKEN, DASHBOARD_OVERVIEW_USE_CASE_TOKEN, LOGGER_TOKEN } from './DashboardProviders';
|
||||
import { DASHBOARD_OVERVIEW_USE_CASE_TOKEN, LOGGER_TOKEN } from './DashboardProviders';
|
||||
|
||||
@Injectable()
|
||||
export class DashboardService {
|
||||
private readonly presenter = new DashboardOverviewPresenter();
|
||||
|
||||
constructor(
|
||||
@Inject(LOGGER_TOKEN) private readonly logger: Logger,
|
||||
@Inject(DASHBOARD_OVERVIEW_USE_CASE_TOKEN) private readonly dashboardOverviewUseCase: DashboardOverviewUseCase,
|
||||
@Inject(DASHBOARD_OVERVIEW_OUTPUT_PORT_TOKEN) private readonly dashboardOverviewPresenter: DashboardOverviewPresenter, // TODO no presenter injection
|
||||
) {}
|
||||
|
||||
async getDashboardOverview(driverId: string): Promise<DashboardOverviewDTO> {
|
||||
@@ -26,6 +27,8 @@ export class DashboardService {
|
||||
throw new Error(result.unwrapErr().details?.message ?? 'Failed to get dashboard overview');
|
||||
}
|
||||
|
||||
return this.dashboardOverviewPresenter.getResponseModel();
|
||||
this.presenter.present(result);
|
||||
|
||||
return this.presenter.getResponseModel();
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import { Race } from '@core/racing/domain/entities/Race';
|
||||
import { Standing } from '@core/racing/domain/entities/Standing';
|
||||
import { Result as RaceResult } from '@core/racing/domain/entities/result/Result';
|
||||
import type { FeedItem } from '@core/social/domain/types/FeedItem';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import { beforeEach, describe, expect, it } from 'vitest';
|
||||
import { DashboardOverviewPresenter } from './DashboardOverviewPresenter';
|
||||
|
||||
@@ -143,7 +144,7 @@ describe('DashboardOverviewPresenter', () => {
|
||||
it('maps DashboardOverviewResult to DashboardOverviewDTO correctly', () => {
|
||||
const output = createOutput();
|
||||
|
||||
presenter.present(output);
|
||||
presenter.present(Result.ok(output));
|
||||
const dto = presenter.getResponseModel();
|
||||
|
||||
expect(dto.activeLeaguesCount).toBe(2);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type {
|
||||
DashboardOverviewResult,
|
||||
} from '@core/racing/application/use-cases/DashboardOverviewUseCase';
|
||||
@@ -13,22 +13,24 @@ import {
|
||||
DashboardFriendSummaryDTO,
|
||||
} from '../dtos/DashboardOverviewDTO';
|
||||
|
||||
export class DashboardOverviewPresenter implements UseCaseOutputPort<DashboardOverviewResult> {
|
||||
export class DashboardOverviewPresenter {
|
||||
private responseModel: DashboardOverviewDTO | null = null;
|
||||
|
||||
present(result: DashboardOverviewResult): void {
|
||||
const currentDriver: DashboardDriverSummaryDTO | null = result.currentDriver
|
||||
present(result: Result<DashboardOverviewResult, unknown>): void {
|
||||
const data = result.unwrap();
|
||||
|
||||
const currentDriver: DashboardDriverSummaryDTO | null = data.currentDriver
|
||||
? {
|
||||
id: result.currentDriver.driver.id,
|
||||
name: String(result.currentDriver.driver.name),
|
||||
country: String(result.currentDriver.driver.country),
|
||||
avatarUrl: result.currentDriver.avatarUrl,
|
||||
rating: result.currentDriver.rating,
|
||||
globalRank: result.currentDriver.globalRank,
|
||||
totalRaces: result.currentDriver.totalRaces,
|
||||
wins: result.currentDriver.wins,
|
||||
podiums: result.currentDriver.podiums,
|
||||
consistency: result.currentDriver.consistency,
|
||||
id: data.currentDriver.driver.id,
|
||||
name: String(data.currentDriver.driver.name),
|
||||
country: String(data.currentDriver.driver.country),
|
||||
avatarUrl: data.currentDriver.avatarUrl,
|
||||
rating: data.currentDriver.rating,
|
||||
globalRank: data.currentDriver.globalRank,
|
||||
totalRaces: data.currentDriver.totalRaces,
|
||||
wins: data.currentDriver.wins,
|
||||
podiums: data.currentDriver.podiums,
|
||||
consistency: data.currentDriver.consistency,
|
||||
}
|
||||
: null;
|
||||
|
||||
@@ -43,13 +45,13 @@ export class DashboardOverviewPresenter implements UseCaseOutputPort<DashboardOv
|
||||
isMyLeague: raceSummary.isMyLeague,
|
||||
});
|
||||
|
||||
const myUpcomingRaces: DashboardRaceSummaryDTO[] = result.myUpcomingRaces.map(mapRace);
|
||||
const otherUpcomingRaces: DashboardRaceSummaryDTO[] = result.otherUpcomingRaces.map(mapRace);
|
||||
const upcomingRaces: DashboardRaceSummaryDTO[] = result.upcomingRaces.map(mapRace);
|
||||
const myUpcomingRaces: DashboardRaceSummaryDTO[] = data.myUpcomingRaces.map(mapRace);
|
||||
const otherUpcomingRaces: DashboardRaceSummaryDTO[] = data.otherUpcomingRaces.map(mapRace);
|
||||
const upcomingRaces: DashboardRaceSummaryDTO[] = data.upcomingRaces.map(mapRace);
|
||||
|
||||
const nextRace: DashboardRaceSummaryDTO | null = result.nextRace ? mapRace(result.nextRace) : null;
|
||||
const nextRace: DashboardRaceSummaryDTO | null = data.nextRace ? mapRace(data.nextRace) : null;
|
||||
|
||||
const recentResults: DashboardRecentResultDTO[] = result.recentResults.map(resultSummary => ({
|
||||
const recentResults: DashboardRecentResultDTO[] = data.recentResults.map(resultSummary => ({
|
||||
raceId: resultSummary.race.id,
|
||||
raceName: String(resultSummary.race.track),
|
||||
leagueId: resultSummary.league?.id ? String(resultSummary.league.id) : null,
|
||||
@@ -60,7 +62,7 @@ export class DashboardOverviewPresenter implements UseCaseOutputPort<DashboardOv
|
||||
}));
|
||||
|
||||
const leagueStandingsSummaries: DashboardLeagueStandingSummaryDTO[] =
|
||||
result.leagueStandingsSummaries.map(standing => ({
|
||||
data.leagueStandingsSummaries.map(standing => ({
|
||||
leagueId: String(standing.league.id),
|
||||
leagueName: String(standing.league.name),
|
||||
position: standing.standing?.position ? Number(standing.standing.position) : null,
|
||||
@@ -68,7 +70,7 @@ export class DashboardOverviewPresenter implements UseCaseOutputPort<DashboardOv
|
||||
points: standing.standing?.points ? Number(standing.standing.points) : null,
|
||||
}));
|
||||
|
||||
const feedItems: DashboardFeedItemSummaryDTO[] = result.feedSummary.items.map(item => ({
|
||||
const feedItems: DashboardFeedItemSummaryDTO[] = data.feedSummary.items.map(item => ({
|
||||
id: item.id,
|
||||
type: String(item.type),
|
||||
headline: item.headline,
|
||||
@@ -79,11 +81,11 @@ export class DashboardOverviewPresenter implements UseCaseOutputPort<DashboardOv
|
||||
}));
|
||||
|
||||
const feedSummary: DashboardFeedSummaryDTO = {
|
||||
notificationCount: result.feedSummary.notificationCount,
|
||||
notificationCount: data.feedSummary.notificationCount,
|
||||
items: feedItems,
|
||||
};
|
||||
|
||||
const friends: DashboardFriendSummaryDTO[] = result.friends.map(friend => ({
|
||||
const friends: DashboardFriendSummaryDTO[] = data.friends.map(friend => ({
|
||||
id: friend.driver.id,
|
||||
name: String(friend.driver.name),
|
||||
country: String(friend.driver.country),
|
||||
@@ -95,7 +97,7 @@ export class DashboardOverviewPresenter implements UseCaseOutputPort<DashboardOv
|
||||
myUpcomingRaces,
|
||||
otherUpcomingRaces,
|
||||
upcomingRaces,
|
||||
activeLeaguesCount: result.activeLeaguesCount,
|
||||
activeLeaguesCount: data.activeLeaguesCount,
|
||||
nextRace,
|
||||
recentResults,
|
||||
leagueStandingsSummaries,
|
||||
|
||||
Reference in New Issue
Block a user