Signed-off-by: Oleg Ivaniv <me@olegivaniv.com> Co-authored-by: Val <68596159+valya@users.noreply.github.com> Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in> Co-authored-by: Valya Bullions <valya@n8n.io> Co-authored-by: Danny Martini <danny@n8n.io> Co-authored-by: Danny Martini <despair.blue@gmail.com> Co-authored-by: Iván Ovejero <ivov.src@gmail.com> Co-authored-by: Omar Ajoue <krynble@gmail.com> Co-authored-by: oleg <me@olegivaniv.com> Co-authored-by: Michael Kret <michael.k@radency.com> Co-authored-by: Michael Kret <88898367+michael-radency@users.noreply.github.com> Co-authored-by: Elias Meire <elias@meire.dev> Co-authored-by: Giulio Andreini <andreini@netseven.it> Co-authored-by: Giulio Andreini <g.andreini@gmail.com> Co-authored-by: Ayato Hayashi <go12limchangyong@gmail.com>
55 lines
2.0 KiB
TypeScript
55 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 workflow = await this.sharedWorkflowRepository.findWorkflowForUser(workflowId, user, [
|
|
'workflow:read',
|
|
]);
|
|
if (!workflow) {
|
|
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);
|
|
}
|
|
}
|