-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
182 lines (148 loc) · 5.96 KB
/
server.js
File metadata and controls
182 lines (148 loc) · 5.96 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
const express = require('express');
const cors = require('cors');
const axios = require('axios');
const { searchHospitals, searchHospitalsByRadius } = require('./csvUtils');
require('dotenv').config();
console.log('Marketplace CMS API Key:', process.env.MARKETPLACE_CMS_API_KEY);
console.log('Google API Key:', process.env.REACT_APP_GOOGLE_API_KEY);
console.log('Back4App App ID:', process.env.BACK4APP_APP_ID_KEY);
console.log('Back4App API Key:', process.env.BACK4APP_API_KEY ? 'Set' : 'Not Set');
const app = express();
const port = process.env.PORT || 3001;
app.use(cors());
app.use(express.json());
app.get('/api/hospitals', async (req, res) => {
try {
const searchQuery = req.query.query;
console.log('Received search query:', searchQuery);
if (!searchQuery || searchQuery.trim() === '') {
return res.json([]);
}
const results = await searchHospitals(searchQuery);
console.log(`Search complete. Found ${results.length} results.`);
res.json(results);
} catch (error) {
console.error('Error searching hospitals:', error);
res.status(500).json({
error: 'An error occurred while searching hospitals',
details: error.message
});
}
});
app.get('/api/hospitals/radius', async (req, res) => {
try {
const { address, radius } = req.query;
console.log('Received radius search:', address, radius);
const geocodeResponse = await axios.get(`https://maps.googleapis.com/maps/api/geocode/json?address=${encodeURIComponent(address)}&key=${process.env.REACT_APP_GOOGLE_API_KEY}`);
console.log('Geocoding response status:', geocodeResponse.data.status);
if (geocodeResponse.data.status !== 'OK' || !geocodeResponse.data.results || geocodeResponse.data.results.length === 0) {
console.error('Geocoding failed:', geocodeResponse.data);
return res.status(400).json({ error: 'Failed to geocode the provided address' });
}
const { lat, lng } = geocodeResponse.data.results[0].geometry.location;
console.log(`Geocoded coordinates: ${lat}, ${lng}`);
const results = await searchHospitalsByRadius(lat, lng, parseFloat(radius));
console.log(`Radius search complete. Found ${results.length} results.`);
res.json(results);
} catch (error) {
console.error('Error searching hospitals by radius:', error);
res.status(500).json({
error: 'An error occurred while searching hospitals',
details: error.message
});
}
});
app.get('/api/counties/:state', async (req, res) => {
try {
const { state } = req.params;
const response = await axios.get(
'https://parseapi.back4app.com/classes/Uscounties_Area',
{
params: {
where: JSON.stringify({ stateAbbreviation: state }),
limit: 1000, // Adjust this limit as needed
},
headers: {
'X-Parse-Application-Id': process.env.BACK4APP_APP_ID_KEY,
'X-Parse-REST-API-Key': process.env.BACK4APP_API_KEY,
}
}
);
console.log('Back4App API Response:', JSON.stringify(response.data, null, 2));
if (!response.data || !response.data.results) {
throw new Error('Unexpected response format from Back4App API');
}
const counties = response.data.results.map(item => item.countyName);
console.log('Processed counties:', counties);
res.json(counties);
} catch (error) {
console.error('Error fetching counties:', error);
res.status(500).json({ error: 'Failed to fetch counties', details: error.message });
}
});
app.post('/api/insurance-plans', async (req, res) => {
try {
const { income, zipCode, county, state, people, market, year } = req.body;
console.log('Received request:', { income, zipCode, county, state, market, year });
// Fetch county FIPS code from Back4App
const back4appResponse = await axios.get(
'https://parseapi.back4app.com/classes/Uscounties_Area',
{
params: {
where: JSON.stringify({ stateAbbreviation: state, countyName: county }),
limit: 1,
},
headers: {
'X-Parse-Application-Id': process.env.BACK4APP_APP_ID_KEY,
'X-Parse-REST-API-Key': process.env.BACK4APP_API_KEY,
}
}
);
console.log('Back4App API Response:', JSON.stringify(back4appResponse.data, null, 2));
if (!back4appResponse.data.results || back4appResponse.data.results.length === 0) {
throw new Error('County not found in Back4App data');
}
const countyData = back4appResponse.data.results[0];
const fullFips = countyData.FIPSCode + countyData.countyCode;
console.log('Full FIPS code:', fullFips);
const apiUrl = 'https://marketplace.api.healthcare.gov/api/v1/plans/search';
const apiKey = process.env.MARKETPLACE_CMS_API_KEY;
console.log('Using API Key:', apiKey ? 'API key is set' : 'API key is not set');
const requestBody = {
household: {
income: parseInt(income),
people: people.map(person => ({
age: parseInt(person.age),
aptc_eligible: person.eligibleForCoverage,
gender: person.gender,
uses_tobacco: person.tobaccoUser
}))
},
market: market,
place: {
countyfips: fullFips,
state: state,
zipcode: zipCode
},
year: parseInt(year)
};
console.log('Marketplace API Request Body:', JSON.stringify(requestBody, null, 2));
const response = await axios.post(`${apiUrl}?apikey=${apiKey}`, requestBody, {
headers: {
'Content-Type': 'application/json'
}
});
console.log('Marketplace API Response Status:', response.status);
res.json(response.data);
} catch (error) {
console.error('Error fetching insurance plans:', error.message);
res.status(500).json({
error: 'An error occurred while fetching insurance plans',
details: error.message,
apiKey: process.env.MARKETPLACE_CMS_API_KEY ? 'API key is set' : 'API key is not set'
});
}
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});