-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample-integration.js
More file actions
255 lines (212 loc) · 7.07 KB
/
Copy pathexample-integration.js
File metadata and controls
255 lines (212 loc) · 7.07 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**
* Example Integration with Bumpie_Meds
*
* This shows how to integrate DrugInteractionChecker
* with the existing PregnancySafetyEngine
*/
const DrugInteractionChecker = require('./drug-interaction-checker');
// Initialize the checker (do this once at app startup)
const interactionChecker = new DrugInteractionChecker({
drugBankApiKey: process.env.DRUGBANK_API_KEY,
enablePregnancyWarnings: true
});
// ============================================
// Example 1: Simple Two-Drug Check
// ============================================
async function example1() {
console.log('\n=== Example 1: Simple Check ===');
const result = await interactionChecker.checkInteractions(
['Warfarin', 'Aspirin']
);
console.log(`Found ${result.interactions.length} interactions`);
console.log('Source:', result.source);
if (result.interactions.length > 0) {
const int = result.interactions[0];
console.log(`\n${int.drug1} + ${int.drug2}`);
console.log(`Severity: ${int.severity}`);
console.log(`Description: ${int.description}`);
}
}
// ============================================
// Example 2: With Pregnancy Context
// ============================================
async function example2() {
console.log('\n=== Example 2: Pregnancy Context ===');
// Patient in week 10 of pregnancy
const result = await interactionChecker.checkInteractions(
['Ibuprofen', 'Lisinopril'],
{ weekOfPregnancy: 10 }
);
result.interactions.forEach(int => {
console.log(`\n${int.drug1} + ${int.drug2}`);
console.log(`Severity: ${int.severity}`);
console.log(`Pregnancy Warning: ${int.pregnancyWarning || 'None'}`);
});
}
// ============================================
// Example 3: Multiple Medications
// ============================================
async function example3() {
console.log('\n=== Example 3: Multiple Medications ===');
const medications = [
'Metformin',
'Lisinopril',
'Aspirin',
'Ibuprofen'
];
const result = await interactionChecker.checkInteractions(
medications,
{ weekOfPregnancy: 20 }
);
console.log(`Checking ${medications.length} medications`);
console.log(`Found ${result.interactions.length} interactions:`);
result.interactions.forEach((int, i) => {
console.log(`\n${i + 1}. ${int.drug1} + ${int.drug2}`);
console.log(` Severity: ${int.severity}`);
console.log(` ${int.description.substring(0, 80)}...`);
});
}
// ============================================
// Example 4: Integration with PregnancySafetyEngine
// ============================================
class EnhancedPregnancySafetyEngine {
constructor() {
this.interactionChecker = new DrugInteractionChecker({
enablePregnancyWarnings: true
});
}
/**
* Enhanced safety check with interactions
*/
async checkMedicationSafety(medicationName, weekOfPregnancy, currentMedications = []) {
// Your existing FDA category check
const fdaResult = {
safe: true,
fdaCategory: 'B',
riskLevel: 'low',
warnings: []
};
// Add interaction check
const allMeds = [medicationName, ...currentMedications];
let interactionResult = { interactions: [], source: 'none' };
if (allMeds.length >= 2) {
interactionResult = await this.interactionChecker.checkInteractions(
allMeds,
{ weekOfPregnancy }
);
}
// Combine results
return {
...fdaResult,
interactions: interactionResult.interactions,
interactionCount: interactionResult.interactions.length,
hasInteractions: interactionResult.interactions.length > 0,
interactionSource: interactionResult.source,
// Mark as unsafe if critical interactions found
safe: fdaResult.safe && !interactionResult.interactions.some(i =>
i.severity === 'CRITICAL' || i.severity === 'SEVERE'
)
};
}
}
async function example4() {
console.log('\n=== Example 4: Enhanced Safety Check ===');
const engine = new EnhancedPregnancySafetyEngine();
const result = await engine.checkMedicationSafety(
'Ibuprofen',
8, // Week 8 of pregnancy
['Lisinopril', 'Aspirin'] // Current medications
);
console.log('Safety Check Result:');
console.log('Safe:', result.safe);
console.log('FDA Category:', result.fdaCategory);
console.log('Risk Level:', result.riskLevel);
console.log('Interactions Found:', result.interactionCount);
if (result.hasInteractions) {
console.log('\nInteractions:');
result.interactions.forEach(int => {
console.log(`- ${int.drug1} + ${int.drug2}: ${int.severity}`);
if (int.pregnancyWarning) {
console.log(` ${int.pregnancyWarning}`);
}
});
}
}
// ============================================
// Example 5: Error Handling
// ============================================
async function example5() {
console.log('\n=== Example 5: Error Handling ===');
try {
// This will try DrugBank first, then fall back to local DB
const result = await interactionChecker.checkInteractions(
['Unknown Drug A', 'Unknown Drug B']
);
console.log('Source:', result.source);
console.log('Interactions:', result.interactions.length);
// Even with unknown drugs, system doesn't crash
// It just returns empty array
} catch (error) {
console.error('Error:', error.message);
}
}
// ============================================
// Example 6: Caching for Production
// ============================================
class CachedInteractionChecker {
constructor() {
this.checker = new DrugInteractionChecker({
enablePregnancyWarnings: true
});
this.cache = new Map();
this.cacheTTL = 24 * 60 * 60 * 1000; // 24 hours
}
async checkInteractions(medications, options = {}) {
// Create cache key
const key = medications.sort().join(',') + JSON.stringify(options);
// Check cache
const cached = this.cache.get(key);
if (cached && Date.now() - cached.timestamp < this.cacheTTL) {
console.log('✅ Cache hit!');
return cached.result;
}
// Fetch fresh data
console.log('⏳ Cache miss, fetching...');
const result = await this.checker.checkInteractions(medications, options);
// Store in cache
this.cache.set(key, {
result,
timestamp: Date.now()
});
return result;
}
}
async function example6() {
console.log('\n=== Example 6: Caching ===');
const cachedChecker = new CachedInteractionChecker();
// First call - cache miss
await cachedChecker.checkInteractions(['Warfarin', 'Aspirin']);
// Second call - cache hit
await cachedChecker.checkInteractions(['Warfarin', 'Aspirin']);
}
// ============================================
// Run All Examples
// ============================================
async function runAll() {
console.log('🧪 Drug Interaction Checker Examples\n');
await example1();
await example2();
await example3();
await example4();
await example5();
await example6();
console.log('\n✅ All examples completed!');
}
// Run if called directly
if (require.main === module) {
runAll().catch(console.error);
}
module.exports = {
EnhancedPregnancySafetyEngine,
CachedInteractionChecker
};