Skip to content

Commit

Permalink
feat(designer): [ConnectionCreation] Adding hidden parameter field in…
Browse files Browse the repository at this point in the history
… ConnectionCreationInfo to pass selected credential id (#4193)

* adding parameter callback

* Adding tests
  • Loading branch information
adboillo authored Feb 15, 2024
1 parent 2137fd2 commit 2d43902
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MockHttpClient } from '../../../../../__test__/mock-http-client';
import { CreateConnection, type CreateConnectionProps } from '../createConnection';
import { CreateConnection, parseParameterValues, type CreateConnectionProps } from '../createConnection';
import { UniversalConnectionParameter } from '../formInputs/universalConnectionParameter';
import {
InitConnectionParameterEditorService,
Expand Down Expand Up @@ -578,6 +578,35 @@ describe('ui/createConnection', () => {
const mappingEditors = findParameterComponents(createConnection, CustomCredentialMappingEditor);
expect(mappingEditors).toHaveLength(0);
});

test('parseParameterValues', () => {
const parameterValues: Record<string, any> = {
a: 'foobar',
b: 42,
c: null,
d: undefined,
e: { foo: 'bar' },
f: ['id', 66],
};
const capabilityEnabledParameters: Record<string, ConnectionParameter> = {
a: { type: 'connection' },
z: { type: 'other' },
};

const { visibleParameterValues, additionalParameterValues } = parseParameterValues(parameterValues, capabilityEnabledParameters);
expect(visibleParameterValues).toStrictEqual({ a: 'foobar' });
expect(additionalParameterValues).toStrictEqual({
b: 42,
c: null,
d: undefined,
e: { foo: 'bar' },
f: ['id', 66],
});

const emptyParameters = parseParameterValues({}, capabilityEnabledParameters);
expect(emptyParameters.visibleParameterValues).toStrictEqual({});
expect(emptyParameters.additionalParameterValues).toStrictEqual({});
});
});

function findConnectionCreateDiv(createConnection: ReactElement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ export interface CreateConnectionProps {
parameterValues?: Record<string, any>,
isOAuthConnection?: boolean,
alternativeParameterValues?: Record<string, any>,
identitySelected?: string
identitySelected?: string,
additionalParameterValues?: Record<string, any>
) => void;
cancelCallback?: () => void;
hideCancelButton?: boolean;
Expand Down Expand Up @@ -301,9 +302,7 @@ export const CreateConnection = (props: CreateConnectionProps) => {
const canSubmit = useMemo(() => !isLoading && validParams, [isLoading, validParams]);

const submitCallback = useCallback(() => {
const visibleParameterValues = Object.fromEntries(
Object.entries(parameterValues).filter(([key]) => Object.keys(capabilityEnabledParameters).includes(key)) ?? []
);
const { visibleParameterValues, additionalParameterValues } = parseParameterValues(parameterValues, capabilityEnabledParameters);

// This value needs to be passed conditionally but the parameter is hidden, so we're manually inputting it here
if (
Expand All @@ -326,7 +325,8 @@ export const CreateConnection = (props: CreateConnectionProps) => {
visibleParameterValues,
isUsingOAuth,
alternativeParameterValues,
identitySelected
identitySelected,
additionalParameterValues
);
}, [
parameterValues,
Expand Down Expand Up @@ -654,3 +654,17 @@ const isServicePrincipalParameterVisible = (key: string, parameter: any): boolea
if (constraints?.hidden === 'true' || constraints?.hideInUI === 'true') return false;
return true;
};

export function parseParameterValues(
parameterValues: Record<string, any>,
capabilityEnabledParameters: Record<string, ConnectionParameter | ConnectionParameterSetParameter>
) {
const visibleParameterValues = Object.fromEntries(
Object.entries(parameterValues).filter(([key]) => Object.keys(capabilityEnabledParameters).includes(key)) ?? []
);
const additionalParameterValues = Object.fromEntries(
Object.entries(parameterValues).filter(([key]) => !Object.keys(capabilityEnabledParameters).includes(key)) ?? []
);

return { visibleParameterValues, additionalParameterValues };
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ export const CreateConnectionWrapper = () => {
parameterValues: Record<string, any> = {},
isOAuthConnection?: boolean,
alternativeParameterValues?: Record<string, any>,
identitySelected?: string
identitySelected?: string,
additionalParameterValues?: Record<string, any>
) => {
if (!connector?.id) return;

Expand Down Expand Up @@ -188,6 +189,7 @@ export const CreateConnectionWrapper = () => {
: undefined,
connectionParameters: outputParameterValues,
alternativeParameterValues,
additionalParameterValues,
};

const parametersMetadata: ConnectionParametersMetadata = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface ConnectionCreationInfo {
displayName?: string;
parameterName?: string;
appSettings?: Record<string, string>;
additionalParameterValues?: Record<string, string>;
}

export interface ConnectionParametersMetadata {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ interface ServiceProviderConnectionModel {
};
parameterSetName?: string;
displayName?: string;
additionalParameterValues?: Record<string, string>;
}

interface FunctionsConnectionModel {
Expand Down Expand Up @@ -605,6 +606,7 @@ function convertToServiceProviderConnectionsData(
displayName,
connectionParameters: connectionParameterValues,
connectionParametersSet: connectionParametersSetValues,
additionalParameterValues,
} = connectionInfo;
const connectionParameters = connectionParametersSetValues
? connectionParameterMetadata.connectionParameterSet?.parameters
Expand All @@ -626,6 +628,7 @@ function convertToServiceProviderConnectionsData(
...optional('parameterSetName', connectionParametersSetValues?.name),
serviceProvider: { id: connectorId },
displayName,
...optional('additionalParameterValues', additionalParameterValues),
},
settings: connectionInfo.appSettings ?? {},
pathLocation: [serviceProviderLocation],
Expand Down

0 comments on commit 2d43902

Please sign in to comment.