Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

populate mamba reports #1847

Merged
merged 9 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions packages/esm-commons-lib/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,55 @@ export async function fetchMambaReportData(reportId: string) {
throw new Error(`Error fetching data for report_id=${reportId}: ${error}`);
}
}

export async function fetchData(
reportType: 'fetchMambaAncData' | 'MotherHivStatus',
reportId?: string,
patientUuid?: string,
pTrackerId?: string,
) {
try {
let endpoint = '';
switch (reportType) {
case 'fetchMambaAncData':
endpoint = `ws/rest/v1/mamba/report?report_id=${reportId}&person_uuid=${patientUuid}`;
break;
case 'MotherHivStatus':
endpoint = `ws/rest/v1/mamba/report?report_id=${reportId}&ptracker_id=${pTrackerId}&person_uuid=${patientUuid}`;
break;
default:
throw new Error('Invalid report type');
}

const response = await openmrsFetch(endpoint);
const data = await response.json();

if (data && data.results && data.results.length > 0) {
lucyjemutai marked this conversation as resolved.
Show resolved Hide resolved
const record = data.results[0].record;

for (const item of record) {
if (!isNaN(item.value as any)) {
return parseInt(item.value, 10);
} else if (isInvalidValue(item.value)) {
return '--';
} else {
return item.value;
}
}
}

return '--';
} catch (error) {
console.error(`Error fetching data for report_id=${reportId}: `, error);
throw new Error(`Error fetching data for report_id=${reportId}: ${error}`);
}
}

function isInvalidValue(value: any): boolean {
if (typeof value === 'string') {
return value.trim() === '';
} else if (value instanceof Date) {
return isNaN(value.getTime());
}
return false;
}
1 change: 1 addition & 0 deletions packages/esm-commons-lib/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export interface DashboardConfig extends DashboardLinkConfig {

export interface PatientChartProps {
patientUuid: string;
pTrackerId?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,29 @@ import styles from '../common.scss';
import { PatientChartProps } from '@ohri/openmrs-esm-ohri-commons-lib';
import CurrentPregnancy from './tabs/current-pregnancy.component';
import HivExposedInfant from './tabs/hiv-exposed-infant.component';
import { usePatient } from '@openmrs/esm-framework';
import { usePatient, useConfig } from '@openmrs/esm-framework';
import dayjs from 'dayjs';

const MaternalSummary: React.FC<PatientChartProps> = ({ patientUuid }) => {
const { t } = useTranslation();
const { identifiersTypes } = useConfig();
const { patient, isLoading } = usePatient(patientUuid);
const dob = patient?.birthDate;
const age = useMemo(() => dayjs().diff(dayjs(patient?.birthDate), 'year'), [patient?.birthDate]);
const [pTrackerId, setPtrackerId] = useState('');

useEffect(() => {
if (patient) {
const reversedIdentifiers = patient.identifier.slice().reverse();
lucyjemutai marked this conversation as resolved.
Show resolved Hide resolved
const pTrackerIdentifier = reversedIdentifiers.find((identifier) => {
return identifier.type.coding[0].code === identifiersTypes.ptrackerIdentifierType;
});

if (pTrackerIdentifier) {
setPtrackerId(pTrackerIdentifier.value);
}
}
}, [identifiersTypes.PTrackerIdentifierType, identifiersTypes.ptrackerIdentifierType, patient]);
lucyjemutai marked this conversation as resolved.
Show resolved Hide resolved

return (
<>
Expand All @@ -27,7 +42,7 @@ const MaternalSummary: React.FC<PatientChartProps> = ({ patientUuid }) => {
</TabList>
<TabPanels>
<TabPanel>
<CurrentPregnancy patientUuid={patientUuid} />
<CurrentPregnancy patientUuid={patientUuid} pTrackerId={pTrackerId} />
</TabPanel>
</TabPanels>
</Tabs>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
fetchPatientLastEncounter,
SummaryCardColumn,
SummaryCard,
fetchMambaReportData,
fetchData,
} from '@ohri/openmrs-esm-ohri-commons-lib';
import dayjs from 'dayjs';
import { moduleName } from '../../..';
Expand All @@ -35,7 +35,7 @@ export interface familyItemProps {
hivStatus: string;
finalOutcome: string;
}
const CurrentPregnancy: React.FC<PatientChartProps> = ({ patientUuid }) => {
const CurrentPregnancy: React.FC<PatientChartProps> = ({ patientUuid, pTrackerId }) => {
const { t } = useTranslation();
const currentPregnancyHeader = t('recentPregnancy', 'Recent Pregnancy');
const arvTherapyHeader = t('art', 'ART');
Expand All @@ -49,21 +49,36 @@ const CurrentPregnancy: React.FC<PatientChartProps> = ({ patientUuid }) => {
const [infantOutcomes, setInfantOutcomes] = useState([]);
const { formNames, encounterTypes, obsConcepts, formUuids } = useConfig();
const [totalAncCount, setTotalAncCount] = useState(null);
const [motherStatus, setMotherStatus] = useState(null);
const [deliveryDate, setDeliveryDate] = useState(null);
const [motherHivStatus, setMotherHivStatus] = useState(null);
const [loading, setLoading] = useState(true);

useEffect(() => {
const fetchData = async () => {
const fetchDataAndSetState = async () => {
try {
const [totalAncCount] = await Promise.all([fetchMambaReportData('no_of_anc_visits')]);
setLoading(true);
const [totalAncCount, motherStatus, deliveryDate, motherHivStatus] = await Promise.all([
lucyjemutai marked this conversation as resolved.
Show resolved Hide resolved
fetchData('fetchMambaAncData', 'no_of_anc_visits', patientUuid),
fetchData('fetchMambaAncData', 'mother_status', patientUuid),
fetchData('fetchMambaAncData', 'estimated_date_of_delivery', patientUuid),
fetchData('MotherHivStatus', 'mother_hiv_status', patientUuid, pTrackerId),
]);

setTotalAncCount(totalAncCount);
setMotherStatus(motherStatus);
setDeliveryDate(deliveryDate);
setMotherHivStatus(motherHivStatus);
} catch (error) {
console.error('Error fetching data:', error);
throw new Error('Error fetching data. Please try again.');
} finally {
setLoading(false);
}
};

fetchData();
}, []);
fetchDataAndSetState();
}, [patientUuid, pTrackerId]);

const headersFamily = [
{
Expand Down Expand Up @@ -237,58 +252,29 @@ const CurrentPregnancy: React.FC<PatientChartProps> = ({ patientUuid }) => {
{
key: 'motherHIVStatus',
header: t('motherHIVStatus', 'Mother HIV Status'),
encounterTypes: [encounterTypes.motherPostnatal, encounterTypes.labourAndDelivery, encounterTypes.antenatal],
getObsValue: (encounters) => {
const pncArtData = {
artInitiation: getObsFromEncounter(encounters[0], obsConcepts.artInitiationConcept),
motherHIVStatus: getObsFromEncounter(encounters[0], obsConcepts.hivTestResultConcept),
pTrackerId: getObsFromEncounter(encounters[0], obsConcepts.pTrackerIdConcept),
};
const lndArtData = {
artInitiation: getObsFromEncounter(encounters[1], obsConcepts.artInitiationConcept),
motherHIVStatus: getObsFromEncounter(encounters[1], obsConcepts.hivTestResultConcept),
pTrackerId: getObsFromEncounter(encounters[1], obsConcepts.pTrackerIdConcept),
};
const ancArtData = {
artInitiation: getObsFromEncounter(encounters[2], obsConcepts.artInitiationConcept),
motherHIVStatus: getObsFromEncounter(encounters[2], obsConcepts.hivTestResultConcept),
pTrackerId: getObsFromEncounter(encounters[2], obsConcepts.pTrackerIdConcept),
};
const latestArtData = getLatestArtDetails(pncArtData, lndArtData, ancArtData);
if (!latestArtData['motherHIVStatus']) {
return '--';
}

return latestArtData['motherHIVStatus'];
encounterTypes: [encounterTypes.labourAndDelivery],
getObsValue: async ([encounter]) => {
lucyjemutai marked this conversation as resolved.
Show resolved Hide resolved
return motherHivStatus;
},
},

{
key: 'expectedDeliveryDate',
header: t('expectedDeliveryDate', 'Expected Delivery Date'),
encounterTypes: [encounterTypes.antenatal],
encounterTypes: [encounterTypes.labourAndDelivery],
getObsValue: async ([encounter]) => {
return getObsFromEncounter(encounter, obsConcepts.eDDConcept, true);
},
getObsSummary: ([encounter]) => {
let edd = getObsFromEncounter(encounter, obsConcepts.eDDConcept, true);
if (edd !== '--') {
const days = calculateDateDifferenceInDate(edd);
edd = edd > 0 ? `In ${days}` : '';
}
return edd;
return deliveryDate;
lucyjemutai marked this conversation as resolved.
Show resolved Hide resolved
},
},
{
key: 'motherStatus',
header: t('motherStatus', 'Mother Status'),
encounterTypes: [encounterTypes.labourAndDelivery],
getObsValue: async ([encounter]) => {
return getObsFromEncounter(encounter, obsConcepts.motherStatusConcept);
return motherStatus;
lucyjemutai marked this conversation as resolved.
Show resolved Hide resolved
},
},
],
[],
[motherStatus, deliveryDate],
);

const arvTherapyColumns: SummaryCardColumn[] = useMemo(
Expand Down
Loading