Files
Automata/packages/cli/src/WorkflowCredentials.ts
कारतोफ्फेलस्क्रिप्ट™ 000e76e3b4 ci(core): Reduce memory usage in tests (part-2) (no-changelog) (#7671)
This also gets rid of `Db.collection`, which was another source of
circular dependencies.
2023-11-10 15:04:26 +01:00

50 lines
1.4 KiB
TypeScript

import Container from 'typedi';
import type { INode, IWorkflowCredentials } from 'n8n-workflow';
import { CredentialsRepository } from '@db/repositories/credentials.repository';
// 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;
let nodeCredentials;
let foundCredentials;
for (node of nodes) {
if (node.disabled === true || !node.credentials) {
continue;
}
for (type of Object.keys(node.credentials)) {
if (!returnCredentials[type]) {
returnCredentials[type] = {};
}
nodeCredentials = node.credentials[type];
if (!nodeCredentials.id) {
throw new Error(
`Credentials with name "${nodeCredentials.name}" for type "${type}" miss an ID.`,
);
}
if (!returnCredentials[type][nodeCredentials.id]) {
foundCredentials = await Container.get(CredentialsRepository).findOneBy({
id: nodeCredentials.id,
type,
});
if (!foundCredentials) {
throw new Error(
`Could not find credentials for type "${type}" with ID "${nodeCredentials.id}".`,
);
}
returnCredentials[type][nodeCredentials.id] = foundCredentials;
}
}
}
return returnCredentials;
}