refactor(core): Make workflow services injectable (no-changelog) (#8033)
Refactor static workflow service classes into DI-compatible classes Context: https://n8nio.slack.com/archives/C069HS026UF/p1702466571648889 Up next: - Inject dependencies into workflow services - Consolidate workflow controllers into one - Make workflow controller injectable - Inject dependencies into workflow controller --------- Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
This commit is contained in:
@@ -8,7 +8,7 @@ import { WorkflowEntity } from '@db/entities/WorkflowEntity';
|
||||
import { validateEntity } from '@/GenericHelpers';
|
||||
import type { ListQuery, WorkflowRequest } from '@/requests';
|
||||
import { isSharingEnabled, rightDiff } from '@/UserManagement/UserManagementHelper';
|
||||
import { EEWorkflowsService as EEWorkflows } from './workflows.services.ee';
|
||||
import { EnterpriseWorkflowService } from './workflow.service.ee';
|
||||
import { ExternalHooks } from '@/ExternalHooks';
|
||||
import { SharedWorkflow } from '@db/entities/SharedWorkflow';
|
||||
import { CredentialsService } from '../credentials/credentials.service';
|
||||
@@ -59,7 +59,7 @@ EEWorkflowController.put(
|
||||
throw new BadRequestError('Bad request');
|
||||
}
|
||||
|
||||
const isOwnedRes = await EEWorkflows.isOwned(req.user, workflowId);
|
||||
const isOwnedRes = await Container.get(EnterpriseWorkflowService).isOwned(req.user, workflowId);
|
||||
const { ownsWorkflow } = isOwnedRes;
|
||||
let { workflow } = isOwnedRes;
|
||||
|
||||
@@ -67,10 +67,14 @@ EEWorkflowController.put(
|
||||
workflow = undefined;
|
||||
// Allow owners/admins to share
|
||||
if (await req.user.hasGlobalScope('workflow:share')) {
|
||||
const sharedRes = await EEWorkflows.getSharing(req.user, workflowId, {
|
||||
allowGlobalScope: true,
|
||||
globalScope: 'workflow:share',
|
||||
});
|
||||
const sharedRes = await Container.get(EnterpriseWorkflowService).getSharing(
|
||||
req.user,
|
||||
workflowId,
|
||||
{
|
||||
allowGlobalScope: true,
|
||||
globalScope: 'workflow:share',
|
||||
},
|
||||
);
|
||||
workflow = sharedRes?.workflow;
|
||||
}
|
||||
if (!workflow) {
|
||||
@@ -79,10 +83,11 @@ EEWorkflowController.put(
|
||||
}
|
||||
|
||||
const ownerIds = (
|
||||
await EEWorkflows.getSharings(Db.getConnection().createEntityManager(), workflowId, [
|
||||
'shared',
|
||||
'shared.role',
|
||||
])
|
||||
await Container.get(EnterpriseWorkflowService).getSharings(
|
||||
Db.getConnection().createEntityManager(),
|
||||
workflowId,
|
||||
['shared', 'shared.role'],
|
||||
)
|
||||
)
|
||||
.filter((e) => e.role.name === 'owner')
|
||||
.map((e) => e.userId);
|
||||
@@ -90,9 +95,12 @@ EEWorkflowController.put(
|
||||
let newShareeIds: string[] = [];
|
||||
await Db.transaction(async (trx) => {
|
||||
// remove all sharings that are not supposed to exist anymore
|
||||
await EEWorkflows.pruneSharings(trx, workflowId, [...ownerIds, ...shareWithIds]);
|
||||
await Container.get(EnterpriseWorkflowService).pruneSharings(trx, workflowId, [
|
||||
...ownerIds,
|
||||
...shareWithIds,
|
||||
]);
|
||||
|
||||
const sharings = await EEWorkflows.getSharings(trx, workflowId);
|
||||
const sharings = await Container.get(EnterpriseWorkflowService).getSharings(trx, workflowId);
|
||||
|
||||
// extract the new sharings that need to be added
|
||||
newShareeIds = rightDiff(
|
||||
@@ -101,7 +109,7 @@ EEWorkflowController.put(
|
||||
);
|
||||
|
||||
if (newShareeIds.length) {
|
||||
await EEWorkflows.share(trx, workflow!, newShareeIds);
|
||||
await Container.get(EnterpriseWorkflowService).share(trx, workflow!, newShareeIds);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -124,7 +132,9 @@ EEWorkflowController.get(
|
||||
relations.push('tags');
|
||||
}
|
||||
|
||||
const workflow = await EEWorkflows.get({ id: workflowId }, { relations });
|
||||
const enterpriseWorkflowService = Container.get(EnterpriseWorkflowService);
|
||||
|
||||
const workflow = await enterpriseWorkflowService.get({ id: workflowId }, { relations });
|
||||
|
||||
if (!workflow) {
|
||||
throw new NotFoundError(`Workflow with ID "${workflowId}" does not exist`);
|
||||
@@ -137,8 +147,8 @@ EEWorkflowController.get(
|
||||
);
|
||||
}
|
||||
|
||||
EEWorkflows.addOwnerAndSharings(workflow);
|
||||
await EEWorkflows.addCredentialsToWorkflow(workflow, req.user);
|
||||
enterpriseWorkflowService.addOwnerAndSharings(workflow);
|
||||
await enterpriseWorkflowService.addCredentialsToWorkflow(workflow, req.user);
|
||||
return workflow;
|
||||
}),
|
||||
);
|
||||
@@ -179,7 +189,10 @@ EEWorkflowController.post(
|
||||
const allCredentials = await CredentialsService.getMany(req.user);
|
||||
|
||||
try {
|
||||
EEWorkflows.validateCredentialPermissionsToUser(newWorkflow, allCredentials);
|
||||
Container.get(EnterpriseWorkflowService).validateCredentialPermissionsToUser(
|
||||
newWorkflow,
|
||||
allCredentials,
|
||||
);
|
||||
} catch (error) {
|
||||
throw new BadRequestError(
|
||||
'The workflow you are trying to save contains credentials that are not shared with you',
|
||||
@@ -240,7 +253,7 @@ EEWorkflowController.get(
|
||||
try {
|
||||
const sharedWorkflowIds = await WorkflowHelpers.getSharedWorkflowIds(req.user);
|
||||
|
||||
const { workflows: data, count } = await EEWorkflows.getMany(
|
||||
const { workflows: data, count } = await Container.get(EnterpriseWorkflowService).getMany(
|
||||
sharedWorkflowIds,
|
||||
req.listQueryOptions,
|
||||
);
|
||||
@@ -264,9 +277,13 @@ EEWorkflowController.patch(
|
||||
const { tags, ...rest } = req.body;
|
||||
Object.assign(updateData, rest);
|
||||
|
||||
const safeWorkflow = await EEWorkflows.preventTampering(updateData, workflowId, req.user);
|
||||
const safeWorkflow = await Container.get(EnterpriseWorkflowService).preventTampering(
|
||||
updateData,
|
||||
workflowId,
|
||||
req.user,
|
||||
);
|
||||
|
||||
const updatedWorkflow = await EEWorkflows.update(
|
||||
const updatedWorkflow = await Container.get(EnterpriseWorkflowService).update(
|
||||
req.user,
|
||||
safeWorkflow,
|
||||
workflowId,
|
||||
@@ -288,10 +305,18 @@ EEWorkflowController.post(
|
||||
Object.assign(workflow, req.body.workflowData);
|
||||
|
||||
if (req.body.workflowData.id !== undefined) {
|
||||
const safeWorkflow = await EEWorkflows.preventTampering(workflow, workflow.id, req.user);
|
||||
const safeWorkflow = await Container.get(EnterpriseWorkflowService).preventTampering(
|
||||
workflow,
|
||||
workflow.id,
|
||||
req.user,
|
||||
);
|
||||
req.body.workflowData.nodes = safeWorkflow.nodes;
|
||||
}
|
||||
|
||||
return EEWorkflows.runManually(req.body, req.user, GenericHelpers.getSessionId(req));
|
||||
return Container.get(EnterpriseWorkflowService).runManually(
|
||||
req.body,
|
||||
req.user,
|
||||
GenericHelpers.getSessionId(req),
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user