From 8fb7d12783f51679d57e5dd13367c3eccc30d4a3 Mon Sep 17 00:00:00 2001 From: Josh Date: Sat, 28 Dec 2024 21:49:34 +0000 Subject: [PATCH] fixed routes for apis --- .env.development | 1 + .env.production | 3 +- backend/server2.js | 81 +++++++++++++++++++++++-------------------- src/utils/apiUtils.js | 5 +-- 4 files changed, 49 insertions(+), 41 deletions(-) diff --git a/.env.development b/.env.development index a410cd1..47226a6 100755 --- a/.env.development +++ b/.env.development @@ -6,3 +6,4 @@ GOOGLE_MAPS_API_KEY=AIzaSyCTMgjiHUF2Vl3QriQu2kDEuZWz39ZAR20 COLLEGE_SCORECARD_KEY = BlZ0tIdmXVGI4G8NxJ9e6dXEiGUfAfnQJyw8bumj REACT_APP_API_URL=http://localhost:5001 +REACT_APP_ENV=development \ No newline at end of file diff --git a/.env.production b/.env.production index a0ba77d..1bb7c40 100644 --- a/.env.production +++ b/.env.production @@ -5,4 +5,5 @@ REACT_APP_BLS_API_KEY=80d7a65a809a43f3a306a41ec874d231 GOOGLE_MAPS_API_KEY=AIzaSyCTMgjiHUF2Vl3QriQu2kDEuZWz39ZAR20 COLLEGE_SCORECARD_KEY = BlZ0tIdmXVGI4G8NxJ9e6dXEiGUfAfnQJyw8bumj -REACT_APP_API_URL=/api +REACT_APP_API_URL=https://dev.aptivaai.com/api +REACT_APP_ENV=production diff --git a/backend/server2.js b/backend/server2.js index f33abfa..d4ec594 100755 --- a/backend/server2.js +++ b/backend/server2.js @@ -9,18 +9,20 @@ import { fileURLToPath } from 'url'; // Import fileURLToPath to handle the curre import { open } from 'sqlite'; // Use the open method directly from sqlite package import sqlite3 from 'sqlite3'; import fs from 'fs'; +import readline from 'readline'; +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); +dotenv.config({ + path: path.resolve(__dirname, `.env.${process.env.NODE_ENV || 'development'}`), +}); -dotenv.config({ path: path.resolve('/home/jcoakley/aptiva-dev1-app/.env') }); // Adjust the path based on your folder structure console.log('ONET_USERNAME:', process.env.ONET_USERNAME); console.log('ONET_PASSWORD:', process.env.ONET_PASSWORD); -// Get the current directory path (workaround for __dirname in ES modules) -const __filename = fileURLToPath(import.meta.url); -const __dirname = path.dirname(__filename); const allowedOrigins = ['http://localhost:3000', 'http://34.16.120.118:3000', 'https://dev.aptivaai.com']; const mappingFilePath = '/home/jcoakley/aptiva-dev1-app/public/CIP_to_ONET_SOC.xlsx' -const institutionJsonFilePath = path.join(__dirname, '../public/Institution_data.json'); +const institutionFilePath = path.join(__dirname, '../public/Institution_data.json'); const app = express(); const PORT = process.env.PORT || 5001; @@ -95,13 +97,6 @@ app.use((req, res, next) => { next(); }); -app.use('/CIP_institution_mapping_fixed.json', (req, res) => { - res.setHeader('Access-Control-Allow-Origin', '*'); - res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS'); - res.sendFile(path.join(__dirname, 'CIP_institution_mapping_fixed.json')); -}); - - // Middleware to parse JSON bodies app.use(express.json()); app.use(express.static(path.join(__dirname, 'public'))); @@ -355,30 +350,42 @@ app.get('/api/schools', (req, res) => { const { cipCode, state } = req.query; console.log('Query Params:', { cipCode, state }); - try { - // Load Institution Data from JSON file - const schoolsData = JSON.parse(fs.readFileSync(institutionJsonFilePath, 'utf8')); - - console.log('Loaded Schools Data:', schoolsData.length); - - // Filter schools based on CIP Code and State - const filteredData = schoolsData.filter((school) => { - const cipCodeValue = school['CIP Code']?.toString().replace('.', '').slice(0, 4) || ''; - const formattedCipCode = cipCode?.toString().replace('.', '').slice(0, 4) || ''; - console.log('Comparing CIP Code:', cipCodeValue, 'with', formattedCipCode); - console.log('Comparing State:', school['State'], 'with', state); - return ( - (!formattedCipCode || cipCodeValue.startsWith(formattedCipCode)) && - (!state || school['State'] === state) - ); - }); - - console.log('Filtered Schools Data:', filteredData.length); - res.json(filteredData); - } catch (error) { - console.error('Error loading or filtering schools data:', error.message); - res.status(500).json({ error: 'Failed to load or filter schools data.' }); + if (!cipCode || !state) { + return res.status(400).json({ error: 'CIP Code and State are required.' }); } + + const results = []; + const matchedCIP = cipCode.replace('.', '').slice(0, 4); // Normalize CIP Code + const input = fs.createReadStream(institutionFilePath); // Use streaming instead of loading the entire file + + const rl = readline.createInterface({ + input, + crlfDelay: Infinity, // Handle line breaks + }); + + rl.on('line', (line) => { + try { + const school = JSON.parse(line); // Parse each line as JSON + const schoolCIP = school['CIP Code']?.toString().replace('.', '').slice(0, 4) || ''; + const schoolState = school['State']?.toUpperCase().trim(); + + if (schoolCIP.startsWith(matchedCIP) && schoolState === state.toUpperCase().trim()) { + results.push(school); // Only add matching results + } + } catch (error) { + console.error('Error parsing line:', error.message); + } + }); + + rl.on('close', () => { + console.log('Filtered Schools Count:', results.length); + res.json(results); // Send only filtered results + }); + + rl.on('error', (error) => { + console.error('Error reading Institution data:', error.message); + res.status(500).json({ error: 'Failed to load schools data.' }); + }); }); @@ -388,9 +395,7 @@ app.get('/api/tuition/:cipCode', (req, res) => { console.log(`Received CIP Code: ${cipCode}, State: ${state}`); try { - const workbook = xlsx.readFile(institutionFilePath); // Load Excel file - const sheet = workbook.Sheets[workbook.SheetNames[0]]; // First sheet - const schoolsData = xlsx.utils.sheet_to_json(sheet); // Convert to JSON + const schoolsData = JSON.parse(fs.readFileSync(institutionFilePath, 'utf8')); console.log('Loaded Tuition Data:', schoolsData.length); diff --git a/src/utils/apiUtils.js b/src/utils/apiUtils.js index db343a8..7bdf343 100644 --- a/src/utils/apiUtils.js +++ b/src/utils/apiUtils.js @@ -4,7 +4,7 @@ import axios from 'axios'; export const fetchAreasByState = async (state) => { try { const apiUrl = process.env.REACT_APP_API_URL || ''; - const response = await fetch('http://127.0.0.1:5001/api/CIP_institution_mapping_fixed.json'); + const response = await fetch(`${process.env.REACT_APP_API_URL}/Institution_data.json`); if (response.status === 200) { return response.data.areas; // Assume the API returns a list of areas @@ -22,7 +22,8 @@ export const fetchAreasByState = async (state) => { // Fetch schools export const fetchSchools = async (cipCode, state = '', level = '', type = '') => { try { - const response = await axios.get('http://127.0.0.1:5001/api/schools', { + const apiUrl = process.env.REACT_APP_API_URL || ''; + const response = await axios.get(`${apiUrl}/api/schools`, { params: { cipCode, state,