From d31fbbba27895c5ab1ccdbf6d8c94b5b7b00893e Mon Sep 17 00:00:00 2001 From: Michael Kret <88898367+michael-radency@users.noreply.github.com> Date: Mon, 17 Oct 2022 14:39:42 +0300 Subject: [PATCH] feat(editor): add automatic credential selection for new nodes (#2746) * :zap: implemented automatic credential selection in nodes * :zap: fixed implementation * :zap: fixed linter error * :hammer: in progress, removed watching for auth type, added check for more then one existing credential * :hammer: removed console log * :hammer: changing auth method for one that have default credential * :hammer: credentials will be set only at node creation time * :hammer: fixed authentication parameter assigment for nodes that does not have it * :zap: better properties checking * :hammer: improvements * :hammer: extracted into function, fix issue with assigning hidden credentials * remove console log * fix bug with multiple creds * fix defaults issue * remove import * simplify to just auth * Revert "simplify to just auth" 042c9cc30fc84f2df493d5fb2aa082af4745cdda * fix get Co-authored-by: Mutasem --- .../src/components/NodeCredentials.vue | 1 + packages/editor-ui/src/views/NodeView.vue | 81 ++++++++++++++++--- 2 files changed, 71 insertions(+), 11 deletions(-) diff --git a/packages/editor-ui/src/components/NodeCredentials.vue b/packages/editor-ui/src/components/NodeCredentials.vue index 0bc81f59e2..7304846104 100644 --- a/packages/editor-ui/src/components/NodeCredentials.vue +++ b/packages/editor-ui/src/components/NodeCredentials.vue @@ -147,6 +147,7 @@ export default mixins( return this.node.credentials || {}; }, }, + methods: { getCredentialOptions(type: string): ICredentialsResponse[] { return (this.allCredentialsByType as Record)[type].filter((credential) => { diff --git a/packages/editor-ui/src/views/NodeView.vue b/packages/editor-ui/src/views/NodeView.vue index d577ad5f1f..c3f8ae3277 100644 --- a/packages/editor-ui/src/views/NodeView.vue +++ b/packages/editor-ui/src/views/NodeView.vue @@ -1365,7 +1365,75 @@ export default mixins( duration: 0, }); }, - async injectNode(nodeTypeName: string, options: AddNodeOptions = {}) { + + async getNewNodeWithDefaultCredential(nodeTypeData: INodeTypeDescription) { + const newNodeData: INodeUi = { + id: uuid(), + name: nodeTypeData.defaults.name as string, + type: nodeTypeData.name, + typeVersion: Array.isArray(nodeTypeData.version) + ? nodeTypeData.version.slice(-1)[0] + : nodeTypeData.version, + position: [0, 0], + parameters: {}, + }; + + const credentialPerType = nodeTypeData.credentials && nodeTypeData.credentials + .map(type => this.$store.getters['credentials/getCredentialsByType'](type.name)) + .flat(); + + if (credentialPerType && credentialPerType.length === 1) { + const defaultCredential = credentialPerType[0]; + + const selectedCredentials = this.$store.getters['credentials/getCredentialById'](defaultCredential.id); + const selected = { id: selectedCredentials.id, name: selectedCredentials.name }; + const credentials = { + [defaultCredential.type]: selected, + }; + + await this.loadNodesProperties([newNodeData].map(node => ({name: node.type, version: node.typeVersion}))); + const nodeType = this.$store.getters['nodeTypes/getNodeType'](newNodeData.type, newNodeData.typeVersion) as INodeTypeDescription; + const nodeParameters = NodeHelpers.getNodeParameters(nodeType.properties, {}, true, false, newNodeData); + + if (nodeTypeData.credentials) { + const authentication = nodeTypeData.credentials.find(type => type.name === defaultCredential.type); + if (authentication?.displayOptions?.hide) { + return newNodeData; + } + + const authDisplayOptions = authentication?.displayOptions?.show; + if (!authDisplayOptions) { + newNodeData.credentials = credentials; + return newNodeData; + } + + if (Object.keys(authDisplayOptions).length === 1 && authDisplayOptions['authentication']) { + // ignore complex case when there's multiple dependencies + newNodeData.credentials = credentials; + + let parameters: { [key:string]: string } = {}; + for (const displayOption of Object.keys(authDisplayOptions)) { + if (nodeParameters && !nodeParameters[displayOption]) { + parameters = {}; + newNodeData.credentials = undefined; + break; + } + const optionValue = authDisplayOptions[displayOption]?.[0]; + if (optionValue && typeof optionValue === 'string') { + parameters[displayOption] = optionValue; + } + newNodeData.parameters = { + ...newNodeData.parameters, + ...parameters, + }; + } + } + } + } + return newNodeData; + }, + + async injectNode (nodeTypeName: string, options: AddNodeOptions = {}) { const nodeTypeData: INodeTypeDescription | null = this.$store.getters['nodeTypes/getNodeType'](nodeTypeName); if (nodeTypeData === null) { @@ -1385,16 +1453,7 @@ export default mixins( return; } - const newNodeData: INodeUi = { - id: uuid(), - name: nodeTypeData.defaults.name as string, - type: nodeTypeData.name, - typeVersion: Array.isArray(nodeTypeData.version) - ? nodeTypeData.version.slice(-1)[0] - : nodeTypeData.version, - position: [0, 0], - parameters: {}, - }; + const newNodeData = await this.getNewNodeWithDefaultCredential(nodeTypeData); // when pulling new connection from node or injecting into a connection const lastSelectedNode = this.lastSelectedNode;