|
| 1 | +import { Coding } from 'fhir/r4b'; |
| 2 | +import { Moment } from 'moment'; |
| 3 | +import { useCallback, useState } from 'react'; |
| 4 | +import { SingleValue } from 'react-select'; |
| 5 | + |
| 6 | +import { service } from '@beda.software/emr/services'; |
| 7 | +import { formatFHIRDate, useService } from '@beda.software/fhir-react'; |
| 8 | +import { mapSuccess } from '@beda.software/remote-data'; |
| 9 | + |
| 10 | +export interface ChartData { |
| 11 | + title: string; |
| 12 | + code: string; |
| 13 | + count: number; |
| 14 | +} |
| 15 | +export interface AnalyticsFilters { |
| 16 | + selectedGender: SingleValue<Coding> | null; |
| 17 | + birthDateRange: [Moment | null, Moment | null] | null; |
| 18 | +} |
| 19 | + |
| 20 | +export function useAnalytics() { |
| 21 | + const [activeData, setActiveData] = useState<ChartData | null>(null); |
| 22 | + const [filters, setFilters] = useState<AnalyticsFilters>({ |
| 23 | + selectedGender: null, |
| 24 | + birthDateRange: null, |
| 25 | + }); |
| 26 | + const [response] = useService(async () => { |
| 27 | + const conditions: string[] = []; |
| 28 | + const params: (string | null)[] = []; |
| 29 | + |
| 30 | + if (filters.birthDateRange?.[0] && filters.birthDateRange?.[1]) { |
| 31 | + conditions.push("(p.resource#>>'{birthDate}')::date >= ? and (p.resource#>>'{birthDate}')::date <= ?"); |
| 32 | + params.push(formatFHIRDate(filters.birthDateRange[0]), formatFHIRDate(filters.birthDateRange[1])); |
| 33 | + } |
| 34 | + |
| 35 | + if (filters.selectedGender?.code) { |
| 36 | + conditions.push("p.resource#>>'{gender}' = ?"); |
| 37 | + params.push(filters.selectedGender.code); |
| 38 | + } |
| 39 | + |
| 40 | + const whereClause = conditions.length > 0 ? ` WHERE ${conditions.join(' AND ')}` : ''; |
| 41 | + const sqlQuery = `select i.resource#>>'{vaccineCode,coding,0,display}' as title, i.resource#>>'{vaccineCode,coding,0,code}' as code, count(i.id) from immunization i join patient p on i.resource#>>'{patient,id}' = p.id${whereClause} group by i.resource#>>'{vaccineCode,coding,0,display}', i.resource#>>'{vaccineCode,coding,0,code}'`; |
| 42 | + |
| 43 | + const sqlResponse = await service<ChartData[]>({ |
| 44 | + url: '/$sql', |
| 45 | + method: 'POST', |
| 46 | + data: [sqlQuery, ...params], |
| 47 | + }); |
| 48 | + return sqlResponse; |
| 49 | + }, [filters]); |
| 50 | + |
| 51 | + const handleGenderChange = useCallback((newValue: SingleValue<Coding>) => { |
| 52 | + setFilters((prev) => ({ |
| 53 | + ...prev, |
| 54 | + selectedGender: newValue, |
| 55 | + })); |
| 56 | + }, []); |
| 57 | + |
| 58 | + const handleDateRangeChange = useCallback((dates: [Moment | null, Moment | null] | null) => { |
| 59 | + setFilters((prev) => ({ |
| 60 | + ...prev, |
| 61 | + birthDateRange: dates, |
| 62 | + })); |
| 63 | + }, []); |
| 64 | + |
| 65 | + return { response, filters, handleGenderChange, handleDateRangeChange, activeData, setActiveData }; |
| 66 | +} |
| 67 | + |
| 68 | +export interface ActiveDataDetailsProps { |
| 69 | + activeData: ChartData; |
| 70 | + onClose: () => void; |
| 71 | + filters: AnalyticsFilters; |
| 72 | +} |
| 73 | + |
| 74 | +export function useActiveDataDetails(props: ActiveDataDetailsProps) { |
| 75 | + const { activeData, filters } = props; |
| 76 | + |
| 77 | + const [response] = useService(async () => { |
| 78 | + const patientConditions: string[] = []; |
| 79 | + const params: (string | null)[] = []; |
| 80 | + |
| 81 | + if (filters.birthDateRange?.[0] && filters.birthDateRange?.[1]) { |
| 82 | + patientConditions.push("(resource#>>'{birthDate}')::date >= ? and (resource#>>'{birthDate}')::date <= ?"); |
| 83 | + params.push(formatFHIRDate(filters.birthDateRange[0]), formatFHIRDate(filters.birthDateRange[1])); |
| 84 | + } |
| 85 | + |
| 86 | + if (filters.selectedGender?.code) { |
| 87 | + patientConditions.push("resource#>>'{gender}' = ?"); |
| 88 | + params.push(filters.selectedGender.code); |
| 89 | + } |
| 90 | + const immunizationConditions = ["i.resource#>>'{vaccineCode,coding,0,code}' = ?"]; |
| 91 | + params.push(activeData.code); |
| 92 | + const patientWhereClause = patientConditions.length > 0 ? `WHERE ${patientConditions.join(' AND ')}` : ''; |
| 93 | + const immunizationWhereClause = |
| 94 | + immunizationConditions.length > 0 ? `WHERE ${immunizationConditions.join(' AND ')}` : ''; |
| 95 | + |
| 96 | + const sqlQuery = `with filtered_patients as (select id from patient ${patientWhereClause}) select (select count(*) from filtered_patients) as total_patients, (select count(distinct i.resource#>>'{patient,id}') from immunization i join filtered_patients fp on i.resource#>>'{patient,id}' = fp.id ${immunizationWhereClause}) as vaccinated_patients`; |
| 97 | + const sqlResponse = mapSuccess( |
| 98 | + await service<Array<{ total_patients: number; vaccinated_patients: number }>>({ |
| 99 | + url: '/$sql', |
| 100 | + method: 'POST', |
| 101 | + data: [sqlQuery, ...params], |
| 102 | + }), |
| 103 | + (data) => data[0], |
| 104 | + ); |
| 105 | + return sqlResponse; |
| 106 | + }, [activeData.code, filters]); |
| 107 | + return { response }; |
| 108 | +} |
0 commit comments