Skip to content

Commit

Permalink
(refactor) updated as PR suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
its-kios09 committed Jan 21, 2025
1 parent e6bc5b7 commit 654af7f
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 91 deletions.
139 changes: 55 additions & 84 deletions packages/esm-providers-app/src/modal/provider-sync-modal.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const HWRSyncModal: React.FC<HWRSyncModalProps> = ({ close, provider }) => {
const { t } = useTranslation();
const [syncLoading, setSyncLoading] = useState(false);

const config = useConfig<ConfigObject>();
const {
providerNationalIdUuid,
licenseBodyUuid,
Expand All @@ -32,127 +33,97 @@ const HWRSyncModal: React.FC<HWRSyncModalProps> = ({ close, provider }) => {
qualificationUuid,
providerAddressUuid,
providerHieFhirReference,
} = useConfig<ConfigObject>();

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProviderDetailsProps> = ({ 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();
Expand Down

0 comments on commit 654af7f

Please sign in to comment.