import { Injectable } from '@nestjs/common'; import { CommandBus, QueryBus } from '@nestjs/cqrs'; import { AutomationInput, CreateAutomationCommand, DeleteAutomationCommand, GetAutomationsQuery, RunAutomationCommand, UpdateAutomationCommand, } from './automations/automations'; import { Automation } from './ports/automations.repository'; /** The project's automation rules (owners and admins). */ @Injectable() export class AutomationsService { constructor( private readonly commandBus: CommandBus, private readonly queryBus: QueryBus, ) {} list(viewerId: string, projectId: string): Promise { return this.queryBus.execute(new GetAutomationsQuery(viewerId, projectId)); } create( actorId: string, projectId: string, rule: AutomationInput, ): Promise { return this.commandBus.execute( new CreateAutomationCommand(actorId, projectId, rule), ); } update( actorId: string, automationId: string, changes: { name?: string; active?: boolean }, ): Promise { return this.commandBus.execute( new UpdateAutomationCommand(actorId, automationId, changes), ); } /** Runs a rule on a ticket; the error, if any, as text. */ run(automation: Automation, issueId: string): Promise { return this.commandBus.execute( new RunAutomationCommand(automation, issueId), ); } delete(actorId: string, automationId: string): Promise { return this.commandBus.execute( new DeleteAutomationCommand(actorId, automationId), ); } }