Skip to content

Commit

Permalink
fix(console,core): fix SAML app attribute mapping issue
Browse files Browse the repository at this point in the history
  • Loading branch information
darcyYe committed Jan 23, 2025
1 parent ed55916 commit 210106e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { trySubmitSafe } from '@/utils/form';

import styles from './AttributeMapping.module.scss';
import { camelCaseToSentenceCase } from './utils';
import { conditionalArray } from '@silverhand/essentials';

const defaultFormValue: Array<[UserClaim | 'id' | '', string]> = [['id', '']];

Expand Down Expand Up @@ -102,12 +103,9 @@ function AttributeMapping({ data, mutateApplication }: Props) {
})
);

const existingKeys = useMemo(() => formValues.map(([key]) => key).filter(Boolean), [formValues]);

const availableKeys = useMemo(
() => completeUserClaims.filter((claim) => !existingKeys.includes(claim)),
[existingKeys]
);
// Not using `useMemo` to avoid the reappearance of the available keys when the form values change.
const existingKeys = formValues.map(([key]) => key).filter(Boolean);
const availableKeys = completeUserClaims.filter((claim) => !existingKeys.includes(claim));

return (
<DetailsForm
Expand Down Expand Up @@ -153,14 +151,14 @@ function AttributeMapping({ data, mutateApplication }: Props) {
<Select
isSearchEnabled
value={value}
options={[
...availableKeys.map((claim) => ({
options={conditionalArray(
availableKeys.map((claim) => ({
title: camelCaseToSentenceCase(claim),
value: claim,
})),
// If this is not specified, the component will fail to render the current value. The current value has been excluded in `availableKeys`.
{ value, title: camelCaseToSentenceCase(value) },
]}
// If this is not specified, the component will fail to render the current value. The current value has been excluded in `availableKeys`. But we should not show if the current value is empty.
value.trim() && [{ title: camelCaseToSentenceCase(value), value }]
)}
onChange={(value) => {
onChange(value);
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ export const createSamlApplicationsLibrary = (queries: Queries) => {
patchApplicationObject: PatchSamlApplication
): Promise<SamlApplicationResponse> => {
const { name, description, customData, ...config } = patchApplicationObject;
const originalApplication = await findApplicationById(id);
const [originalApplication, originalAppConfig] = await Promise.all([
findApplicationById(id),
findSamlApplicationConfigByApplicationId(id),
]);

Check warning on line 82 in packages/core/src/libraries/saml-application/saml-applications.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/libraries/saml-application/saml-applications.ts#L79-L82

Added lines #L79 - L82 were not covered by tests
const applicationData = removeUndefinedKeys(
pick(patchApplicationObject, 'name', 'description', 'customData')
);
Expand All @@ -95,11 +98,11 @@ export const createSamlApplicationsLibrary = (queries: Queries) => {
: originalApplication,
Object.keys(config).length > 0
? updateSamlApplicationConfig({
set: config,
set: { ...originalAppConfig, ...config },

Check warning on line 101 in packages/core/src/libraries/saml-application/saml-applications.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/libraries/saml-application/saml-applications.ts#L101

Added line #L101 was not covered by tests
where: { applicationId: id },
jsonbMode: 'merge',
jsonbMode: 'replace',

Check warning on line 103 in packages/core/src/libraries/saml-application/saml-applications.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/libraries/saml-application/saml-applications.ts#L103

Added line #L103 was not covered by tests
})
: findSamlApplicationConfigByApplicationId(id),
: originalAppConfig,

Check warning on line 105 in packages/core/src/libraries/saml-application/saml-applications.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/libraries/saml-application/saml-applications.ts#L105

Added line #L105 was not covered by tests
]);

return ensembleSamlApplication({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ describe('SAML application', () => {
},
},
},
{
name: 'Update with empty attribute mapping',
config: {
attributeMapping: {},
},
},
])('should update SAML application - %#', async ({ name, config }) => {
const formattedName = name ? `updated-${name}` : undefined;
const initConfig = {
Expand Down

0 comments on commit 210106e

Please sign in to comment.