n8n
Version:
n8n Workflow Automation Tool
87 lines • 4.54 kB
JavaScript
"use strict";
const typedi_1 = require("typedi");
const n8n_workflow_1 = require("n8n-workflow");
const ActiveExecutions_1 = require("../../../../ActiveExecutions");
const global_middleware_1 = require("../../shared/middlewares/global.middleware");
const workflows_service_1 = require("../workflows/workflows.service");
const pagination_service_1 = require("../../shared/services/pagination.service");
const InternalHooks_1 = require("../../../../InternalHooks");
const execution_repository_1 = require("../../../../databases/repositories/execution.repository");
module.exports = {
deleteExecution: [
async (req, res) => {
const sharedWorkflowsIds = await (0, workflows_service_1.getSharedWorkflowIds)(req.user, ['workflow:delete']);
if (!sharedWorkflowsIds.length) {
return res.status(404).json({ message: 'Not Found' });
}
const { id } = req.params;
const execution = await typedi_1.Container.get(execution_repository_1.ExecutionRepository).getExecutionInWorkflowsForPublicApi(id, sharedWorkflowsIds, false);
if (!execution) {
return res.status(404).json({ message: 'Not Found' });
}
await typedi_1.Container.get(execution_repository_1.ExecutionRepository).hardDelete({
workflowId: execution.workflowId,
executionId: execution.id,
});
execution.id = id;
return res.json((0, n8n_workflow_1.replaceCircularReferences)(execution));
},
],
getExecution: [
async (req, res) => {
const sharedWorkflowsIds = await (0, workflows_service_1.getSharedWorkflowIds)(req.user, ['workflow:read']);
if (!sharedWorkflowsIds.length) {
return res.status(404).json({ message: 'Not Found' });
}
const { id } = req.params;
const { includeData = false } = req.query;
const execution = await typedi_1.Container.get(execution_repository_1.ExecutionRepository).getExecutionInWorkflowsForPublicApi(id, sharedWorkflowsIds, includeData);
if (!execution) {
return res.status(404).json({ message: 'Not Found' });
}
void typedi_1.Container.get(InternalHooks_1.InternalHooks).onUserRetrievedExecution({
user_id: req.user.id,
public_api: true,
});
return res.json((0, n8n_workflow_1.replaceCircularReferences)(execution));
},
],
getExecutions: [
global_middleware_1.validCursor,
async (req, res) => {
const { lastId = undefined, limit = 100, status = undefined, includeData = false, workflowId = undefined, } = req.query;
const sharedWorkflowsIds = await (0, workflows_service_1.getSharedWorkflowIds)(req.user, ['workflow:read']);
if (!sharedWorkflowsIds.length || (workflowId && !sharedWorkflowsIds.includes(workflowId))) {
return res.status(200).json({ data: [], nextCursor: null });
}
const runningExecutionsIds = typedi_1.Container.get(ActiveExecutions_1.ActiveExecutions)
.getActiveExecutions()
.map(({ id }) => id);
const filters = {
status,
limit,
lastId,
includeData,
workflowIds: workflowId ? [workflowId] : sharedWorkflowsIds,
excludedExecutionsIds: runningExecutionsIds,
};
const executions = await typedi_1.Container.get(execution_repository_1.ExecutionRepository).getExecutionsForPublicApi(filters);
const newLastId = !executions.length ? '0' : executions.slice(-1)[0].id;
filters.lastId = newLastId;
const count = await typedi_1.Container.get(execution_repository_1.ExecutionRepository).getExecutionsCountForPublicApi(filters);
void typedi_1.Container.get(InternalHooks_1.InternalHooks).onUserRetrievedAllExecutions({
user_id: req.user.id,
public_api: true,
});
return res.json({
data: (0, n8n_workflow_1.replaceCircularReferences)(executions),
nextCursor: (0, pagination_service_1.encodeNextCursor)({
lastId: newLastId,
limit,
numberOfNextRecords: count,
}),
});
},
],
};
//# sourceMappingURL=executions.handler.js.map