Refactor to clean up

This commit is contained in:
Iván Ovejero 2022-05-03 09:28:50 +02:00
parent bd02a6127e
commit 791ed9566f

View file

@ -40,7 +40,7 @@ export const nodeHelpers = mixins(
) )
.extend({ .extend({
computed: { computed: {
...mapGetters('credentials', [ 'getCredentialTypeByName' ]), ...mapGetters('credentials', [ 'getCredentialTypeByName', 'getCredentialsByType' ]),
}, },
methods: { methods: {
@ -210,78 +210,37 @@ export const nodeHelpers = mixins(
nodeCredentialType: string; nodeCredentialType: string;
}; };
/**
* For HTTP Request node, report missing credential if no selected credential
* or if no selected credentials of the chosen generic auth type.
*/
if ( if (
node.type === HTTP_REQUEST_NODE_TYPE && isHttpRequestNodeV2(node) &&
node.typeVersion === 2 &&
authenticateWith === 'genericAuth' && authenticateWith === 'genericAuth' &&
( selectedCredsAreUnusable(node, genericAuthType)
node.credentials === undefined ||
Object.keys(node.credentials).includes(genericAuthType) === false
)
) { ) {
const credentialType = this.getCredentialTypeByName(genericAuthType); const credential = this.getCredentialTypeByName(genericAuthType);
return reportUnsetCredential(credential);
return {
credentials: {
[credentialType.name]: [`Credentials for "${credentialType.displayName}" are not set.`],
},
};
} }
/**
* For HTTP Request node, report missing credential if no selected credential
* and if ID of chosen credential ID does not match ID in DB, so it was deleted.
*/
if ( if (
node.type === HTTP_REQUEST_NODE_TYPE && isHttpRequestNodeV2(node) &&
node.typeVersion === 2 &&
authenticateWith === 'nodeCredential' && authenticateWith === 'nodeCredential' &&
nodeCredentialType !== '' && nodeCredentialType !== '' &&
node.credentials !== undefined node.credentials !== undefined
) { ) {
const credentialType = this.getCredentialTypeByName(nodeCredentialType); const stored = this.getCredentialsByType(nodeCredentialType);
const dbCredentialsForType: ICredentialsResponse[] | null = this.$store.getters['credentials/getCredentialsByType'](nodeCredentialType); if (selectedCredsWereDeleted(node, nodeCredentialType, stored)) {
const credential = this.getCredentialTypeByName(nodeCredentialType);
const selectedCredentials = node.credentials[nodeCredentialType]; return reportUnsetCredential(credential);
if (dbCredentialsForType !== null) {
const idMatch = dbCredentialsForType.find((c) => c.id === selectedCredentials.id);
if (!idMatch) {
return {
credentials: {
[credentialType.name]: [`Credentials for "${credentialType.displayName}" are not set.`],
},
};
}
} }
} }
/**
* For HTTP Request node, report missing credential if no selected credential
* or if no selected credentials of the chosen node credential type.
*/
if ( if (
node.type === HTTP_REQUEST_NODE_TYPE && isHttpRequestNodeV2(node) &&
node.typeVersion === 2 &&
authenticateWith === 'nodeCredential' && authenticateWith === 'nodeCredential' &&
nodeCredentialType !== '' && nodeCredentialType !== '' &&
( selectedCredsAreUnusable(node, nodeCredentialType)
node.credentials === undefined ||
Object.keys(node.credentials).includes(nodeCredentialType) === false
)
) { ) {
const credentialType = this.getCredentialTypeByName(nodeCredentialType); const credential = this.getCredentialTypeByName(nodeCredentialType);
return reportUnsetCredential(credential);
return {
credentials: {
[credentialType.name]: [`Credentials for "${credentialType.displayName}" are not set.`],
},
};
} }
for (const credentialTypeDescription of nodeType!.credentials!) { for (const credentialTypeDescription of nodeType!.credentials!) {
@ -480,3 +439,41 @@ export const nodeHelpers = mixins(
}, },
}, },
}); });
function isHttpRequestNodeV2(node: INodeUi) {
return node.type === HTTP_REQUEST_NODE_TYPE && node.typeVersion === 2;
}
/**
* Whether the node has no selected credentials, or none of the node's
* selected credentials are of the specified type.
*/
function selectedCredsAreUnusable(node: INodeUi, credentialType: string) {
return node.credentials === undefined || Object.keys(node.credentials).includes(credentialType) === false;
}
/**
* Whether the node's selected credentials of the specified type
* can no longer be found in the database.
*/
function selectedCredsWereDeleted(
node: INodeUi,
nodeCredentialType: string,
storedCredsByType: ICredentialsResponse[] | null,
) {
if (!node.credentials || !storedCredsByType) return false;
const selectedCredsByType = node.credentials[nodeCredentialType];
if (!selectedCredsByType) return false;
return !storedCredsByType.find((c) => c.id === selectedCredsByType.id);
}
function reportUnsetCredential(credentialType: ICredentialType) {
return {
credentials: {
[credentialType.name]: [`Credentials for "${credentialType.displayName}" are not set.`],
},
};
}