refactor analytics module
This commit is contained in:
@@ -1,12 +1,10 @@
|
||||
import { Provider } from '@nestjs/common';
|
||||
import { AnalyticsService } from './AnalyticsService';
|
||||
import type { IPageViewRepository } from '@core/analytics/domain/repositories/IPageViewRepository';
|
||||
import type { IEngagementRepository } from '@core/analytics/domain/repositories/IEngagementRepository';
|
||||
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
|
||||
import type { RecordPageViewOutput } from '@core/analytics/application/use-cases/RecordPageViewUseCase';
|
||||
import type { RecordEngagementOutput } from '@core/analytics/application/use-cases/RecordEngagementUseCase';
|
||||
import type { GetDashboardDataOutput } from '@core/analytics/application/use-cases/GetDashboardDataUseCase';
|
||||
import type { GetAnalyticsMetricsOutput } from '@core/analytics/application/use-cases/GetAnalyticsMetricsUseCase';
|
||||
import type { RecordEngagementOutput } from '@core/analytics/application/use-cases/RecordEngagementUseCase';
|
||||
import type { RecordPageViewOutput } from '@core/analytics/application/use-cases/RecordPageViewUseCase';
|
||||
import type { IEngagementRepository } from '@core/analytics/domain/repositories/IEngagementRepository';
|
||||
import type { IPageViewRepository } from '@core/analytics/domain/repositories/IPageViewRepository';
|
||||
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
|
||||
import { Provider } from '@nestjs/common';
|
||||
|
||||
const Logger_TOKEN = 'Logger_TOKEN';
|
||||
const IPAGE_VIEW_REPO_TOKEN = 'IPageViewRepository_TOKEN';
|
||||
@@ -20,21 +18,16 @@ const GET_ANALYTICS_METRICS_OUTPUT_PORT_TOKEN = 'GetAnalyticsMetricsOutputPort_T
|
||||
import { InMemoryEngagementRepository } from '@adapters/analytics/persistence/inmemory/InMemoryEngagementRepository';
|
||||
import { InMemoryPageViewRepository } from '@adapters/analytics/persistence/inmemory/InMemoryPageViewRepository';
|
||||
import { ConsoleLogger } from '@adapters/logging/ConsoleLogger';
|
||||
import { RecordPageViewUseCase } from '@core/analytics/application/use-cases/RecordPageViewUseCase';
|
||||
import { RecordEngagementUseCase } from '@core/analytics/application/use-cases/RecordEngagementUseCase';
|
||||
import { GetDashboardDataUseCase } from '@core/analytics/application/use-cases/GetDashboardDataUseCase';
|
||||
import { GetAnalyticsMetricsUseCase } from '@core/analytics/application/use-cases/GetAnalyticsMetricsUseCase';
|
||||
import { RecordPageViewPresenter } from './presenters/RecordPageViewPresenter';
|
||||
import { RecordEngagementPresenter } from './presenters/RecordEngagementPresenter';
|
||||
import { GetDashboardDataPresenter } from './presenters/GetDashboardDataPresenter';
|
||||
import { GetDashboardDataOutput, GetDashboardDataUseCase } from '@core/analytics/application/use-cases/GetDashboardDataUseCase';
|
||||
import { RecordEngagementUseCase } from '@core/analytics/application/use-cases/RecordEngagementUseCase';
|
||||
import { RecordPageViewUseCase } from '@core/analytics/application/use-cases/RecordPageViewUseCase';
|
||||
import { GetAnalyticsMetricsPresenter } from './presenters/GetAnalyticsMetricsPresenter';
|
||||
import { GetDashboardDataPresenter } from './presenters/GetDashboardDataPresenter';
|
||||
import { RecordEngagementPresenter } from './presenters/RecordEngagementPresenter';
|
||||
import { RecordPageViewPresenter } from './presenters/RecordPageViewPresenter';
|
||||
|
||||
export const AnalyticsProviders: Provider[] = [
|
||||
AnalyticsService,
|
||||
RecordPageViewPresenter,
|
||||
RecordEngagementPresenter,
|
||||
GetDashboardDataPresenter,
|
||||
GetAnalyticsMetricsPresenter,
|
||||
{
|
||||
provide: Logger_TOKEN,
|
||||
useClass: ConsoleLogger,
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
import { Injectable, Inject } from '@nestjs/common';
|
||||
import type { RecordPageViewInputDTO } from './dtos/RecordPageViewInputDTO';
|
||||
import type { RecordPageViewOutputDTO } from './dtos/RecordPageViewOutputDTO';
|
||||
import { GetAnalyticsMetricsUseCase } from '@core/analytics/application/use-cases/GetAnalyticsMetricsUseCase';
|
||||
import { GetDashboardDataUseCase } from '@core/analytics/application/use-cases/GetDashboardDataUseCase';
|
||||
import { RecordEngagementUseCase } from '@core/analytics/application/use-cases/RecordEngagementUseCase';
|
||||
import { RecordPageViewUseCase } from '@core/analytics/application/use-cases/RecordPageViewUseCase';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import type { GetAnalyticsMetricsOutputDTO } from './dtos/GetAnalyticsMetricsOutputDTO';
|
||||
import type { GetDashboardDataOutputDTO } from './dtos/GetDashboardDataOutputDTO';
|
||||
import type { RecordEngagementInputDTO } from './dtos/RecordEngagementInputDTO';
|
||||
import type { RecordEngagementOutputDTO } from './dtos/RecordEngagementOutputDTO';
|
||||
import type { GetDashboardDataOutputDTO } from './dtos/GetDashboardDataOutputDTO';
|
||||
import type { GetAnalyticsMetricsOutputDTO } from './dtos/GetAnalyticsMetricsOutputDTO';
|
||||
import { RecordPageViewUseCase } from '@core/analytics/application/use-cases/RecordPageViewUseCase';
|
||||
import { RecordEngagementUseCase } from '@core/analytics/application/use-cases/RecordEngagementUseCase';
|
||||
import { GetDashboardDataUseCase } from '@core/analytics/application/use-cases/GetDashboardDataUseCase';
|
||||
import { GetAnalyticsMetricsUseCase } from '@core/analytics/application/use-cases/GetAnalyticsMetricsUseCase';
|
||||
import { RecordPageViewPresenter } from './presenters/RecordPageViewPresenter';
|
||||
import { RecordEngagementPresenter } from './presenters/RecordEngagementPresenter';
|
||||
import { GetDashboardDataPresenter } from './presenters/GetDashboardDataPresenter';
|
||||
import type { RecordPageViewInputDTO } from './dtos/RecordPageViewInputDTO';
|
||||
import type { RecordPageViewOutputDTO } from './dtos/RecordPageViewOutputDTO';
|
||||
import { GetAnalyticsMetricsPresenter } from './presenters/GetAnalyticsMetricsPresenter';
|
||||
import { GetDashboardDataPresenter } from './presenters/GetDashboardDataPresenter';
|
||||
import { RecordEngagementPresenter } from './presenters/RecordEngagementPresenter';
|
||||
import { RecordPageViewPresenter } from './presenters/RecordPageViewPresenter';
|
||||
|
||||
type RecordPageViewInput = RecordPageViewInputDTO;
|
||||
type RecordEngagementInput = RecordEngagementInputDTO;
|
||||
@@ -24,45 +24,53 @@ export class AnalyticsService {
|
||||
@Inject(RecordEngagementUseCase) private readonly recordEngagementUseCase: RecordEngagementUseCase,
|
||||
@Inject(GetDashboardDataUseCase) private readonly getDashboardDataUseCase: GetDashboardDataUseCase,
|
||||
@Inject(GetAnalyticsMetricsUseCase) private readonly getAnalyticsMetricsUseCase: GetAnalyticsMetricsUseCase,
|
||||
private readonly recordPageViewPresenter: RecordPageViewPresenter,
|
||||
private readonly recordEngagementPresenter: RecordEngagementPresenter,
|
||||
private readonly getDashboardDataPresenter: GetDashboardDataPresenter,
|
||||
private readonly getAnalyticsMetricsPresenter: GetAnalyticsMetricsPresenter,
|
||||
) {}
|
||||
|
||||
async recordPageView(input: RecordPageViewInput): Promise<RecordPageViewOutputDTO> {
|
||||
const presenter = new RecordPageViewPresenter();
|
||||
|
||||
const result = await this.recordPageViewUseCase.execute(input);
|
||||
if (result.isErr()) {
|
||||
const error = result.unwrapErr();
|
||||
throw new Error(error.details?.message ?? 'Failed to record page view');
|
||||
}
|
||||
return this.recordPageViewPresenter.getResponseModel();
|
||||
|
||||
return presenter.responseModel;
|
||||
}
|
||||
|
||||
async recordEngagement(input: RecordEngagementInput): Promise<RecordEngagementOutputDTO> {
|
||||
const presenter = new RecordEngagementPresenter();
|
||||
|
||||
const result = await this.recordEngagementUseCase.execute(input);
|
||||
if (result.isErr()) {
|
||||
const error = result.unwrapErr();
|
||||
throw new Error(error.details?.message ?? 'Failed to record engagement');
|
||||
}
|
||||
return this.recordEngagementPresenter.getResponseModel();
|
||||
|
||||
return presenter.responseModel;
|
||||
}
|
||||
|
||||
async getDashboardData(): Promise<GetDashboardDataOutputDTO> {
|
||||
const presenter = new GetDashboardDataPresenter();
|
||||
|
||||
const result = await this.getDashboardDataUseCase.execute({});
|
||||
if (result.isErr()) {
|
||||
const error = result.unwrapErr();
|
||||
throw new Error(error.details?.message ?? 'Failed to get dashboard data');
|
||||
}
|
||||
return this.getDashboardDataPresenter.getResponseModel();
|
||||
|
||||
return presenter.responseModel;
|
||||
}
|
||||
|
||||
async getAnalyticsMetrics(): Promise<GetAnalyticsMetricsOutputDTO> {
|
||||
const presenter = new GetAnalyticsMetricsPresenter();
|
||||
|
||||
const result = await this.getAnalyticsMetricsUseCase.execute({});
|
||||
if (result.isErr()) {
|
||||
const error = result.unwrapErr();
|
||||
throw new Error(error.details?.message ?? 'Failed to get analytics metrics');
|
||||
}
|
||||
return this.getAnalyticsMetricsPresenter.getResponseModel();
|
||||
|
||||
return presenter.responseModel;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,9 +27,19 @@ describe('GetAnalyticsMetricsPresenter', () => {
|
||||
averageSessionDuration: 300,
|
||||
bounceRate: 0.4,
|
||||
});
|
||||
expect(presenter.responseModel).toEqual({
|
||||
pageViews: 1000,
|
||||
uniqueVisitors: 500,
|
||||
averageSessionDuration: 300,
|
||||
bounceRate: 0.4,
|
||||
});
|
||||
});
|
||||
|
||||
it('getResponseModel throws if not presented', () => {
|
||||
expect(() => presenter.getResponseModel()).toThrow('Presenter not presented');
|
||||
});
|
||||
|
||||
it('responseModel throws if not presented', () => {
|
||||
expect(() => presenter.responseModel).toThrow('Presenter not presented');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import type { UseCaseOutputPort } from '@core/shared/application';
|
||||
import type { GetAnalyticsMetricsOutput } from '@core/analytics/application/use-cases/GetAnalyticsMetricsUseCase';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application';
|
||||
import type { GetAnalyticsMetricsOutputDTO } from '../dtos/GetAnalyticsMetricsOutputDTO';
|
||||
|
||||
export class GetAnalyticsMetricsPresenter implements UseCaseOutputPort<GetAnalyticsMetricsOutput> {
|
||||
private responseModel: GetAnalyticsMetricsOutputDTO | null = null;
|
||||
private model: GetAnalyticsMetricsOutputDTO | null = null;
|
||||
|
||||
present(result: GetAnalyticsMetricsOutput): void {
|
||||
this.responseModel = {
|
||||
this.model = {
|
||||
pageViews: result.pageViews,
|
||||
uniqueVisitors: result.uniqueVisitors,
|
||||
averageSessionDuration: result.averageSessionDuration,
|
||||
@@ -14,8 +14,13 @@ export class GetAnalyticsMetricsPresenter implements UseCaseOutputPort<GetAnalyt
|
||||
};
|
||||
}
|
||||
|
||||
get responseModel(): GetAnalyticsMetricsOutputDTO {
|
||||
if (!this.model) throw new Error('Presenter not presented');
|
||||
return this.model;
|
||||
}
|
||||
|
||||
getResponseModel(): GetAnalyticsMetricsOutputDTO {
|
||||
if (!this.responseModel) throw new Error('Presenter not presented');
|
||||
return this.responseModel;
|
||||
if (!this.model) throw new Error('Presenter not presented');
|
||||
return this.model;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,9 +25,19 @@ describe('GetDashboardDataPresenter', () => {
|
||||
totalRaces: 20,
|
||||
totalLeagues: 5,
|
||||
});
|
||||
expect(presenter.responseModel).toEqual({
|
||||
totalUsers: 100,
|
||||
activeUsers: 50,
|
||||
totalRaces: 20,
|
||||
totalLeagues: 5,
|
||||
});
|
||||
});
|
||||
|
||||
it('getResponseModel throws if not presented', () => {
|
||||
expect(() => presenter.getResponseModel()).toThrow('Presenter not presented');
|
||||
});
|
||||
|
||||
it('responseModel throws if not presented', () => {
|
||||
expect(() => presenter.responseModel).toThrow('Presenter not presented');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import type { UseCaseOutputPort } from '@core/shared/application';
|
||||
import type { GetDashboardDataOutput } from '@core/analytics/application/use-cases/GetDashboardDataUseCase';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application';
|
||||
import type { GetDashboardDataOutputDTO } from '../dtos/GetDashboardDataOutputDTO';
|
||||
|
||||
export class GetDashboardDataPresenter implements UseCaseOutputPort<GetDashboardDataOutput> {
|
||||
private responseModel: GetDashboardDataOutputDTO | null = null;
|
||||
private model: GetDashboardDataOutputDTO | null = null;
|
||||
|
||||
present(result: GetDashboardDataOutput): void {
|
||||
this.responseModel = {
|
||||
this.model = {
|
||||
totalUsers: result.totalUsers,
|
||||
activeUsers: result.activeUsers,
|
||||
totalRaces: result.totalRaces,
|
||||
@@ -14,8 +14,13 @@ export class GetDashboardDataPresenter implements UseCaseOutputPort<GetDashboard
|
||||
};
|
||||
}
|
||||
|
||||
get responseModel(): GetDashboardDataOutputDTO {
|
||||
if (!this.model) throw new Error('Presenter not presented');
|
||||
return this.model;
|
||||
}
|
||||
|
||||
getResponseModel(): GetDashboardDataOutputDTO {
|
||||
if (!this.responseModel) throw new Error('Presenter not presented');
|
||||
return this.responseModel;
|
||||
if (!this.model) throw new Error('Presenter not presented');
|
||||
return this.model;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,9 +21,17 @@ describe('RecordEngagementPresenter', () => {
|
||||
eventId: 'event-123',
|
||||
engagementWeight: 10,
|
||||
});
|
||||
expect(presenter.responseModel).toEqual({
|
||||
eventId: 'event-123',
|
||||
engagementWeight: 10,
|
||||
});
|
||||
});
|
||||
|
||||
it('getResponseModel throws if not presented', () => {
|
||||
expect(() => presenter.getResponseModel()).toThrow('Presenter not presented');
|
||||
});
|
||||
|
||||
it('responseModel throws if not presented', () => {
|
||||
expect(() => presenter.responseModel).toThrow('Presenter not presented');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,19 +1,24 @@
|
||||
import type { UseCaseOutputPort } from '@core/shared/application';
|
||||
import type { RecordEngagementOutput } from '@core/analytics/application/use-cases/RecordEngagementUseCase';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application';
|
||||
import type { RecordEngagementOutputDTO } from '../dtos/RecordEngagementOutputDTO';
|
||||
|
||||
export class RecordEngagementPresenter implements UseCaseOutputPort<RecordEngagementOutput> {
|
||||
private responseModel: RecordEngagementOutputDTO | null = null;
|
||||
private model: RecordEngagementOutputDTO | null = null;
|
||||
|
||||
present(result: RecordEngagementOutput): void {
|
||||
this.responseModel = {
|
||||
this.model = {
|
||||
eventId: result.eventId,
|
||||
engagementWeight: result.engagementWeight,
|
||||
};
|
||||
}
|
||||
|
||||
get responseModel(): RecordEngagementOutputDTO {
|
||||
if (!this.model) throw new Error('Presenter not presented');
|
||||
return this.model;
|
||||
}
|
||||
|
||||
getResponseModel(): RecordEngagementOutputDTO {
|
||||
if (!this.responseModel) throw new Error('Presenter not presented');
|
||||
return this.responseModel;
|
||||
if (!this.model) throw new Error('Presenter not presented');
|
||||
return this.model;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,9 +19,16 @@ describe('RecordPageViewPresenter', () => {
|
||||
expect(presenter.getResponseModel()).toEqual({
|
||||
pageViewId: 'pv-123',
|
||||
});
|
||||
expect(presenter.responseModel).toEqual({
|
||||
pageViewId: 'pv-123',
|
||||
});
|
||||
});
|
||||
|
||||
it('getResponseModel throws if not presented', () => {
|
||||
expect(() => presenter.getResponseModel()).toThrow('Presenter not presented');
|
||||
});
|
||||
|
||||
it('responseModel throws if not presented', () => {
|
||||
expect(() => presenter.responseModel).toThrow('Presenter not presented');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,18 +1,23 @@
|
||||
import type { UseCaseOutputPort } from '@core/shared/application';
|
||||
import type { RecordPageViewOutput } from '@core/analytics/application/use-cases/RecordPageViewUseCase';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application';
|
||||
import type { RecordPageViewOutputDTO } from '../dtos/RecordPageViewOutputDTO';
|
||||
|
||||
export class RecordPageViewPresenter implements UseCaseOutputPort<RecordPageViewOutput> {
|
||||
private responseModel: RecordPageViewOutputDTO | null = null;
|
||||
private model: RecordPageViewOutputDTO | null = null;
|
||||
|
||||
present(result: RecordPageViewOutput): void {
|
||||
this.responseModel = {
|
||||
this.model = {
|
||||
pageViewId: result.pageViewId,
|
||||
};
|
||||
}
|
||||
|
||||
get responseModel(): RecordPageViewOutputDTO {
|
||||
if (!this.model) throw new Error('Presenter not presented');
|
||||
return this.model;
|
||||
}
|
||||
|
||||
getResponseModel(): RecordPageViewOutputDTO {
|
||||
if (!this.responseModel) throw new Error('Presenter not presented');
|
||||
return this.responseModel;
|
||||
if (!this.model) throw new Error('Presenter not presented');
|
||||
return this.model;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user