2021-08-29 11:58:11 -07:00
|
|
|
/* eslint-disable no-prototype-builtins */
|
|
|
|
import { INode, IWorkflowCredentials } from 'n8n-workflow';
|
|
|
|
// eslint-disable-next-line import/no-cycle
|
|
|
|
import { Db } from '.';
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
2019-06-23 03:35:23 -07:00
|
|
|
export async function WorkflowCredentials(nodes: INode[]): Promise<IWorkflowCredentials> {
|
|
|
|
// Go through all nodes to find which credentials are needed to execute the workflow
|
|
|
|
const returnCredentials: IWorkflowCredentials = {};
|
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
let node;
|
|
|
|
let type;
|
|
|
|
let name;
|
|
|
|
let foundCredentials;
|
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
2019-06-23 03:35:23 -07:00
|
|
|
for (node of nodes) {
|
2020-02-09 23:18:44 -08:00
|
|
|
if (node.disabled === true || !node.credentials) {
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line no-continue
|
2019-06-23 03:35:23 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
2019-06-23 03:35:23 -07:00
|
|
|
for (type of Object.keys(node.credentials)) {
|
|
|
|
if (!returnCredentials.hasOwnProperty(type)) {
|
|
|
|
returnCredentials[type] = {};
|
|
|
|
}
|
|
|
|
name = node.credentials[type];
|
|
|
|
|
|
|
|
if (!returnCredentials[type].hasOwnProperty(name)) {
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line no-await-in-loop, @typescript-eslint/no-non-null-assertion
|
2019-06-23 03:35:23 -07:00
|
|
|
foundCredentials = await Db.collections.Credentials!.find({ name, type });
|
|
|
|
if (!foundCredentials.length) {
|
|
|
|
throw new Error(`Could not find credentials for type "${type}" with name "${name}".`);
|
|
|
|
}
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line prefer-destructuring
|
2019-06-23 03:35:23 -07:00
|
|
|
returnCredentials[type][name] = foundCredentials[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnCredentials;
|
|
|
|
}
|