From 654af7fa09ef759623cf80c1edd50f5f131cd13d Mon Sep 17 00:00:00 2001 From: its-kios09 Date: Tue, 21 Jan 2025 14:42:18 +0300 Subject: [PATCH] (refactor) updated as PR suggestion --- .../modal/provider-sync-modal.component.tsx | 139 +++++++----------- .../src/table/provider-details.component.tsx | 41 +++++- 2 files changed, 89 insertions(+), 91 deletions(-) diff --git a/packages/esm-providers-app/src/modal/provider-sync-modal.component.tsx b/packages/esm-providers-app/src/modal/provider-sync-modal.component.tsx index a02d00a5..633215f3 100644 --- a/packages/esm-providers-app/src/modal/provider-sync-modal.component.tsx +++ b/packages/esm-providers-app/src/modal/provider-sync-modal.component.tsx @@ -21,6 +21,7 @@ const HWRSyncModal: React.FC = ({ close, provider }) => { const { t } = useTranslation(); const [syncLoading, setSyncLoading] = useState(false); + const config = useConfig(); const { providerNationalIdUuid, licenseBodyUuid, @@ -32,127 +33,97 @@ const HWRSyncModal: React.FC = ({ close, provider }) => { qualificationUuid, providerAddressUuid, providerHieFhirReference, - } = useConfig(); - - const providerNationalId = provider.attributes.find((attr) => attr.attributeType.uuid === providerNationalIdUuid); - const registrationNumber = provider.attributes.find((attr) => attr.attributeType.uuid === licenseBodyUuid); - const passPortNumber = provider.attributes.find((attr) => attr.attributeType.uuid === passportNumberUuid); + } = config; + + const attributeMapping = { + [identifierTypes[0]?.key]: + provider.attributes.find((attr) => attr.attributeType.uuid === providerNationalIdUuid)?.value || '--', + [identifierTypes[1]?.key]: + provider.attributes.find((attr) => attr.attributeType.uuid === licenseBodyUuid)?.value || '--', + [identifierTypes[2]?.key]: + provider.attributes.find((attr) => attr.attributeType.uuid === passportNumberUuid)?.value || '--', + }; const [searchHWR, setSearchHWR] = useState({ identifierType: identifierTypes[0]?.key, - identifier: providerNationalId?.value, + identifier: attributeMapping[identifierTypes[0]?.key], }); const handleIdentifierTypeChange = (selectedItem: { key: string; name: string } | null) => { const selectedKey = selectedItem?.key ?? ''; - const identifierValue = - selectedKey === identifierTypes[0]?.key - ? providerNationalId?.value || '--' - : selectedKey === identifierTypes[1]?.key - ? registrationNumber?.value || '--' - : selectedKey === identifierTypes[2]?.key - ? passPortNumber?.value || '--' - : ''; - setSearchHWR((prev) => ({ ...prev, identifierType: selectedKey, - identifier: identifierValue, + identifier: attributeMapping[selectedKey] || '', })); }; - const isSearchDisabled = () => { - const { identifierType } = searchHWR; - return ( - (identifierType === identifierTypes[0]?.key && !providerNationalId?.value) || - (identifierType === identifierTypes[1]?.key && !registrationNumber?.value) || - (identifierType === identifierTypes[2]?.key && !passPortNumber?.value) - ); - }; + const isSearchDisabled = () => !searchHWR.identifier; const handleSync = async () => { try { setSyncLoading(true); const healthWorker: Practitioner = await searchHealthCareWork(searchHWR.identifierType, searchHWR.identifier); - const licenseNumber = healthWorker.entry[0]?.resource.identifier?.find((id) => - id.type?.coding?.some((code) => code.code === 'license-number'), - )?.value; - const regNumber = healthWorker?.entry[0]?.resource.identifier?.find((id) => - id.type?.coding?.some((code) => code.code === 'board-registration-number'), - )?.value; - const licenseDate = formatDate( - new Date( - healthWorker?.entry[0]?.resource.identifier?.find((id) => - id.type?.coding?.some((code) => code.code === 'license-number'), - )?.period?.end, + const resource = healthWorker.entry[0]?.resource; + + const extractedAttributes = { + licenseNumber: resource?.identifier?.find((id) => + id.type?.coding?.some((code) => code.code === 'license-number'), + )?.value, + regNumber: resource?.identifier?.find((id) => + id.type?.coding?.some((code) => code.code === 'board-registration-number'), + )?.value, + licenseDate: formatDate( + new Date( + resource?.identifier?.find((id) => + id.type?.coding?.some((code) => code.code === 'license-number'), + )?.period?.end, + ), ), - ); - const phoneNumber = healthWorker?.entry[0]?.resource?.telecom?.find( - (contact) => contact.system === 'phone', - )?.value; - const email = healthWorker?.entry[0]?.resource?.telecom?.find((contact) => contact.system === 'email')?.value; - - const qualification = - healthWorker?.entry[0]?.resource?.qualification?.[0]?.code?.coding?.[0]?.display || - healthWorker?.entry[0]?.resource?.extension?.find( - (ext) => ext.url === 'https://ts.kenya-hie.health/Codesystem/specialty', - )?.valueCodeableConcept?.coding?.[0]?.display; - - const fhirBundle = JSON.stringify(healthWorker); + phoneNumber: resource?.telecom?.find((contact) => contact.system === 'phone')?.value, + email: resource?.telecom?.find((contact) => contact.system === 'email')?.value, + qualification: + resource?.qualification?.[0]?.code?.coding?.[0]?.display || + resource?.extension?.find((ext) => ext.url === 'https://ts.kenya-hie.health/Codesystem/specialty') + ?.valueCodeableConcept?.coding?.[0]?.display, + }; const updatableAttributes = [ - { - attributeType: licenseNumberUuid, - value: licenseNumber, - }, - { - attributeType: licenseBodyUuid, - value: regNumber, - }, - { - attributeType: licenseExpiryDateUuid, - value: parseDate(licenseDate), - }, - { - attributeType: phoneNumberUuid, - value: phoneNumber, - }, - { - attributeType: qualificationUuid, - value: qualification, - }, - { - attributeType: providerHieFhirReference, - value: fhirBundle, - }, - { - attributeType: providerAddressUuid, - value: email, - }, - ]; + { attributeType: licenseNumberUuid, value: extractedAttributes.licenseNumber }, + { attributeType: licenseBodyUuid, value: extractedAttributes.regNumber }, + { attributeType: licenseExpiryDateUuid, value: parseDate(extractedAttributes.licenseDate) }, + { attributeType: phoneNumberUuid, value: extractedAttributes.phoneNumber }, + { attributeType: qualificationUuid, value: extractedAttributes.qualification }, + { attributeType: providerHieFhirReference, value: JSON.stringify(healthWorker) }, + { attributeType: providerAddressUuid, value: extractedAttributes.email }, + ].filter((attr) => attr.value !== undefined && attr.value !== null && attr.value !== ''); await Promise.all( updatableAttributes.map((attr) => { - const _attribute = provider.attributes.find((at) => at.attributeType.uuid === attr.attributeType)?.uuid; - if (!_attribute) { + const existingAttribute = provider.attributes.find( + (at) => at.attributeType.uuid === attr.attributeType, + )?.uuid; + if (!existingAttribute) { return createProviderAttribute(attr, provider.uuid); } - return updateProviderAttributes({ value: attr.value }, provider.uuid, _attribute); + return updateProviderAttributes({ value: attr.value }, provider.uuid, existingAttribute); }), ); mutate((key) => typeof key === 'string' && key.startsWith('/ws/rest/v1/provider')); - showSnackbar({ title: 'Success', kind: 'success', subtitle: t('syncmsg', 'Account synced successfully!') }); + showSnackbar({ + title: 'Success', + kind: 'success', + subtitle: t('syncmsg', 'Account synced successfully!'), + }); close(); } catch (err) { - const { identifierType, identifier } = searchHWR; showToast({ critical: false, kind: 'error', - description: t('errorSyncMsg', `Failed to sync the account with ${identifier}. and ${err}`), + description: t('errorSyncMsg', `Failed to sync the account with ${searchHWR.identifier}. ${err}`), title: t('hwrERROR', 'Sync Failed'), - onActionButtonClick: () => close, }); } finally { setSyncLoading(false); diff --git a/packages/esm-providers-app/src/table/provider-details.component.tsx b/packages/esm-providers-app/src/table/provider-details.component.tsx index 2dd7f624..3a998278 100644 --- a/packages/esm-providers-app/src/table/provider-details.component.tsx +++ b/packages/esm-providers-app/src/table/provider-details.component.tsx @@ -12,19 +12,46 @@ interface ProviderDetailsProps { providerUuid: string; } +interface ProviderAttribute { + value: string; +} + +interface ProviderAttributes { + licenseAttr?: ProviderAttribute; + nationalID?: ProviderAttribute; + dateAttr?: ProviderAttribute; + phoneNumber?: ProviderAttribute; + qualification?: ProviderAttribute; + registrationNumber?: ProviderAttribute; + emailAddress?: ProviderAttribute; +} + const ProviderDetails: React.FC = ({ providerUuid }) => { const { t } = useTranslation(); const patientBannerRef = useRef(null); const { provider } = useProviderDetails(providerUuid); const { user, isLoading, error } = useProviderUser(providerUuid); - const licenseAttr = provider?.attributes?.find((attr) => attr.attributeType.display === 'Practising License Number'); - const nationalID = provider?.attributes?.find((attr) => attr.attributeType.display === 'Provider National Id Number'); - const dateAttr = provider?.attributes?.find((attr) => attr.attributeType.display === 'License Expiry Date'); - const phoneNumber = provider?.attributes?.find((attr) => attr.attributeType.display === 'Provider Telephone'); - const qualification = provider?.attributes?.find((attr) => attr.attributeType.display === 'Provider Qualification'); - const registrationNumber = provider?.attributes?.find((attr) => attr.attributeType.display === 'License Body'); - const emailAddress = provider?.attributes?.find((attr) => attr.attributeType.display === 'Provider Address'); + const attributeMap = { + licenseAttr: 'Practising License Number', + nationalID: 'Provider National Id Number', + dateAttr: 'License Expiry Date', + phoneNumber: 'Provider Telephone', + qualification: 'Provider Qualification', + registrationNumber: 'License Body', + emailAddress: 'Provider Address', + }; + + const attributes: ProviderAttributes = Object.entries(attributeMap).reduce((acc, [key, display]) => { + const attr = provider?.attributes?.find((attr) => attr.attributeType.display === display); + if (attr) { + acc[key as keyof ProviderAttributes] = { value: attr.value }; + } + return acc; + }, {} as ProviderAttributes); + + const { licenseAttr, nationalID, dateAttr, phoneNumber, qualification, registrationNumber, emailAddress } = + attributes; const formattedExpiryDate = dateAttr?.value ? dayjs(dateAttr.value).format('YYYY-MM-DD') : null; const today = dayjs();