refactor(core): Move methods from WorkflowHelpers into various workflow services (no-changelog) (#8348)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2024-01-17 10:16:13 +01:00
committed by GitHub
parent ab52aaf7e9
commit 7cdbb424e3
21 changed files with 896 additions and 802 deletions

View File

@@ -0,0 +1,36 @@
import { Service } from 'typedi';
import { In, type FindOptionsWhere } from 'typeorm';
import type { RoleNames } from '@db/entities/Role';
import type { SharedWorkflow } from '@db/entities/SharedWorkflow';
import type { User } from '@db/entities/User';
import { RoleRepository } from '@db/repositories/role.repository';
import { SharedWorkflowRepository } from '@db/repositories/sharedWorkflow.repository';
@Service()
export class WorkflowSharingService {
constructor(
private readonly roleRepository: RoleRepository,
private readonly sharedWorkflowRepository: SharedWorkflowRepository,
) {}
/**
* Get the IDs of the workflows that have been shared with the user.
* Returns all IDs if user has the 'workflow:read' scope.
*/
async getSharedWorkflowIds(user: User, roleNames?: RoleNames[]): Promise<string[]> {
const where: FindOptionsWhere<SharedWorkflow> = {};
if (!user.hasGlobalScope('workflow:read')) {
where.userId = user.id;
}
if (roleNames?.length) {
const roleIds = await this.roleRepository.getIdsInScopeWorkflowByNames(roleNames);
where.roleId = In(roleIds);
}
const sharedWorkflows = await this.sharedWorkflowRepository.find({
where,
select: ['workflowId'],
});
return sharedWorkflows.map(({ workflowId }) => workflowId);
}
}