Migrated all sqlite tables to mySQL for server
This commit is contained in:
parent
72e52d57fe
commit
3fdf280817
@ -6,6 +6,12 @@ GOOGLE_MAPS_API_KEY=AIzaSyCTMgjiHUF2Vl3QriQu2kDEuZWz39ZAR20
|
|||||||
REACT_APP_GOOGLE_MAPS_API_KEY=AIzaSyCTMgjiHUF2Vl3QriQu2kDEuZWz39ZAR20
|
REACT_APP_GOOGLE_MAPS_API_KEY=AIzaSyCTMgjiHUF2Vl3QriQu2kDEuZWz39ZAR20
|
||||||
COLLEGE_SCORECARD_KEY = BlZ0tIdmXVGI4G8NxJ9e6dXEiGUfAfnQJyw8bumj
|
COLLEGE_SCORECARD_KEY = BlZ0tIdmXVGI4G8NxJ9e6dXEiGUfAfnQJyw8bumj
|
||||||
|
|
||||||
|
DB_HOST=34.67.180.54
|
||||||
|
DB_PORT=3306
|
||||||
|
DB_USER=sqluser
|
||||||
|
DB_NAME=user_profile_db
|
||||||
|
DB_PASSWORD=ps<g+2DO-eTb2mb5
|
||||||
|
|
||||||
REACT_APP_API_URL=https://dev1.aptivaai.com/api
|
REACT_APP_API_URL=https://dev1.aptivaai.com/api
|
||||||
REACT_APP_ENV=production
|
REACT_APP_ENV=production
|
||||||
REACT_APP_OPENAI_API_KEY=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
|
REACT_APP_OPENAI_API_KEY=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
|
||||||
|
@ -4,30 +4,59 @@ import cors from 'cors';
|
|||||||
import helmet from 'helmet';
|
import helmet from 'helmet';
|
||||||
import dotenv from 'dotenv';
|
import dotenv from 'dotenv';
|
||||||
import { fileURLToPath } from 'url';
|
import { fileURLToPath } from 'url';
|
||||||
|
import fs from 'fs';
|
||||||
|
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import sqlite3 from 'sqlite3'; // Import SQLite
|
|
||||||
import bodyParser from 'body-parser';
|
import bodyParser from 'body-parser';
|
||||||
import bcrypt from 'bcrypt';
|
import bcrypt from 'bcrypt';
|
||||||
import jwt from 'jsonwebtoken'; // For token-based authentication
|
import jwt from 'jsonwebtoken'; // For token-based authentication
|
||||||
|
|
||||||
// Derive __dirname for ES modules
|
|
||||||
|
import mysql from 'mysql2';
|
||||||
|
|
||||||
|
import sqlite3 from 'sqlite3';
|
||||||
|
|
||||||
|
|
||||||
const __filename = fileURLToPath(import.meta.url);
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
const __dirname = path.dirname(__filename);
|
const __dirname = path.dirname(__filename);
|
||||||
const dbPath = path.resolve('/home/jcoakley/aptiva-dev1-app/user_profile.db');
|
|
||||||
|
|
||||||
const db = new sqlite3.Database(dbPath, (err) => {
|
const rootPath = path.resolve(__dirname, '..'); // Up one level
|
||||||
if (err) {
|
const env = process.env.NODE_ENV?.trim() || 'development';
|
||||||
console.error('Error connecting to database:', err.message);
|
const envPath = path.resolve(rootPath, `.env.${env}`);
|
||||||
} else {
|
dotenv.config({ path: envPath }); // Load .env file
|
||||||
console.log('Connected to user_profile.db');
|
|
||||||
}
|
// Grab secrets and config from ENV
|
||||||
|
const SECRET_KEY = process.env.SECRET_KEY || 'supersecurekey';
|
||||||
|
const DB_HOST = process.env.DB_HOST || '127.0.0.1';
|
||||||
|
const DB_PORT = process.env.DB_PORT ? parseInt(process.env.DB_PORT) : 3306;
|
||||||
|
const DB_USER = process.env.DB_USER || 'sqluser';
|
||||||
|
const DB_PASSWORD = process.env.DB_PASSWORD || '';
|
||||||
|
const DB_NAME = process.env.DB_NAME || 'user_profile_db';
|
||||||
|
|
||||||
|
console.log('SECRET_KEY:', SECRET_KEY);
|
||||||
|
console.log('DB_HOST:', DB_HOST);
|
||||||
|
console.log('DB_USER:', DB_USER);
|
||||||
|
console.log('DB_NAME:', DB_NAME);
|
||||||
|
console.log('Current Working Directory:', process.cwd());
|
||||||
|
|
||||||
|
// Create a MySQL pool for user_profile data
|
||||||
|
const pool = mysql.createPool({
|
||||||
|
host: DB_HOST,
|
||||||
|
port: DB_PORT,
|
||||||
|
user: DB_USER,
|
||||||
|
password: DB_PASSWORD,
|
||||||
|
database: DB_NAME,
|
||||||
|
connectionLimit: 10, // optional
|
||||||
});
|
});
|
||||||
|
|
||||||
dotenv.config({ path: path.resolve('/home/jcoakley/aptiva-dev1-app/.env') }); // Adjust the path based on your folder structure
|
// Test a quick query (optional)
|
||||||
const SECRET_KEY = process.env.SECRET_KEY || 'supersecurekey'; // Use a secure key in production
|
pool.query('SELECT 1', (err) => {
|
||||||
console.log('ONET_USERNAME:', process.env.ONET_USERNAME);
|
if (err) {
|
||||||
console.log('ONET_PASSWORD:', process.env.ONET_PASSWORD);
|
console.error('Error connecting to MySQL user_profile_db:', err.message);
|
||||||
console.log('Current Working Directory:', process.cwd());
|
} else {
|
||||||
|
console.log('Connected to MySQL user_profile_db');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const PORT = 5000;
|
const PORT = 5000;
|
||||||
@ -36,7 +65,8 @@ const allowedOrigins = ['http://localhost:3000', 'http://34.16.120.118:3000', 'h
|
|||||||
app.disable('x-powered-by');
|
app.disable('x-powered-by');
|
||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
app.use(helmet({
|
app.use(
|
||||||
|
helmet({
|
||||||
contentSecurityPolicy: false,
|
contentSecurityPolicy: false,
|
||||||
crossOriginEmbedderPolicy: false,
|
crossOriginEmbedderPolicy: false,
|
||||||
})
|
})
|
||||||
@ -83,8 +113,7 @@ app.use((req, res, next) => {
|
|||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// =============== USER REGISTRATION (MySQL) ===============
|
||||||
// Route for user registration
|
|
||||||
app.post('/api/register', async (req, res) => {
|
app.post('/api/register', async (req, res) => {
|
||||||
const {
|
const {
|
||||||
userId,
|
userId,
|
||||||
@ -99,60 +128,69 @@ app.post('/api/register', async (req, res) => {
|
|||||||
career_situation
|
career_situation
|
||||||
} = req.body;
|
} = req.body;
|
||||||
|
|
||||||
// Validate all required fields, including career_situation
|
if (
|
||||||
if (!userId || !username || !password || !firstname || !lastname || !email || !zipcode || !state || !area || !career_situation) {
|
!userId ||
|
||||||
|
!username ||
|
||||||
|
!password ||
|
||||||
|
!firstname ||
|
||||||
|
!lastname ||
|
||||||
|
!email ||
|
||||||
|
!zipcode ||
|
||||||
|
!state ||
|
||||||
|
!area ||
|
||||||
|
!career_situation
|
||||||
|
) {
|
||||||
return res.status(400).json({ error: 'All fields including career_situation are required' });
|
return res.status(400).json({ error: 'All fields including career_situation are required' });
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const hashedPassword = await bcrypt.hash(password, 10);
|
const hashedPassword = await bcrypt.hash(password, 10);
|
||||||
|
|
||||||
// Insert into user_auth
|
// 1) Insert into user_profile first
|
||||||
const authQuery = `
|
const profileQuery = `
|
||||||
INSERT INTO user_auth (user_id, username, hashed_password)
|
INSERT INTO user_profile (id, firstname, lastname, email, zipcode, state, area, career_situation)
|
||||||
VALUES (?, ?, ?)
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
`;
|
`;
|
||||||
|
pool.query(
|
||||||
|
profileQuery,
|
||||||
|
[userId, firstname, lastname, email, zipcode, state, area, career_situation],
|
||||||
|
(err2, results2) => {
|
||||||
|
if (err2) {
|
||||||
|
console.error('Error inserting into user_profile:', err2.message);
|
||||||
|
return res.status(500).json({ error: 'Failed to create user profile' });
|
||||||
|
}
|
||||||
|
|
||||||
db.run(authQuery, [userId, username, hashedPassword], function (err) {
|
// 2) Insert into user_auth
|
||||||
if (err) {
|
const authQuery = `
|
||||||
console.error('Error inserting into user_auth:', err.message);
|
INSERT INTO user_auth (user_id, username, hashed_password)
|
||||||
if (err.message.includes('UNIQUE constraint failed')) {
|
VALUES (?, ?, ?)
|
||||||
return res.status(400).json({ error: 'Username already exists' });
|
`;
|
||||||
}
|
pool.query(authQuery, [userId, username, hashedPassword], (err, results) => {
|
||||||
return res.status(500).json({ error: 'Failed to register user' });
|
if (err) {
|
||||||
}
|
console.error('Error inserting into user_auth:', err.message);
|
||||||
|
if (err.code === 'ER_DUP_ENTRY') {
|
||||||
|
return res.status(400).json({ error: 'Username already exists' });
|
||||||
|
}
|
||||||
|
return res.status(500).json({ error: 'Failed to register user' });
|
||||||
|
}
|
||||||
|
return res.status(201).json({ message: 'User registered successfully', userId });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
// Insert into user_profile including career_situation
|
|
||||||
const profileQuery = `
|
|
||||||
INSERT INTO user_profile (user_id, firstname, lastname, email, zipcode, state, area, career_situation)
|
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
||||||
`;
|
|
||||||
|
|
||||||
db.run(profileQuery, [userId, firstname, lastname, email, zipcode, state, area, career_situation], (err) => {
|
|
||||||
if (err) {
|
|
||||||
console.error('Error inserting into user_profile:', err.message);
|
|
||||||
return res.status(500).json({ error: 'Failed to create user profile' });
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.status(201).json({ message: 'User registered successfully', userId });
|
|
||||||
});
|
|
||||||
});
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error during registration:', error.message);
|
console.error('Error during registration:', error.message);
|
||||||
return res.status(500).json({ error: 'Internal server error' });
|
return res.status(500).json({ error: 'Internal server error' });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// =============== SIGN-IN (MySQL) ===============
|
||||||
// Route to handle user sign-in (updated with career_priorities and career_list)
|
|
||||||
app.post('/api/signin', async (req, res) => {
|
app.post('/api/signin', async (req, res) => {
|
||||||
const { username, password } = req.body;
|
const { username, password } = req.body;
|
||||||
|
|
||||||
if (!username || !password) {
|
if (!username || !password) {
|
||||||
return res.status(400).json({ error: 'Both username and password are required' });
|
return res.status(400).json({ error: 'Both username and password are required' });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Updated query includes career_priorities and career_list
|
|
||||||
const query = `
|
const query = `
|
||||||
SELECT
|
SELECT
|
||||||
user_auth.user_id,
|
user_auth.user_id,
|
||||||
@ -164,34 +202,29 @@ app.post('/api/signin', async (req, res) => {
|
|||||||
user_profile.email,
|
user_profile.email,
|
||||||
user_profile.firstname,
|
user_profile.firstname,
|
||||||
user_profile.lastname,
|
user_profile.lastname,
|
||||||
user_profile.career_priorities, -- new field
|
user_profile.career_priorities,
|
||||||
user_profile.career_list -- new field
|
user_profile.career_list
|
||||||
FROM user_auth
|
FROM user_auth
|
||||||
LEFT JOIN user_profile ON user_auth.user_id = user_profile.user_id
|
LEFT JOIN user_profile ON user_auth.user_id = user_profile.user_id
|
||||||
WHERE user_auth.username = ?
|
WHERE user_auth.username = ?
|
||||||
`;
|
`;
|
||||||
|
pool.query(query, [username], async (err, results) => {
|
||||||
db.get(query, [username], async (err, row) => {
|
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error('Error querying user_auth:', err.message);
|
console.error('Error querying user_auth:', err.message);
|
||||||
return res.status(500).json({ error: 'Failed to query user authentication data' });
|
return res.status(500).json({ error: 'Failed to query user authentication data' });
|
||||||
}
|
}
|
||||||
|
|
||||||
// If no matching username
|
if (!results || results.length === 0) {
|
||||||
if (!row) {
|
|
||||||
return res.status(401).json({ error: 'Invalid username or password' });
|
return res.status(401).json({ error: 'Invalid username or password' });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify the password using bcrypt
|
const row = results[0];
|
||||||
const isMatch = await bcrypt.compare(password, row.hashed_password);
|
const isMatch = await bcrypt.compare(password, row.hashed_password);
|
||||||
if (!isMatch) {
|
if (!isMatch) {
|
||||||
return res.status(401).json({ error: 'Invalid username or password' });
|
return res.status(401).json({ error: 'Invalid username or password' });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate JWT
|
|
||||||
const token = jwt.sign({ userId: row.user_id }, SECRET_KEY, { expiresIn: '2h' });
|
const token = jwt.sign({ userId: row.user_id }, SECRET_KEY, { expiresIn: '2h' });
|
||||||
|
|
||||||
// Return fully updated user object
|
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
message: 'Login successful',
|
message: 'Login successful',
|
||||||
token,
|
token,
|
||||||
@ -202,30 +235,26 @@ app.post('/api/signin', async (req, res) => {
|
|||||||
lastname: row.lastname,
|
lastname: row.lastname,
|
||||||
email: row.email,
|
email: row.email,
|
||||||
zipcode: row.zipcode,
|
zipcode: row.zipcode,
|
||||||
is_premium: row.is_premium,
|
is_premium: row.is_premium,
|
||||||
is_pro_premium: row.is_pro_premium,
|
is_pro_premium: row.is_pro_premium,
|
||||||
career_situation: row.career_situation,
|
career_situation: row.career_situation,
|
||||||
career_priorities: row.career_priorities, // newly added
|
career_priorities: row.career_priorities,
|
||||||
career_list: row.career_list, // newly added
|
career_list: row.career_list,
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// =============== CHECK USERNAME (MySQL) ===============
|
||||||
/// Check if username already exists
|
|
||||||
app.get('/api/check-username/:username', (req, res) => {
|
app.get('/api/check-username/:username', (req, res) => {
|
||||||
const { username } = req.params;
|
const { username } = req.params;
|
||||||
|
|
||||||
const query = `SELECT username FROM user_auth WHERE username = ?`;
|
const query = `SELECT username FROM user_auth WHERE username = ?`;
|
||||||
|
pool.query(query, [username], (err, results) => {
|
||||||
db.get(query, [username], (err, row) => {
|
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error('Error checking username:', err.message);
|
console.error('Error checking username:', err.message);
|
||||||
return res.status(500).json({ error: 'Database error' });
|
return res.status(500).json({ error: 'Database error' });
|
||||||
}
|
}
|
||||||
|
if (results && results.length > 0) {
|
||||||
if (row) {
|
|
||||||
res.status(200).json({ exists: true });
|
res.status(200).json({ exists: true });
|
||||||
} else {
|
} else {
|
||||||
res.status(200).json({ exists: false });
|
res.status(200).json({ exists: false });
|
||||||
@ -233,7 +262,7 @@ app.get('/api/check-username/:username', (req, res) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Upsert user profile (corrected and robust)
|
// =============== UPSERT USER PROFILE (MySQL) ===============
|
||||||
app.post('/api/user-profile', (req, res) => {
|
app.post('/api/user-profile', (req, res) => {
|
||||||
const token = req.headers.authorization?.split(' ')[1];
|
const token = req.headers.authorization?.split(' ')[1];
|
||||||
if (!token) {
|
if (!token) {
|
||||||
@ -262,32 +291,35 @@ app.post('/api/user-profile', (req, res) => {
|
|||||||
career_list,
|
career_list,
|
||||||
} = req.body;
|
} = req.body;
|
||||||
|
|
||||||
// Check existing profile explicitly first
|
// Check existing profile
|
||||||
db.get(`SELECT * FROM user_profile WHERE user_id = ?;`, [userId], (err, existingRow) => {
|
pool.query(`SELECT * FROM user_profile WHERE user_id = ?`, [userId], (err, results) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error('Error checking profile:', err.message);
|
console.error('Error checking profile:', err.message);
|
||||||
return res.status(500).json({ error: 'Database error' });
|
return res.status(500).json({ error: 'Database error' });
|
||||||
}
|
}
|
||||||
|
const existingRow = results && results.length > 0 ? results[0] : null;
|
||||||
|
|
||||||
// Require fields ONLY if no existing profile found
|
|
||||||
if (!existingRow && (!firstName || !lastName || !email || !zipCode || !state || !area)) {
|
if (!existingRow && (!firstName || !lastName || !email || !zipCode || !state || !area)) {
|
||||||
return res.status(400).json({ error: 'All fields are required for initial profile creation.' });
|
return res.status(400).json({ error: 'All fields are required for initial profile creation.' });
|
||||||
}
|
}
|
||||||
|
|
||||||
const finalAnswers = interest_inventory_answers !== undefined
|
const finalAnswers =
|
||||||
? interest_inventory_answers
|
interest_inventory_answers !== undefined
|
||||||
: existingRow?.interest_inventory_answers || null;
|
? interest_inventory_answers
|
||||||
|
: existingRow?.interest_inventory_answers || null;
|
||||||
|
|
||||||
const finalCareerPriorities = career_priorities !== undefined
|
const finalCareerPriorities =
|
||||||
? career_priorities
|
career_priorities !== undefined
|
||||||
: existingRow?.career_priorities || null;
|
? career_priorities
|
||||||
|
: existingRow?.career_priorities || null;
|
||||||
|
|
||||||
const finalCareerList = career_list !== undefined
|
const finalCareerList =
|
||||||
? career_list
|
career_list !== undefined
|
||||||
: existingRow?.career_list || null;
|
? career_list
|
||||||
|
: existingRow?.career_list || null;
|
||||||
|
|
||||||
if (existingRow) {
|
if (existingRow) {
|
||||||
// Update existing profile clearly
|
// Update
|
||||||
const updateQuery = `
|
const updateQuery = `
|
||||||
UPDATE user_profile
|
UPDATE user_profile
|
||||||
SET firstname = ?, lastname = ?, email = ?, zipcode = ?, state = ?, area = ?, career_situation = ?,
|
SET firstname = ?, lastname = ?, email = ?, zipcode = ?, state = ?, area = ?, career_situation = ?,
|
||||||
@ -305,22 +337,21 @@ app.post('/api/user-profile', (req, res) => {
|
|||||||
finalAnswers,
|
finalAnswers,
|
||||||
finalCareerPriorities,
|
finalCareerPriorities,
|
||||||
finalCareerList,
|
finalCareerList,
|
||||||
userId,
|
userId
|
||||||
];
|
];
|
||||||
|
pool.query(updateQuery, params, (err2, result2) => {
|
||||||
db.run(updateQuery, params, function (err) {
|
if (err2) {
|
||||||
if (err) {
|
console.error('Update error:', err2.message);
|
||||||
console.error('Update error:', err.message);
|
|
||||||
return res.status(500).json({ error: 'Failed to update user profile' });
|
return res.status(500).json({ error: 'Failed to update user profile' });
|
||||||
}
|
}
|
||||||
res.status(200).json({ message: 'User profile updated successfully' });
|
res.status(200).json({ message: 'User profile updated successfully' });
|
||||||
});
|
});
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// Insert new profile clearly
|
// Insert new profile
|
||||||
const insertQuery = `
|
const insertQuery = `
|
||||||
INSERT INTO user_profile
|
INSERT INTO user_profile
|
||||||
(user_id, firstname, lastname, email, zipcode, state, area, career_situation, interest_inventory_answers, career_priorities, career_list)
|
(user_id, firstname, lastname, email, zipcode, state, area, career_situation,
|
||||||
|
interest_inventory_answers, career_priorities, career_list)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
`;
|
`;
|
||||||
const params = [
|
const params = [
|
||||||
@ -336,61 +367,54 @@ app.post('/api/user-profile', (req, res) => {
|
|||||||
finalCareerPriorities,
|
finalCareerPriorities,
|
||||||
finalCareerList,
|
finalCareerList,
|
||||||
];
|
];
|
||||||
|
pool.query(insertQuery, params, (err3, result3) => {
|
||||||
db.run(insertQuery, params, function (err) {
|
if (err3) {
|
||||||
if (err) {
|
console.error('Insert error:', err3.message);
|
||||||
console.error('Insert error:', err.message);
|
|
||||||
return res.status(500).json({ error: 'Failed to create user profile' });
|
return res.status(500).json({ error: 'Failed to create user profile' });
|
||||||
}
|
}
|
||||||
res.status(201).json({ message: 'User profile created successfully', id: this.lastID });
|
res.status(201).json({ message: 'User profile created successfully', id: result3.insertId });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// =============== FETCH USER PROFILE (MySQL) ===============
|
||||||
// Route to fetch user profile
|
|
||||||
app.get('/api/user-profile', (req, res) => {
|
app.get('/api/user-profile', (req, res) => {
|
||||||
const token = req.headers.authorization?.split(' ')[1];
|
const token = req.headers.authorization?.split(' ')[1];
|
||||||
if (!token) {
|
if (!token) {
|
||||||
return res.status(401).json({ error: 'Authorization token is required' });
|
return res.status(401).json({ error: 'Authorization token is required' });
|
||||||
}
|
}
|
||||||
|
let userId;
|
||||||
try {
|
try {
|
||||||
// Extract the userId (user_id) from the token
|
const decoded = jwt.verify(token, SECRET_KEY);
|
||||||
const { userId } = jwt.verify(token, SECRET_KEY);
|
userId = decoded.userId;
|
||||||
|
|
||||||
// Query user_profile using user_id
|
|
||||||
const query = 'SELECT * FROM user_profile WHERE user_id = ?';
|
|
||||||
db.get(query, [userId], (err, row) => {
|
|
||||||
if (err) {
|
|
||||||
console.error('Error fetching user profile:', err.message);
|
|
||||||
return res.status(500).json({ error: 'Internal server error' });
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!row) {
|
|
||||||
return res.status(404).json({ error: 'User profile not found' });
|
|
||||||
}
|
|
||||||
|
|
||||||
res.status(200).json(row); // Return the profile row
|
|
||||||
});
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error verifying token:', error.message);
|
console.error('Error verifying token:', error.message);
|
||||||
res.status(401).json({ error: 'Invalid or expired token' });
|
return res.status(401).json({ error: 'Invalid or expired token' });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const query = 'SELECT * FROM user_profile WHERE user_id = ?';
|
||||||
|
pool.query(query, [userId], (err, results) => {
|
||||||
|
if (err) {
|
||||||
|
console.error('Error fetching user profile:', err.message);
|
||||||
|
return res.status(500).json({ error: 'Internal server error' });
|
||||||
|
}
|
||||||
|
if (!results || results.length === 0) {
|
||||||
|
return res.status(404).json({ error: 'User profile not found' });
|
||||||
|
}
|
||||||
|
res.status(200).json(results[0]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// =============== SALARY_INFO REMAINS IN SQLITE ===============
|
||||||
// Route to fetch areas by state
|
|
||||||
app.get('/api/areas', (req, res) => {
|
app.get('/api/areas', (req, res) => {
|
||||||
const { state } = req.query;
|
const { state } = req.query;
|
||||||
|
|
||||||
if (!state) {
|
if (!state) {
|
||||||
return res.status(400).json({ error: 'State parameter is required' });
|
return res.status(400).json({ error: 'State parameter is required' });
|
||||||
}
|
}
|
||||||
|
|
||||||
const dbPath = path.resolve('/home/jcoakley/aptiva-dev1-app/salary_info.db'); // Path to salary_info.db
|
const salaryDbPath = path.resolve('/home/jcoakley/aptiva-dev1-app/salary_info.db');
|
||||||
const db = new sqlite3.Database(dbPath, sqlite3.OPEN_READONLY, (err) => {
|
const salaryDb = new sqlite3.Database(salaryDbPath, sqlite3.OPEN_READONLY, (err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error('Error connecting to database:', err.message);
|
console.error('Error connecting to database:', err.message);
|
||||||
return res.status(500).json({ error: 'Failed to connect to database' });
|
return res.status(500).json({ error: 'Failed to connect to database' });
|
||||||
@ -398,27 +422,23 @@ app.get('/api/areas', (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const query = `SELECT DISTINCT AREA_TITLE FROM salary_data WHERE PRIM_STATE = ?`;
|
const query = `SELECT DISTINCT AREA_TITLE FROM salary_data WHERE PRIM_STATE = ?`;
|
||||||
|
salaryDb.all(query, [state], (err, rows) => {
|
||||||
db.all(query, [state], (err, rows) => {
|
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error('Error executing query:', err.message);
|
console.error('Error executing query:', err.message);
|
||||||
return res.status(500).json({ error: 'Failed to fetch areas' });
|
return res.status(500).json({ error: 'Failed to fetch areas' });
|
||||||
}
|
}
|
||||||
|
|
||||||
const areas = rows.map((row) => row.AREA_TITLE);
|
const areas = rows.map((row) => row.AREA_TITLE);
|
||||||
res.json({ areas });
|
res.json({ areas });
|
||||||
});
|
});
|
||||||
|
|
||||||
db.close((err) => {
|
salaryDb.close((err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error('Error closing the database:', err.message);
|
console.error('Error closing the salary_info.db:', err.message);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// =============== START SERVER ===============
|
||||||
|
|
||||||
// Start the server
|
|
||||||
app.listen(PORT, () => {
|
app.listen(PORT, () => {
|
||||||
console.log(`Server running on http://localhost:${PORT}`);
|
console.log(`Server running on http://localhost:${PORT}`);
|
||||||
});
|
});
|
||||||
|
4
export_salary.sql
Normal file
4
export_salary.sql
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
.mode csv
|
||||||
|
.headers on
|
||||||
|
.once salary_data.csv
|
||||||
|
SELECT * FROM salary_data;
|
110
package-lock.json
generated
110
package-lock.json
generated
@ -32,6 +32,7 @@
|
|||||||
"mammoth": "^1.9.0",
|
"mammoth": "^1.9.0",
|
||||||
"moment": "^2.30.1",
|
"moment": "^2.30.1",
|
||||||
"multer": "^1.4.5-lts.2",
|
"multer": "^1.4.5-lts.2",
|
||||||
|
"mysql2": "^3.14.1",
|
||||||
"openai": "^4.97.0",
|
"openai": "^4.97.0",
|
||||||
"pdf-parse": "^1.1.1",
|
"pdf-parse": "^1.1.1",
|
||||||
"pdfjs-dist": "^3.11.174",
|
"pdfjs-dist": "^3.11.174",
|
||||||
@ -5489,6 +5490,15 @@
|
|||||||
"url": "https://github.com/sponsors/ljharb"
|
"url": "https://github.com/sponsors/ljharb"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/aws-ssl-profiles": {
|
||||||
|
"version": "1.1.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/aws-ssl-profiles/-/aws-ssl-profiles-1.1.2.tgz",
|
||||||
|
"integrity": "sha512-NZKeq9AfyQvEeNlN0zSYAaWrmBffJh3IELMZfRpJVWgrpEbtEpnjvzqBPf+mxoI287JohRDoa+/nsfqqiZmF6g==",
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 6.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/axe-core": {
|
"node_modules/axe-core": {
|
||||||
"version": "4.10.3",
|
"version": "4.10.3",
|
||||||
"resolved": "https://registry.npmjs.org/axe-core/-/axe-core-4.10.3.tgz",
|
"resolved": "https://registry.npmjs.org/axe-core/-/axe-core-4.10.3.tgz",
|
||||||
@ -7575,6 +7585,15 @@
|
|||||||
"integrity": "sha512-bd2L678uiWATM6m5Z1VzNCErI3jiGzt6HGY8OVICs40JQq/HALfbyNJmp0UDakEY4pMMaN0Ly5om/B1VI/+xfQ==",
|
"integrity": "sha512-bd2L678uiWATM6m5Z1VzNCErI3jiGzt6HGY8OVICs40JQq/HALfbyNJmp0UDakEY4pMMaN0Ly5om/B1VI/+xfQ==",
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
|
"node_modules/denque": {
|
||||||
|
"version": "2.1.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/denque/-/denque-2.1.0.tgz",
|
||||||
|
"integrity": "sha512-HVQE3AAb/pxF8fQAoiqpvg9i3evqug3hoiwakOyZAwJm+6vZehbkYXZ0l4JxS+I3QxM97v5aaRNhj8v5oBhekw==",
|
||||||
|
"license": "Apache-2.0",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=0.10"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/depd": {
|
"node_modules/depd": {
|
||||||
"version": "2.0.0",
|
"version": "2.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz",
|
||||||
@ -9687,6 +9706,15 @@
|
|||||||
"node": ">=10"
|
"node": ">=10"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/generate-function": {
|
||||||
|
"version": "2.3.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/generate-function/-/generate-function-2.3.1.tgz",
|
||||||
|
"integrity": "sha512-eeB5GfMNeevm/GRYq20ShmsaGcmI81kIX2K9XQx5miC8KdHaC6Jm0qQ8ZNeGOi7wYB8OsdxKs+Y2oVuTFuVwKQ==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"is-property": "^1.0.2"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/gensync": {
|
"node_modules/gensync": {
|
||||||
"version": "1.0.0-beta.2",
|
"version": "1.0.0-beta.2",
|
||||||
"resolved": "https://registry.npmjs.org/gensync/-/gensync-1.0.0-beta.2.tgz",
|
"resolved": "https://registry.npmjs.org/gensync/-/gensync-1.0.0-beta.2.tgz",
|
||||||
@ -10882,6 +10910,12 @@
|
|||||||
"integrity": "sha512-bCYeRA2rVibKZd+s2625gGnGF/t7DSqDs4dP7CrLA1m7jKWz6pps0LpYLJN8Q64HtmPKJ1hrN3nzPNKFEKOUiQ==",
|
"integrity": "sha512-bCYeRA2rVibKZd+s2625gGnGF/t7DSqDs4dP7CrLA1m7jKWz6pps0LpYLJN8Q64HtmPKJ1hrN3nzPNKFEKOUiQ==",
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
|
"node_modules/is-property": {
|
||||||
|
"version": "1.0.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/is-property/-/is-property-1.0.2.tgz",
|
||||||
|
"integrity": "sha512-Ks/IoX00TtClbGQr4TWXemAnktAQvYB7HzcCxDGqEZU6oCmb2INHuOoKxbtR+HFkmYWBKv/dOZtGRiAjDhj92g==",
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
"node_modules/is-regex": {
|
"node_modules/is-regex": {
|
||||||
"version": "1.2.1",
|
"version": "1.2.1",
|
||||||
"resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.2.1.tgz",
|
"resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.2.1.tgz",
|
||||||
@ -12700,6 +12734,12 @@
|
|||||||
"integrity": "sha512-xfBaXQd9ryd9dlSDvnvI0lvxfLJlYAZzXomUYzLKtUeOQvOP5piqAWuGtrhWeqaXK9hhoM/iyJc5AV+XfsX3HQ==",
|
"integrity": "sha512-xfBaXQd9ryd9dlSDvnvI0lvxfLJlYAZzXomUYzLKtUeOQvOP5piqAWuGtrhWeqaXK9hhoM/iyJc5AV+XfsX3HQ==",
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
|
"node_modules/long": {
|
||||||
|
"version": "5.3.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/long/-/long-5.3.2.tgz",
|
||||||
|
"integrity": "sha512-mNAgZ1GmyNhD7AuqnTG3/VQ26o760+ZYBPKjPvugO8+nLbYfX6TVpJPseBvopbdY+qpZ/lKUnmEc1LeZYS3QAA==",
|
||||||
|
"license": "Apache-2.0"
|
||||||
|
},
|
||||||
"node_modules/loose-envify": {
|
"node_modules/loose-envify": {
|
||||||
"version": "1.4.0",
|
"version": "1.4.0",
|
||||||
"resolved": "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz",
|
"resolved": "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz",
|
||||||
@ -12741,6 +12781,21 @@
|
|||||||
"yallist": "^3.0.2"
|
"yallist": "^3.0.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/lru.min": {
|
||||||
|
"version": "1.1.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/lru.min/-/lru.min-1.1.2.tgz",
|
||||||
|
"integrity": "sha512-Nv9KddBcQSlQopmBHXSsZVY5xsdlZkdH/Iey0BlcBYggMd4two7cZnKOK9vmy3nY0O5RGH99z1PCeTpPqszUYg==",
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"bun": ">=1.0.0",
|
||||||
|
"deno": ">=1.30.0",
|
||||||
|
"node": ">=8.0.0"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"type": "github",
|
||||||
|
"url": "https://github.com/sponsors/wellwelwel"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/lucide-react": {
|
"node_modules/lucide-react": {
|
||||||
"version": "0.483.0",
|
"version": "0.483.0",
|
||||||
"resolved": "https://registry.npmjs.org/lucide-react/-/lucide-react-0.483.0.tgz",
|
"resolved": "https://registry.npmjs.org/lucide-react/-/lucide-react-0.483.0.tgz",
|
||||||
@ -13209,6 +13264,26 @@
|
|||||||
"multicast-dns": "cli.js"
|
"multicast-dns": "cli.js"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/mysql2": {
|
||||||
|
"version": "3.14.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/mysql2/-/mysql2-3.14.1.tgz",
|
||||||
|
"integrity": "sha512-7ytuPQJjQB8TNAYX/H2yhL+iQOnIBjAMam361R7UAL0lOVXWjtdrmoL9HYKqKoLp/8UUTRcvo1QPvK9KL7wA8w==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"aws-ssl-profiles": "^1.1.1",
|
||||||
|
"denque": "^2.1.0",
|
||||||
|
"generate-function": "^2.3.1",
|
||||||
|
"iconv-lite": "^0.6.3",
|
||||||
|
"long": "^5.2.1",
|
||||||
|
"lru.min": "^1.0.0",
|
||||||
|
"named-placeholders": "^1.1.3",
|
||||||
|
"seq-queue": "^0.0.5",
|
||||||
|
"sqlstring": "^2.3.2"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 8.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/mz": {
|
"node_modules/mz": {
|
||||||
"version": "2.7.0",
|
"version": "2.7.0",
|
||||||
"resolved": "https://registry.npmjs.org/mz/-/mz-2.7.0.tgz",
|
"resolved": "https://registry.npmjs.org/mz/-/mz-2.7.0.tgz",
|
||||||
@ -13220,6 +13295,27 @@
|
|||||||
"thenify-all": "^1.0.0"
|
"thenify-all": "^1.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/named-placeholders": {
|
||||||
|
"version": "1.1.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/named-placeholders/-/named-placeholders-1.1.3.tgz",
|
||||||
|
"integrity": "sha512-eLoBxg6wE/rZkJPhU/xRX1WTpkFEwDJEN96oxFrTsqBdbT5ec295Q+CoHrL9IT0DipqKhmGcaZmwOt8OON5x1w==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"lru-cache": "^7.14.1"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=12.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/named-placeholders/node_modules/lru-cache": {
|
||||||
|
"version": "7.18.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-7.18.3.tgz",
|
||||||
|
"integrity": "sha512-jumlc0BIUrS3qJGgIkWZsyfAM7NCWiBcCDhnd+3NNM5KbBmLTgHVfWBcg6W+rLUsIpzpERPsvwUP7CckAQSOoA==",
|
||||||
|
"license": "ISC",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=12"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/nan": {
|
"node_modules/nan": {
|
||||||
"version": "2.22.2",
|
"version": "2.22.2",
|
||||||
"resolved": "https://registry.npmjs.org/nan/-/nan-2.22.2.tgz",
|
"resolved": "https://registry.npmjs.org/nan/-/nan-2.22.2.tgz",
|
||||||
@ -17094,6 +17190,11 @@
|
|||||||
"node": ">= 0.8"
|
"node": ">= 0.8"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/seq-queue": {
|
||||||
|
"version": "0.0.5",
|
||||||
|
"resolved": "https://registry.npmjs.org/seq-queue/-/seq-queue-0.0.5.tgz",
|
||||||
|
"integrity": "sha512-hr3Wtp/GZIc/6DAGPDcV4/9WoZhjrkXsi5B/07QgX8tsdc6ilr7BFM6PM6rbdAX1kFSDYeZGLipIZZKyQP0O5Q=="
|
||||||
|
},
|
||||||
"node_modules/serialize-javascript": {
|
"node_modules/serialize-javascript": {
|
||||||
"version": "6.0.2",
|
"version": "6.0.2",
|
||||||
"resolved": "https://registry.npmjs.org/serialize-javascript/-/serialize-javascript-6.0.2.tgz",
|
"resolved": "https://registry.npmjs.org/serialize-javascript/-/serialize-javascript-6.0.2.tgz",
|
||||||
@ -17646,6 +17747,15 @@
|
|||||||
"integrity": "sha512-5m3bsyrjFWE1xf7nz7YXdN4udnVtXK6/Yfgn5qnahL6bCkf2yKt4k3nuTKAtT4r3IG8JNR2ncsIMdZuAzJjHQQ==",
|
"integrity": "sha512-5m3bsyrjFWE1xf7nz7YXdN4udnVtXK6/Yfgn5qnahL6bCkf2yKt4k3nuTKAtT4r3IG8JNR2ncsIMdZuAzJjHQQ==",
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
|
"node_modules/sqlstring": {
|
||||||
|
"version": "2.3.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/sqlstring/-/sqlstring-2.3.3.tgz",
|
||||||
|
"integrity": "sha512-qC9iz2FlN7DQl3+wjwn3802RTyjCx7sDvfQEXchwa6CWOx07/WVfh91gBmQ9fahw8snwGEWU3xGzOt4tFyHLxg==",
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/ssf": {
|
"node_modules/ssf": {
|
||||||
"version": "0.11.2",
|
"version": "0.11.2",
|
||||||
"resolved": "https://registry.npmjs.org/ssf/-/ssf-0.11.2.tgz",
|
"resolved": "https://registry.npmjs.org/ssf/-/ssf-0.11.2.tgz",
|
||||||
|
@ -27,6 +27,7 @@
|
|||||||
"mammoth": "^1.9.0",
|
"mammoth": "^1.9.0",
|
||||||
"moment": "^2.30.1",
|
"moment": "^2.30.1",
|
||||||
"multer": "^1.4.5-lts.2",
|
"multer": "^1.4.5-lts.2",
|
||||||
|
"mysql2": "^3.14.1",
|
||||||
"openai": "^4.97.0",
|
"openai": "^4.97.0",
|
||||||
"pdf-parse": "^1.1.1",
|
"pdf-parse": "^1.1.1",
|
||||||
"pdfjs-dist": "^3.11.174",
|
"pdfjs-dist": "^3.11.174",
|
||||||
|
413428
salary_data.csv
Normal file
413428
salary_data.csv
Normal file
File diff suppressed because it is too large
Load Diff
216
user_profile.sql
216
user_profile.sql
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user