import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Pipeline } from './pipeline.entity'; import { Repository } from 'typeorm'; import { BaseDbService } from '../commons/services/base-db.service'; import { CreatePipelineInput } from './dtos/create-pipeline.input'; import { UpdatePipelineInput } from './dtos/update-pipeline.input'; import { ListPipelineArgs } from './dtos/list-pipelines.args'; import { InjectQueue } from '@nestjs/bull'; import { LIST_LOGS_TASK } from '../repos/repos.constants'; import { Queue } from 'bull'; import { ListLogsOption } from '../repos/models/list-logs.options'; @Injectable() export class PipelinesService extends BaseDbService { readonly uniqueFields: Array> = [['projectId', 'name']]; constructor( @InjectRepository(Pipeline) readonly repository: Repository, @InjectQueue(LIST_LOGS_TASK) private readonly listLogsQueue: Queue, ) { super(repository); } async list(dto: ListPipelineArgs) { return this.repository.find(dto); } async create(dto: CreatePipelineInput) { await this.isDuplicateEntity(dto); return await this.repository.save(this.repository.create(dto)); } async update(id: string, dto: UpdatePipelineInput) { await this.isDuplicateEntityForUpdate(id, dto); const old = await this.findOne(id); return await this.repository.save(this.repository.merge(old, dto)); } async remove(id: string) { return (await this.repository.softDelete({ id })).affected; } async listLogsForPipeline(id: string) { const pipeline = await this.repository.findOneOrFail({ where: { id }, relations: ['project'], }); const job = await this.listLogsQueue.add({ project: pipeline.project, branch: pipeline.branch, }); return job; } }