2023-01-27 05:56:56 -08:00
|
|
|
import type { INode, IWorkflowCredentials } from 'n8n-workflow';
|
2022-11-09 06:25:00 -08:00
|
|
|
import * as Db from '@/Db';
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
|
|
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 = {};
|
|
|
|
|
|
|
|
let node;
|
|
|
|
let type;
|
2021-10-13 15:21:00 -07:00
|
|
|
let nodeCredentials;
|
2019-06-23 03:35:23 -07:00
|
|
|
let foundCredentials;
|
2023-07-31 02:00:48 -07:00
|
|
|
|
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) {
|
2019-06-23 03:35:23 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (type of Object.keys(node.credentials)) {
|
2021-10-13 15:21:00 -07:00
|
|
|
if (!returnCredentials[type]) {
|
2019-06-23 03:35:23 -07:00
|
|
|
returnCredentials[type] = {};
|
|
|
|
}
|
2021-10-13 15:21:00 -07:00
|
|
|
nodeCredentials = node.credentials[type];
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2021-10-13 15:21:00 -07:00
|
|
|
if (!nodeCredentials.id) {
|
|
|
|
throw new Error(
|
|
|
|
`Credentials with name "${nodeCredentials.name}" for type "${type}" miss an ID.`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!returnCredentials[type][nodeCredentials.id]) {
|
2023-01-13 09:12:22 -08:00
|
|
|
foundCredentials = await Db.collections.Credentials.findOneBy({
|
2021-10-13 15:21:00 -07:00
|
|
|
id: nodeCredentials.id,
|
|
|
|
type,
|
|
|
|
});
|
|
|
|
if (!foundCredentials) {
|
|
|
|
throw new Error(
|
|
|
|
`Could not find credentials for type "${type}" with ID "${nodeCredentials.id}".`,
|
|
|
|
);
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
2023-07-31 02:00:48 -07:00
|
|
|
|
2021-10-13 15:21:00 -07:00
|
|
|
returnCredentials[type][nodeCredentials.id] = foundCredentials;
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnCredentials;
|
|
|
|
}
|