mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
⚡ Add POST /workflows/:workflowId/activate
This commit is contained in:
parent
ad9250d776
commit
10ede21229
5
packages/cli/src/PublicApi/Services/user.ts
Normal file
5
packages/cli/src/PublicApi/Services/user.ts
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
import { User } from '../../databases/entities/User';
|
||||||
|
|
||||||
|
export function isInstanceOwner(user: User): boolean {
|
||||||
|
return user.globalRole.name === 'owner';
|
||||||
|
}
|
|
@ -1,5 +1,7 @@
|
||||||
import { User } from '../../databases/entities/User';
|
import { User } from '../../databases/entities/User';
|
||||||
|
import { WorkflowEntity } from '../../databases/entities/WorkflowEntity';
|
||||||
import { Db } from '../..';
|
import { Db } from '../..';
|
||||||
|
import { Workflow } from 'n8n-workflow';
|
||||||
|
|
||||||
export async function getSharedWorkflowIds(user: User): Promise<number[]> {
|
export async function getSharedWorkflowIds(user: User): Promise<number[]> {
|
||||||
const sharedWorkflows = await Db.collections.SharedWorkflow.find({
|
const sharedWorkflows = await Db.collections.SharedWorkflow.find({
|
||||||
|
@ -22,3 +24,16 @@ export async function getWorkflowAccess(
|
||||||
});
|
});
|
||||||
return !!sharedWorkflows.length;
|
return !!sharedWorkflows.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getWorkflowById(id: number): Promise<WorkflowEntity | undefined> {
|
||||||
|
const workflow = await Db.collections.Workflow.findOne({
|
||||||
|
where: {
|
||||||
|
id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return workflow;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function activeWorkflow(workflow: WorkflowEntity): Promise<void> {
|
||||||
|
await Db.collections.Workflow.update(workflow, { active: true });
|
||||||
|
}
|
||||||
|
|
|
@ -52,3 +52,9 @@ export declare namespace ExecutionRequest {
|
||||||
type Get = AuthenticatedRequest<{ executionId: number }, {}, {}, {}>;
|
type Get = AuthenticatedRequest<{ executionId: number }, {}, {}, {}>;
|
||||||
type Delete = Get;
|
type Delete = Get;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export declare namespace WorkflowRequest {
|
||||||
|
type Get = AuthenticatedRequest<{ workflowId: number }, {}, {}, {}>;
|
||||||
|
type Delete = Get;
|
||||||
|
type Activate = Get;
|
||||||
|
}
|
||||||
|
|
|
@ -1,9 +1,73 @@
|
||||||
|
import express = require('express');
|
||||||
|
import { ActiveWorkflowRunner } from '../../..';
|
||||||
|
import { WorkflowEntity } from '../../../databases/entities/WorkflowEntity';
|
||||||
|
import { authorize, instanceOwnerSetup } from '../../middlewares';
|
||||||
|
import { WorkflowRequest } from '../../publicApiRequest';
|
||||||
|
import { isInstanceOwner } from '../../Services/user';
|
||||||
|
import { getWorkflowById, getWorkflowAccess, activeWorkflow } from '../../Services/workflow';
|
||||||
|
|
||||||
export = {
|
export = {
|
||||||
createWorkflow: [],
|
createWorkflow: [],
|
||||||
deleteWorkflow: [],
|
deleteWorkflow: [],
|
||||||
getWorkflow: [],
|
getWorkflow: [],
|
||||||
getWorkflows: [],
|
getWorkflows: [],
|
||||||
updateWorkflow: [],
|
updateWorkflow: [],
|
||||||
activateWorkflow: [],
|
activateWorkflow: [
|
||||||
|
instanceOwnerSetup,
|
||||||
|
authorize(['owner', 'member']),
|
||||||
|
async (req: WorkflowRequest.Activate, res: express.Response): Promise<express.Response> => {
|
||||||
|
const { workflowId } = req.params;
|
||||||
|
|
||||||
|
const workflow = await getWorkflowById(workflowId);
|
||||||
|
if (workflow === undefined) {
|
||||||
|
return res.status(404).json();
|
||||||
|
}
|
||||||
|
|
||||||
|
const workflowRunner = ActiveWorkflowRunner.getInstance();
|
||||||
|
|
||||||
|
const activateWorkflow = async (w: WorkflowEntity) => {
|
||||||
|
await workflowRunner.add(w.id.toString(), 'activate');
|
||||||
|
// change the status to active in the DB
|
||||||
|
await activeWorkflow(workflow);
|
||||||
|
};
|
||||||
|
|
||||||
|
if (isInstanceOwner(req.user)) {
|
||||||
|
if (!workflow.active) {
|
||||||
|
try {
|
||||||
|
await activateWorkflow(workflow);
|
||||||
|
return res.json(workflow);
|
||||||
|
} catch (error) {
|
||||||
|
// todo
|
||||||
|
// remove the type assertion
|
||||||
|
const errorObject = error as unknown as { message: string };
|
||||||
|
return res.status(400).json({ error: errorObject.message });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// nothing to do as the wokflow is already active
|
||||||
|
return res.json(workflow);
|
||||||
|
}
|
||||||
|
|
||||||
|
const userHasAccessToWorkflow = await getWorkflowAccess(req.user, workflowId.toString());
|
||||||
|
|
||||||
|
if (userHasAccessToWorkflow) {
|
||||||
|
if (!workflow.active) {
|
||||||
|
try {
|
||||||
|
await activateWorkflow(workflow);
|
||||||
|
return res.json(workflow);
|
||||||
|
} catch (error) {
|
||||||
|
// todo
|
||||||
|
// remove the type assertion
|
||||||
|
const errorObject = error as unknown as { message: string };
|
||||||
|
return res.status(400).json({ error: errorObject.message });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res.json(workflow);
|
||||||
|
}
|
||||||
|
// member trying to access workflow that does not own
|
||||||
|
// 404 or 403?
|
||||||
|
// 403 will let him know about the existance of the workflow
|
||||||
|
return res.status(404).json();
|
||||||
|
},
|
||||||
|
],
|
||||||
desactivateWorkflow: [],
|
desactivateWorkflow: [],
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue