Files
Automata/packages/cli/src/services/activeWorkflows.service.ts
Tomi Turtiainen 9a1cc56806 fix: Set '@typescript-eslint/return-await' rule to 'always' for node code (no-changelog) (#8363)
Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
2024-01-17 17:08:50 +02:00

53 lines
2.0 KiB
TypeScript

import { Service } from 'typedi';
import type { User } from '@db/entities/User';
import { SharedWorkflowRepository } from '@db/repositories/sharedWorkflow.repository';
import { WorkflowRepository } from '@db/repositories/workflow.repository';
import { ActivationErrorsService } from '@/ActivationErrors.service';
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
import { Logger } from '@/Logger';
@Service()
export class ActiveWorkflowsService {
constructor(
private readonly logger: Logger,
private readonly workflowRepository: WorkflowRepository,
private readonly sharedWorkflowRepository: SharedWorkflowRepository,
private readonly activationErrorsService: ActivationErrorsService,
) {}
async getAllActiveIdsInStorage() {
const activationErrors = await this.activationErrorsService.getAll();
const activeWorkflowIds = await this.workflowRepository.getActiveIds();
return activeWorkflowIds.filter((workflowId) => !activationErrors[workflowId]);
}
async getAllActiveIdsFor(user: User) {
const activationErrors = await this.activationErrorsService.getAll();
const activeWorkflowIds = await this.workflowRepository.getActiveIds();
const hasFullAccess = user.hasGlobalScope('workflow:list');
if (hasFullAccess) {
return activeWorkflowIds.filter((workflowId) => !activationErrors[workflowId]);
}
const sharedWorkflowIds =
await this.sharedWorkflowRepository.getSharedWorkflowIds(activeWorkflowIds);
return sharedWorkflowIds.filter((workflowId) => !activationErrors[workflowId]);
}
async getActivationError(workflowId: string, user: User) {
const hasAccess = await this.sharedWorkflowRepository.hasAccess(workflowId, user);
if (!hasAccess) {
this.logger.verbose('User attempted to access workflow errors without permissions', {
workflowId,
userId: user.id,
});
throw new BadRequestError(`Workflow with ID "${workflowId}" could not be found.`);
}
return await this.activationErrorsService.get(workflowId);
}
}