38 lines
1.2 KiB
TypeScript
38 lines
1.2 KiB
TypeScript
import { Result } from '../../shared/result/Result';
|
|
import { RaceCreationResult } from '../../domain/value-objects/RaceCreationResult';
|
|
import type { ICheckoutService } from '../ports/ICheckoutService';
|
|
|
|
export class CompleteRaceCreationUseCase {
|
|
constructor(private readonly checkoutService: ICheckoutService) {}
|
|
|
|
async execute(sessionId: string): Promise<Result<RaceCreationResult>> {
|
|
if (!sessionId || sessionId.trim() === '') {
|
|
return Result.err(new Error('Session ID is required'));
|
|
}
|
|
|
|
const infoResult = await this.checkoutService.extractCheckoutInfo();
|
|
|
|
if (infoResult.isErr()) {
|
|
return Result.err(infoResult.unwrapErr());
|
|
}
|
|
|
|
const info = infoResult.unwrap();
|
|
|
|
if (!info.price) {
|
|
return Result.err(new Error('Could not extract price from checkout page'));
|
|
}
|
|
|
|
try {
|
|
const raceCreationResult = RaceCreationResult.create({
|
|
sessionId,
|
|
price: info.price.toDisplayString(),
|
|
timestamp: new Date(),
|
|
});
|
|
|
|
return Result.ok(raceCreationResult);
|
|
} catch (error) {
|
|
const err = error instanceof Error ? error : new Error('Unknown error');
|
|
return Result.err(err);
|
|
}
|
|
}
|
|
} |