import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { WorkflowAction } from 'src/common/entities';
import { handleKnownErrors } from 'src/common/utils/handle-error.util';
import { ERROR_CODES } from 'src/common/constants/error-string-constants';

@Injectable()
export class WorkflowActionRepository {
  constructor(
    @InjectRepository(WorkflowAction)
    private readonly repo: Repository<WorkflowAction>,
  ) {}

  async findByWorkflowConfigurationId(workflowConfigurationId: number): Promise<WorkflowAction[]> {
    try {
      return await this.repo.find({
        where: { workflowConfiguration: { id: workflowConfigurationId } },
        order: { id: 'ASC' },
      });
    } catch (error) {
      handleKnownErrors(ERROR_CODES.WORKFLOW_ACTION_GET_FAILED, error);
    }
  }
}
