import type { AsyncUseCase } from '@gridpilot/shared/application'; import { AutomationSession } from '../../domain/entities/AutomationSession'; import type { HostedSessionConfig } from '../../domain/types/HostedSessionConfig'; import { AutomationEnginePort } from '../ports/AutomationEnginePort'; import type { IBrowserAutomation } from '../ports/ScreenAutomationPort'; import { SessionRepositoryPort } from '../ports/SessionRepositoryPort'; import type { SessionDTO } from '../dto/SessionDTO'; export class StartAutomationSessionUseCase implements AsyncUseCase { constructor( private readonly automationEngine: AutomationEnginePort, private readonly browserAutomation: IBrowserAutomation, private readonly sessionRepository: SessionRepositoryPort ) {} async execute(config: HostedSessionConfig): Promise { const session = AutomationSession.create(config); const validationResult = await this.automationEngine.validateConfiguration(config); if (!validationResult.isValid) { throw new Error(validationResult.error); } await this.sessionRepository.save(session); const dto: SessionDTO = { sessionId: session.id, state: session.state.value, currentStep: session.currentStep.value, config: session.config, ...(session.startedAt ? { startedAt: session.startedAt } : {}), ...(session.completedAt ? { completedAt: session.completedAt } : {}), ...(session.errorMessage ? { errorMessage: session.errorMessage } : {}), }; return dto; } }