import api from '../auth/apiClient.js'; import { promises as fs } from 'fs'; const careersFile = '../../updated_career_data_final.json'; const outputFile = '../../careers_with_ratings.json'; const onetUsername = 'aptivaai'; const onetPassword = '2296ahq'; // Sleep function for delay between calls const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms)); // Helper function to convert O*Net numeric scores (0-100) to a 1-5 scale const mapScoreToScale = (score) => { if(score >= 80) return 5; if(score >= 60) return 4; if(score >= 40) return 3; if(score >= 20) return 2; return 1; }; // Fully corrected function to fetch ratings from O*Net API const fetchCareerRatingsCorrected = async (socCode) => { try { const response = await api.get( `https://services.onetcenter.org/ws/online/occupations/${socCode}/details`, { auth: { username: onetUsername, password: onetPassword } } ); const data = response.data; // Correctly parse the work_values array from O*Net's structure const workValues = Array.isArray(data.work_values?.element) ? data.work_values.element : []; // Correctly parse the bright_outlook boolean from O*Net's structure const brightOutlook = data.occupation.tags.bright_outlook === true; // Extract numerical scores for balance and recognition const balanceValue = workValues.find(v => v.name === 'Working Conditions')?.score.value || 0; const recognitionValue = workValues.find(v => v.name === 'Recognition')?.score.value || 0; // Return mapped ratings accurately reflecting O*Net data return { growth: brightOutlook ? 5 : 1, balance: mapScoreToScale(balanceValue), recognition: mapScoreToScale(recognitionValue) }; } catch (error) { console.error(`Error fetching details for ${socCode}:`, error.message); return { growth: 1, balance: 1, recognition: 1 }; } }; // Main function to populate ratings for all careers const populateRatingsCorrected = async () => { const careersData = JSON.parse(await fs.readFile(careersFile, 'utf8')); for (let career of careersData) { const ratings = await fetchCareerRatingsCorrected(career.soc_code); if (ratings) { career.ratings = ratings; console.log(`Fetched ratings for: ${career.title}`); } // Wait for .5 seconds between API calls to avoid hitting API limits await sleep(500); } await fs.writeFile(outputFile, JSON.stringify(careersData, null, 2)); console.log(`Ratings populated to: ${outputFile}`); }; // Call the corrected function to populate ratings populateRatingsCorrected();