1
0
Fork 0
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:
ricardo 2022-05-01 21:54:41 -04:00
parent ad9250d776
commit 10ede21229
4 changed files with 91 additions and 1 deletions
packages/cli/src/PublicApi

View file

@ -0,0 +1,5 @@
import { User } from '../../databases/entities/User';
export function isInstanceOwner(user: User): boolean {
return user.globalRole.name === 'owner';
}

View file

@ -1,5 +1,7 @@
import { User } from '../../databases/entities/User';
import { WorkflowEntity } from '../../databases/entities/WorkflowEntity';
import { Db } from '../..';
import { Workflow } from 'n8n-workflow';
export async function getSharedWorkflowIds(user: User): Promise<number[]> {
const sharedWorkflows = await Db.collections.SharedWorkflow.find({
@ -22,3 +24,16 @@ export async function getWorkflowAccess(
});
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 });
}

View file

@ -52,3 +52,9 @@ export declare namespace ExecutionRequest {
type Get = AuthenticatedRequest<{ executionId: number }, {}, {}, {}>;
type Delete = Get;
}
export declare namespace WorkflowRequest {
type Get = AuthenticatedRequest<{ workflowId: number }, {}, {}, {}>;
type Delete = Get;
type Activate = Get;
}

View file

@ -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 = {
createWorkflow: [],
deleteWorkflow: [],
getWorkflow: [],
getWorkflows: [],
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: [],
};