forked from chicagopcdc/FHIR_resource_tabular_viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataUtils.js
More file actions
135 lines (118 loc) · 4.32 KB
/
dataUtils.js
File metadata and controls
135 lines (118 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// dataUtils.js - Helper functions for loading and processing FHIR data
import * as XLSX from 'xlsx';
export const loadFHIRData = async () => {
try {
const response = await window.fs.readFile('multi_patient_combined_fhir.xlsx');
const workbook = XLSX.read(response);
const data = {
patients: XLSX.utils.sheet_to_json(workbook.Sheets['patient']),
observations: XLSX.utils.sheet_to_json(workbook.Sheets['observation']),
documents: XLSX.utils.sheet_to_json(workbook.Sheets['documentreference']),
conditions: XLSX.utils.sheet_to_json(workbook.Sheets['condition']),
procedures: XLSX.utils.sheet_to_json(workbook.Sheets['procedure']),
medications: XLSX.utils.sheet_to_json(workbook.Sheets['medicationrequest']),
encounters: XLSX.utils.sheet_to_json(workbook.Sheets['encounter']),
allergies: XLSX.utils.sheet_to_json(workbook.Sheets['allergyintolerance'])
};
return data;
} catch (error) {
console.error('Error loading FHIR data:', error);
return null;
}
};
export const calculateAge = (birthDate) => {
if (!birthDate) return 0;
const today = new Date();
const birth = new Date(birthDate);
let age = today.getFullYear() - birth.getFullYear();
const monthDiff = today.getMonth() - birth.getMonth();
if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birth.getDate())) {
age--;
}
return age;
};
export const formatDate = (dateString) => {
if (!dateString) return 'N/A';
try {
return new Date(dateString).toLocaleDateString();
} catch (error) {
return 'Invalid Date';
}
};
export const formatDateTime = (dateString) => {
if (!dateString) return 'N/A';
try {
return new Date(dateString).toLocaleString();
} catch (error) {
return 'Invalid Date';
}
};
export const getPatientDataById = (patientId, allData) => {
if (!allData || !allData.patients) return null;
const patientIndex = parseInt(patientId.split('-')[1]) - 1;
const patient = allData.patients[patientIndex];
if (!patient) return null;
return {
patient,
observations: allData.observations.filter(obs =>
obs.source_patient_id === patient.source_patient_id
),
documents: allData.documents.filter(doc =>
doc.source_patient_id === patient.source_patient_id
),
conditions: allData.conditions.filter(condition =>
condition.source_patient_id === patient.source_patient_id
),
procedures: allData.procedures.filter(procedure =>
procedure.source_patient_id === patient.source_patient_id
),
medications: allData.medications.filter(medication =>
medication.source_patient_id === patient.source_patient_id
),
encounters: allData.encounters.filter(encounter =>
encounter.source_patient_id === patient.source_patient_id
),
allergies: allData.allergies.filter(allergy =>
allergy.source_patient_id === patient.source_patient_id
)
};
};
export const transformPatientsForTable = (patients) => {
return patients.map((patient, index) => ({
id: `PT-${String(index + 1).padStart(3, '0')}`,
originalId: patient.id,
name: `${patient.given_name} ${patient.family_name}`,
birthDate: patient.birth_date,
age: calculateAge(patient.birth_date),
gender: patient.gender.charAt(0).toUpperCase() + patient.gender.slice(1),
phone: `+1-555-${String(1230 + index).padStart(4, '0')}`,
email: '[email protected]',
address: `${patient.city}, ${patient.state} ${patient.postal_code}`,
status: Math.random() > 0.2 ? 'ACTIVE' : 'INACTIVE'
}));
};
export const filterPatients = (patients, searchTerm) => {
if (!searchTerm) return patients;
const term = searchTerm.toLowerCase();
return patients.filter(patient =>
patient.name.toLowerCase().includes(term) ||
patient.id.toLowerCase().includes(term) ||
patient.email.toLowerCase().includes(term) ||
patient.address.toLowerCase().includes(term)
);
};
export const applyQuickFilter = (patients, filterType) => {
switch (filterType) {
case 'active':
return patients.filter(p => p.status === 'ACTIVE');
case 'inactive':
return patients.filter(p => p.status === 'INACTIVE');
case 'male':
return patients.filter(p => p.gender === 'Male');
case 'female':
return patients.filter(p => p.gender === 'Female');
case 'all':
default:
return patients;
}
};