Skip to content

Commit

Permalink
Extend form values getter to include extra field
Browse files Browse the repository at this point in the history
  • Loading branch information
kahummer committed Sep 9, 2024
1 parent 741834e commit 5417ab3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(() =>
Expand All @@ -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);
Expand Down Expand Up @@ -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 ||

Check warning on line 265 in packages/keycloak-user-management/src/components/forms/UserForm/utils.tsx

View workflow job for this annotation

GitHub Actions / test (22.x, ubuntu-latest)

Unexpected any. Specify a different type
'';
}
if (Array.isArray((practitioner as IPractitioner)?.telecom)) {

Check failure on line 268 in packages/keycloak-user-management/src/components/forms/UserForm/utils.tsx

View workflow job for this annotation

GitHub Actions / test (22.x, ubuntu-latest)

Unnecessary optional chain on a non-nullish value
phoneNumber =
(practitioner as IPractitioner).telecom?.find((telco) => telco.system === 'phone')?.value ||
'';
}

let userType: FormFields['userType'] = 'practitioner';

if (practitionerRole) {
Expand All @@ -274,6 +290,8 @@ export const getFormValues = (
id,
firstName,
lastName,
nationalId,
phoneNumber,
email,
username,
enabled,
Expand All @@ -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] } : {}),
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -375,7 +407,7 @@ export const postPutPractitioner =
};
}

const practitionerIsEditMode = !!values.practitioner?.identifier;
const practitionerIsEditMode = !!practObj?.identifier;

return createOrEditPractitioners(baseUrl, practitioner, practitionerIsEditMode, t);
};
Expand Down
2 changes: 2 additions & 0 deletions packages/keycloak-user-management/src/ducks/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export interface KeycloakUser {
firstName: string;
id: string;
lastName: string;
nationalId: string;
phoneNumber: string;
notBefore?: number;
requiredActions?: string[];
totp?: boolean;
Expand Down

0 comments on commit 5417ab3

Please sign in to comment.