Files
gridpilot.gg/packages/automation/application/use-cases/StartAutomationSessionUseCase.ts
2025-12-12 14:23:40 +01:00

39 lines
1.5 KiB
TypeScript

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<HostedSessionConfig, SessionDTO> {
constructor(
private readonly automationEngine: AutomationEnginePort,
private readonly browserAutomation: IBrowserAutomation,
private readonly sessionRepository: SessionRepositoryPort
) {}
async execute(config: HostedSessionConfig): Promise<SessionDTO> {
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;
}
}