From 5417ab348682b24ff8e3b1213dbb69e5f3978404 Mon Sep 17 00:00:00 2001 From: kahummer Date: Tue, 10 Sep 2024 00:53:31 +0300 Subject: [PATCH] Extend form values getter to include extra field --- .../src/components/forms/UserForm/utils.tsx | 40 +++++++++++++++++-- .../src/ducks/user.ts | 2 + 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/packages/keycloak-user-management/src/components/forms/UserForm/utils.tsx b/packages/keycloak-user-management/src/components/forms/UserForm/utils.tsx index 2d5f9fb78..6367d0aa7 100644 --- a/packages/keycloak-user-management/src/components/forms/UserForm/utils.tsx +++ b/packages/keycloak-user-management/src/components/forms/UserForm/utils.tsx @@ -99,8 +99,9 @@ const createEditKeycloakUser = async ( `${KEYCLOAK_URL_USERS}/${keycloakUserPayload.id}`, keycloakBaseURL ); + const { nationalId, phoneNumber, ...coreUserPayload } = keycloakUserPayload; return serve - .update(keycloakUserPayload) + .update(coreUserPayload) .then(() => { sendSuccessNotification(t('User edited successfully')); updateGroupsAndPractitionerCallback(keycloakUserPayload.id).catch(() => @@ -111,10 +112,11 @@ const createEditKeycloakUser = async ( throw error; }); } else { + const { nationalId, phoneNumber, ...coreUserPayload } = keycloakUserPayload; // create new keycloak user const serve = new KeycloakService(KEYCLOAK_URL_USERS, keycloakBaseURL); return serve - .create(keycloakUserPayload) + .create(coreUserPayload) .then((res) => { sendSuccessNotification(t('User created successfully')); const keycloakUserId = getUserId(res); @@ -255,6 +257,20 @@ export const getFormValues = ( const { contact: contacts, fhir_core_app_id: fhirCoreAppId } = keycloakUser.attributes ?? {}; const { active } = practitioner ?? {}; + let nationalId = ''; + let phoneNumber = ''; + + if (Array.isArray(practitioner?.identifier)) { + nationalId = + practitioner?.identifier?.find((item: any) => item?.type?.text === 'National ID')?.value || + ''; + } + if (Array.isArray((practitioner as IPractitioner)?.telecom)) { + phoneNumber = + (practitioner as IPractitioner).telecom?.find((telco) => telco.system === 'phone')?.value || + ''; + } + let userType: FormFields['userType'] = 'practitioner'; if (practitionerRole) { @@ -274,6 +290,8 @@ export const getFormValues = ( id, firstName, lastName, + nationalId, + phoneNumber, email, username, enabled, @@ -298,7 +316,18 @@ export const getUserAndGroupsPayload = (values: FormFields) => { const isEditMode = !!values.id; // possibility of creating a practitioner for an existing user if one was not created before - const { id, username, firstName, lastName, email, enabled, contact, fhirCoreAppId } = values; + const { + id, + username, + firstName, + lastName, + nationalId, + phoneNumber, + email, + enabled, + contact, + fhirCoreAppId, + } = values; const preUserAttributes = { ...(contact ? { contact: [contact] } : {}), ...(fhirCoreAppId ? { fhir_core_app_id: [fhirCoreAppId] } : {}), @@ -315,6 +344,8 @@ export const getUserAndGroupsPayload = (values: FormFields) => { firstName, id: isEditMode ? id : '', // id is generated by keycloak for after POST new user lastName, + nationalId, + phoneNumber, username, ...(email ? { email } : {}), enabled, @@ -365,6 +396,7 @@ export const postPutPractitioner = // otherwise follow the practitioner's activation field const practitionerActive = enabled === false ? false : active === undefined ? false : active; const practObj = values.practitioner as Practitioner | undefined; + if (practObj?.identifier) { practitioner = { ...practObj, @@ -375,7 +407,7 @@ export const postPutPractitioner = }; } - const practitionerIsEditMode = !!values.practitioner?.identifier; + const practitionerIsEditMode = !!practObj?.identifier; return createOrEditPractitioners(baseUrl, practitioner, practitionerIsEditMode, t); }; diff --git a/packages/keycloak-user-management/src/ducks/user.ts b/packages/keycloak-user-management/src/ducks/user.ts index c0d5b2d86..f822088a1 100644 --- a/packages/keycloak-user-management/src/ducks/user.ts +++ b/packages/keycloak-user-management/src/ducks/user.ts @@ -60,6 +60,8 @@ export interface KeycloakUser { firstName: string; id: string; lastName: string; + nationalId: string; + phoneNumber: string; notBefore?: number; requiredActions?: string[]; totp?: boolean;