All files / src/execution-logs execution-logs.service.ts

72.22% Statements 13/18
0% Branches 0/2
66.66% Functions 2/3
68.75% Lines 11/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5219x 19x 19x 19x       19x     6x   6x           33x     33x           33x         33x                                      
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ExecutionLog, AIAction } from '../core-entities';
import { CreateExecutionLogDto } from './dto/execution-log.dto';
 
@Injectable()
export class ExecutionLogsService {
  constructor(
    @InjectRepository(ExecutionLog)
    private executionLogsRepository: Repository<ExecutionLog>,
    @InjectRepository(AIAction)
    private aiActionsRepository: Repository<AIAction>,
  ) {}
 
  async createLog(
    createExecutionLogDto: CreateExecutionLogDto,
  ): Promise<ExecutionLog> {
    const aiAction = await this.aiActionsRepository.findOneBy({
      id: createExecutionLogDto.action_id,
    });
    Iif (!aiAction) {
      throw new NotFoundException(
        `AIAction with ID "${createExecutionLogDto.action_id}" not found. Cannot create execution log.`,
      );
    }
 
    const newLog = this.executionLogsRepository.create({
      ...createExecutionLogDto,
      aiAction: aiAction, // Associate with the AIAction entity
    });
 
    return this.executionLogsRepository.save(newLog);
  }
 
  async findAllByActionId(actionId: string): Promise<ExecutionLog[]> {
    const aiAction = await this.aiActionsRepository.findOneBy({
      id: actionId,
    });
    Iif (!aiAction) {
      throw new NotFoundException(
        `AIAction with ID "${actionId}" not found. Cannot retrieve execution logs.`,
      );
    }
 
    return this.executionLogsRepository.find({
      where: { action_id: actionId },
      order: { created_at: 'ASC' },
    });
  }
}