From 6eb585dc1a6053e236285cfb6ffb44a936846edd Mon Sep 17 00:00:00 2001 From: Stamsy Date: Tue, 26 Nov 2024 16:47:14 +0200 Subject: [PATCH] add search method to get group from list --- .../nodes/Aws/Cognito/AwsCognito.node.ts | 3 +- .../nodes/Aws/Cognito/GenericFunctions.ts | 45 +++++++++++++------ .../Cognito/descriptions/GroupDescription.ts | 21 +++++---- 3 files changed, 47 insertions(+), 22 deletions(-) diff --git a/packages/nodes-base/nodes/Aws/Cognito/AwsCognito.node.ts b/packages/nodes-base/nodes/Aws/Cognito/AwsCognito.node.ts index e213931858..cf3cad8943 100644 --- a/packages/nodes-base/nodes/Aws/Cognito/AwsCognito.node.ts +++ b/packages/nodes-base/nodes/Aws/Cognito/AwsCognito.node.ts @@ -9,7 +9,7 @@ import { groupOperations, groupFields, } from './descriptions'; -import { presendStringifyBody, searchUserPools } from './GenericFunctions'; +import { presendStringifyBody, searchUserPools, searchGroups } from './GenericFunctions'; export class AwsCognito implements INodeType { description: INodeTypeDescription = { @@ -87,6 +87,7 @@ export class AwsCognito implements INodeType { methods = { listSearch: { searchUserPools, + searchGroups, // Todo: Add more search methods }, }; diff --git a/packages/nodes-base/nodes/Aws/Cognito/GenericFunctions.ts b/packages/nodes-base/nodes/Aws/Cognito/GenericFunctions.ts index a6b685df0a..d41a10be7d 100644 --- a/packages/nodes-base/nodes/Aws/Cognito/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Aws/Cognito/GenericFunctions.ts @@ -382,37 +382,56 @@ export async function searchGroups( filter?: string, paginationToken?: string, ): Promise { + // Get the userPoolId from the input + const userPoolIdRaw = this.getNodeParameter('userPoolId', '') as IDataObject; + console.log('Raw User Pool ID:', userPoolIdRaw); + + // Extract the actual value + const userPoolId = userPoolIdRaw.value as string; + + // Ensure that userPoolId is provided + if (!userPoolId) { + throw new ApplicationError('User Pool ID is required to search groups'); + } + // Setup the options for the AWS request const opts: IHttpRequestOptions = { - url: '', // the base url is set in "awsRequest" + url: '', // the base URL is set in "awsRequest" method: 'POST', headers: { 'X-Amz-Target': 'AWSCognitoIdentityProviderService.ListGroups', }, body: JSON.stringify({ - MaxResults: 60, // the maximum number by documentation is 60 + UserPoolId: userPoolId, + MaxResults: 60, NextToken: paginationToken ?? undefined, }), }; + const responseData: IDataObject = await awsRequest.call(this, opts); - const groups = responseData.Groups as Array<{ Name: string; Id: string }>; + const groups = responseData.Groups as Array<{ GroupName?: string }> | undefined; + // If no groups exist, return an empty list + if (!groups) { + return { results: [] }; + } + + // Map and filter the response const results: INodeListSearchItems[] = groups - .map((a) => ({ - name: a.Name, - value: a.Id, + .filter((group) => group.GroupName) + .map((group) => ({ + name: group.GroupName as string, + value: group.GroupName as string, })) .filter( - (a) => + (group) => !filter || - a.name.toLowerCase().includes(filter.toLowerCase()) || - a.value.toLowerCase().includes(filter.toLowerCase()), + group.name.toLowerCase().includes(filter.toLowerCase()) || + group.value.toLowerCase().includes(filter.toLowerCase()), ) .sort((a, b) => { - if (a.name.toLowerCase() < b.name.toLowerCase()) return -1; - if (a.name.toLowerCase() > b.name.toLowerCase()) return 1; - return 0; + return a.name.toLowerCase().localeCompare(b.name.toLowerCase()); }); - return { results, paginationToken: responseData.NextToken }; // ToDo: Test if pagination for the search methods works + return { results, paginationToken: responseData.NextToken }; } diff --git a/packages/nodes-base/nodes/Aws/Cognito/descriptions/GroupDescription.ts b/packages/nodes-base/nodes/Aws/Cognito/descriptions/GroupDescription.ts index 78e7ea9a6d..7a65c0d0cd 100644 --- a/packages/nodes-base/nodes/Aws/Cognito/descriptions/GroupDescription.ts +++ b/packages/nodes-base/nodes/Aws/Cognito/descriptions/GroupDescription.ts @@ -394,7 +394,7 @@ const getFields: INodeProperties[] = [ }, modes: [ { - displayName: 'From list', // ToDo: Fix error when selecting this option + displayName: 'From list', name: 'list', type: 'list', typeOptions: { @@ -427,7 +427,12 @@ const getFields: INodeProperties[] = [ type: 'resourceLocator', default: { mode: 'list', value: '' }, description: 'The name of the group to retrieve', - displayOptions: { show: { resource: ['group'], operation: ['get'] } }, + displayOptions: { + show: { + resource: ['group'], + operation: ['get'], + }, + }, routing: { send: { type: 'body', @@ -440,13 +445,13 @@ const getFields: INodeProperties[] = [ name: 'list', type: 'list', typeOptions: { - searchListMethod: 'listGroups', + searchListMethod: 'searchGroups', searchable: true, }, }, { displayName: 'By Name', - name: 'id', + name: 'GroupName', type: 'string', hint: 'Enter the group name', validation: [ @@ -454,7 +459,7 @@ const getFields: INodeProperties[] = [ type: 'regex', properties: { regex: '^[\\w+=,.@-]+$', - errorMessage: 'The group name must follow the pattern "xxxxxx_xxxxxxxxxxx"', + errorMessage: 'The group name must follow the allowed pattern.', }, }, ], @@ -700,13 +705,13 @@ const updateFields: INodeProperties[] = [ name: 'list', type: 'list', typeOptions: { - searchListMethod: 'listGroups', + searchListMethod: 'searchGroups', searchable: true, }, }, { displayName: 'By Name', - name: 'id', + name: 'GroupName', type: 'string', hint: 'Enter the group name', validation: [ @@ -714,7 +719,7 @@ const updateFields: INodeProperties[] = [ type: 'regex', properties: { regex: '^[\\w+=,.@-]+$', - errorMessage: 'The group name must follow the pattern "xxxxxx_xxxxxxxxxxx"', + errorMessage: 'The group name must follow the allowed pattern.', }, }, ],