fixed routes for apis

This commit is contained in:
Josh 2024-12-28 21:49:34 +00:00
parent 67e3e962e7
commit a04b097159
3 changed files with 47 additions and 40 deletions

View File

@ -6,3 +6,4 @@ GOOGLE_MAPS_API_KEY=AIzaSyCTMgjiHUF2Vl3QriQu2kDEuZWz39ZAR20
COLLEGE_SCORECARD_KEY = BlZ0tIdmXVGI4G8NxJ9e6dXEiGUfAfnQJyw8bumj
REACT_APP_API_URL=http://localhost:5001
REACT_APP_ENV=development

View File

@ -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);

View File

@ -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,