refactor use cases
This commit is contained in:
@@ -1,18 +1,20 @@
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { Season } from '../../domain/entities/Season';
|
||||
import type { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
|
||||
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
|
||||
import type { LeagueConfigFormModel } from '../dto/LeagueConfigFormDTO';
|
||||
import type { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
|
||||
import type { Weekday } from '../../domain/types/Weekday';
|
||||
import { LeagueTimezone } from '../../domain/value-objects/LeagueTimezone';
|
||||
import { MonthlyRecurrencePattern } from '../../domain/value-objects/MonthlyRecurrencePattern';
|
||||
import { RaceTimeOfDay } from '../../domain/value-objects/RaceTimeOfDay';
|
||||
import { RecurrenceStrategyFactory } from '../../domain/value-objects/RecurrenceStrategy';
|
||||
import { SeasonDropPolicy } from '../../domain/value-objects/SeasonDropPolicy';
|
||||
import { SeasonSchedule } from '../../domain/value-objects/SeasonSchedule';
|
||||
import { SeasonScoringConfig } from '../../domain/value-objects/SeasonScoringConfig';
|
||||
import { SeasonDropPolicy } from '../../domain/value-objects/SeasonDropPolicy';
|
||||
import { SeasonStewardingConfig } from '../../domain/value-objects/SeasonStewardingConfig';
|
||||
import { RaceTimeOfDay } from '../../domain/value-objects/RaceTimeOfDay';
|
||||
import { LeagueTimezone } from '../../domain/value-objects/LeagueTimezone';
|
||||
import { RecurrenceStrategyFactory } from '../../domain/value-objects/RecurrenceStrategy';
|
||||
import { WeekdaySet } from '../../domain/value-objects/WeekdaySet';
|
||||
import { MonthlyRecurrencePattern } from '../../domain/value-objects/MonthlyRecurrencePattern';
|
||||
import type { Weekday } from '../../domain/types/Weekday';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import type { LeagueConfigFormModel } from '../dto/LeagueConfigFormDTO';
|
||||
|
||||
// TODO The whole file mixes a lot of concerns...
|
||||
|
||||
export interface CreateSeasonForLeagueCommand {
|
||||
leagueId: string;
|
||||
|
||||
@@ -1,9 +1,15 @@
|
||||
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
|
||||
import { CompleteDriverOnboardingUseCase, type CompleteDriverOnboardingResult } from './CompleteDriverOnboardingUseCase';
|
||||
import {
|
||||
CompleteDriverOnboardingUseCase,
|
||||
type CompleteDriverOnboardingInput,
|
||||
type CompleteDriverOnboardingResult,
|
||||
type CompleteDriverOnboardingApplicationError,
|
||||
} from './CompleteDriverOnboardingUseCase';
|
||||
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
|
||||
import { Driver } from '../../domain/entities/Driver';
|
||||
import type { CompleteDriverOnboardingInput } from './CompleteDriverOnboardingUseCase';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
import type { Logger } from '@core/shared/application/Logger';
|
||||
import type { Result } from '@core/shared/application/Result';
|
||||
|
||||
describe('CompleteDriverOnboardingUseCase', () => {
|
||||
let useCase: CompleteDriverOnboardingUseCase;
|
||||
@@ -11,17 +17,25 @@ describe('CompleteDriverOnboardingUseCase', () => {
|
||||
findById: Mock;
|
||||
create: Mock;
|
||||
};
|
||||
let output: { present: Mock };
|
||||
let logger: Logger & { error: Mock };
|
||||
let output: { present: Mock } & UseCaseOutputPort<CompleteDriverOnboardingResult>;
|
||||
|
||||
beforeEach(() => {
|
||||
driverRepository = {
|
||||
findById: vi.fn(),
|
||||
create: vi.fn(),
|
||||
};
|
||||
output = { present: vi.fn() };
|
||||
logger = {
|
||||
debug: vi.fn(),
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
} as unknown as Logger & { error: Mock };
|
||||
output = { present: vi.fn() } as unknown as typeof output;
|
||||
useCase = new CompleteDriverOnboardingUseCase(
|
||||
driverRepository as unknown as IDriverRepository,
|
||||
output as unknown as UseCaseOutputPort<CompleteDriverOnboardingResult>,
|
||||
logger,
|
||||
output,
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ import { Driver } from '../../domain/entities/Driver';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
import type { Logger } from '@core/shared/application/Logger';
|
||||
|
||||
export interface CompleteDriverOnboardingInput {
|
||||
userId: string;
|
||||
@@ -17,28 +18,43 @@ export type CompleteDriverOnboardingResult = {
|
||||
driver: Driver;
|
||||
};
|
||||
|
||||
export type CompleteDriverOnboardingErrorCode =
|
||||
| 'DRIVER_ALREADY_EXISTS'
|
||||
| 'REPOSITORY_ERROR';
|
||||
|
||||
export type CompleteDriverOnboardingApplicationError = ApplicationErrorCode<
|
||||
CompleteDriverOnboardingErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
|
||||
/**
|
||||
* Use Case for completing driver onboarding.
|
||||
*/
|
||||
export class CompleteDriverOnboardingUseCase {
|
||||
constructor(
|
||||
private readonly driverRepository: IDriverRepository,
|
||||
private readonly logger: Logger,
|
||||
private readonly output: UseCaseOutputPort<CompleteDriverOnboardingResult>,
|
||||
) {}
|
||||
|
||||
async execute(command: CompleteDriverOnboardingInput): Promise<Result<void, ApplicationErrorCode<'DRIVER_ALREADY_EXISTS' | 'REPOSITORY_ERROR'>>> {
|
||||
async execute(
|
||||
input: CompleteDriverOnboardingInput,
|
||||
): Promise<Result<void, CompleteDriverOnboardingApplicationError>> {
|
||||
try {
|
||||
const existing = await this.driverRepository.findById(command.userId);
|
||||
const existing = await this.driverRepository.findById(input.userId);
|
||||
if (existing) {
|
||||
return Result.err({ code: 'DRIVER_ALREADY_EXISTS' });
|
||||
return Result.err({
|
||||
code: 'DRIVER_ALREADY_EXISTS',
|
||||
details: { message: 'Driver already exists' },
|
||||
});
|
||||
}
|
||||
|
||||
const driver = Driver.create({
|
||||
id: command.userId,
|
||||
iracingId: command.userId,
|
||||
name: command.displayName,
|
||||
country: command.country,
|
||||
...(command.bio !== undefined ? { bio: command.bio } : {}),
|
||||
id: input.userId,
|
||||
iracingId: input.userId,
|
||||
name: input.displayName,
|
||||
country: input.country,
|
||||
...(input.bio !== undefined ? { bio: input.bio } : {}),
|
||||
});
|
||||
|
||||
await this.driverRepository.create(driver);
|
||||
@@ -47,10 +63,16 @@ export class CompleteDriverOnboardingUseCase {
|
||||
|
||||
return Result.ok(undefined);
|
||||
} catch (error) {
|
||||
const err = error instanceof Error ? error : new Error('Unknown error');
|
||||
|
||||
this.logger.error('CompleteDriverOnboardingUseCase.execute failed', err, {
|
||||
input,
|
||||
});
|
||||
|
||||
return Result.err({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: {
|
||||
message: error instanceof Error ? error.message : 'Unknown error',
|
||||
message: err.message ?? 'Unexpected repository error',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user