fixed career_list save
This commit is contained in:
parent
0194e9fc19
commit
b71920f68e
@ -233,7 +233,7 @@ app.get('/api/check-username/:username', (req, res) => {
|
||||
});
|
||||
});
|
||||
|
||||
// Upsert user profile with career_priorities and career_list
|
||||
// Upsert user profile (corrected and robust)
|
||||
app.post('/api/user-profile', (req, res) => {
|
||||
const token = req.headers.authorization?.split(' ')[1];
|
||||
if (!token) {
|
||||
@ -262,75 +262,69 @@ app.post('/api/user-profile', (req, res) => {
|
||||
career_list,
|
||||
} = req.body;
|
||||
|
||||
if (!firstName || !lastName || !email || !zipCode || !state || !area) {
|
||||
return res.status(400).json({ error: 'All fields are required (except interest_inventory_answers, career_priorities, career_list)' });
|
||||
}
|
||||
|
||||
// Check for existing user profile
|
||||
const checkQuery = `SELECT * FROM user_profile WHERE user_id = ?;`;
|
||||
db.get(checkQuery, [userId], (err, existingRow) => {
|
||||
// Check existing profile explicitly first
|
||||
db.get(`SELECT * FROM user_profile WHERE user_id = ?;`, [userId], (err, existingRow) => {
|
||||
if (err) {
|
||||
console.error('Error checking profile:', err.message);
|
||||
return res.status(500).json({ error: 'Database error' });
|
||||
}
|
||||
|
||||
// Preserve existing data if not provided
|
||||
const finalAnswers = interest_inventory_answers === undefined
|
||||
? existingRow?.interest_inventory_answers || null
|
||||
: interest_inventory_answers;
|
||||
// Require fields ONLY if no existing profile found
|
||||
if (!existingRow && (!firstName || !lastName || !email || !zipCode || !state || !area)) {
|
||||
return res.status(400).json({ error: 'All fields are required for initial profile creation.' });
|
||||
}
|
||||
|
||||
const finalCareerPriorities = career_priorities === undefined
|
||||
? existingRow?.career_priorities || null
|
||||
: career_priorities;
|
||||
const finalAnswers = interest_inventory_answers !== undefined
|
||||
? interest_inventory_answers
|
||||
: existingRow?.interest_inventory_answers || null;
|
||||
|
||||
const finalCareerList = career_list === undefined
|
||||
? existingRow?.career_list || null
|
||||
: career_list;
|
||||
const finalCareerPriorities = career_priorities !== undefined
|
||||
? career_priorities
|
||||
: existingRow?.career_priorities || null;
|
||||
|
||||
const finalCareerList = career_list !== undefined
|
||||
? career_list
|
||||
: existingRow?.career_list || null;
|
||||
|
||||
if (existingRow) {
|
||||
// Update existing profile
|
||||
// Update existing profile clearly
|
||||
const updateQuery = `
|
||||
UPDATE user_profile
|
||||
SET firstname = ?,
|
||||
lastname = ?,
|
||||
email = ?,
|
||||
zipcode = ?,
|
||||
state = ?,
|
||||
area = ?,
|
||||
career_situation = ?,
|
||||
interest_inventory_answers = ?,
|
||||
career_priorities = ?,
|
||||
career_list = ?
|
||||
SET firstname = ?, lastname = ?, email = ?, zipcode = ?, state = ?, area = ?, career_situation = ?,
|
||||
interest_inventory_answers = ?, career_priorities = ?, career_list = ?
|
||||
WHERE user_id = ?
|
||||
`;
|
||||
const params = [
|
||||
firstName,
|
||||
lastName,
|
||||
email,
|
||||
zipCode,
|
||||
state,
|
||||
area,
|
||||
firstName || existingRow.firstname,
|
||||
lastName || existingRow.lastname,
|
||||
email || existingRow.email,
|
||||
zipCode || existingRow.zipcode,
|
||||
state || existingRow.state,
|
||||
area || existingRow.area,
|
||||
careerSituation || existingRow.career_situation,
|
||||
finalAnswers,
|
||||
finalCareerPriorities,
|
||||
finalCareerList,
|
||||
userId,
|
||||
];
|
||||
|
||||
db.run(updateQuery, params, function (err) {
|
||||
if (err) {
|
||||
console.error('Error updating profile:', err.message);
|
||||
console.error('Update error:', err.message);
|
||||
return res.status(500).json({ error: 'Failed to update user profile' });
|
||||
}
|
||||
return res.status(200).json({ message: 'User profile updated successfully' });
|
||||
res.status(200).json({ message: 'User profile updated successfully' });
|
||||
});
|
||||
|
||||
} else {
|
||||
// Insert new profile
|
||||
// Insert new profile clearly
|
||||
const insertQuery = `
|
||||
INSERT INTO user_profile
|
||||
(firstname, lastname, email, zipcode, state, area, career_situation, interest_inventory_answers, career_priorities, career_list, user_id)
|
||||
(user_id, firstname, lastname, email, zipcode, state, area, career_situation, interest_inventory_answers, career_priorities, career_list)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`;
|
||||
const params = [
|
||||
userId,
|
||||
firstName,
|
||||
lastName,
|
||||
email,
|
||||
@ -341,23 +335,20 @@ app.post('/api/user-profile', (req, res) => {
|
||||
finalAnswers,
|
||||
finalCareerPriorities,
|
||||
finalCareerList,
|
||||
userId,
|
||||
];
|
||||
|
||||
db.run(insertQuery, params, function (err) {
|
||||
if (err) {
|
||||
console.error('Error inserting profile:', err.message);
|
||||
console.error('Insert error:', err.message);
|
||||
return res.status(500).json({ error: 'Failed to create user profile' });
|
||||
}
|
||||
return res
|
||||
.status(201)
|
||||
.json({ message: 'User profile created successfully', id: this.lastID });
|
||||
res.status(201).json({ message: 'User profile created successfully', id: this.lastID });
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
// Route to fetch user profile
|
||||
app.get('/api/user-profile', (req, res) => {
|
||||
const token = req.headers.authorization?.split(' ')[1];
|
||||
|
@ -57,6 +57,35 @@ function CareerExplorer({ handleCareerClick, userState, areaTitle }) {
|
||||
return masterCareerRatings.find(c => c.soc_code === socCode)?.ratings || {};
|
||||
};
|
||||
|
||||
const saveCareerListToBackend = async (newCareerList) => {
|
||||
try {
|
||||
const token = localStorage.getItem('token');
|
||||
await axios.post(`${apiUrl}/user-profile`, {
|
||||
// Provide all required fields from userProfile
|
||||
// If your DB requires them, fill them in here:
|
||||
firstName: userProfile?.firstname,
|
||||
lastName: userProfile?.lastname,
|
||||
email: userProfile?.email,
|
||||
zipCode: userProfile?.zipcode,
|
||||
state: userProfile?.state,
|
||||
area: userProfile?.area,
|
||||
careerSituation: userProfile?.career_situation,
|
||||
|
||||
// For the rest, ensure we're not overwriting if not needed
|
||||
interest_inventory_answers: userProfile?.interest_inventory_answers,
|
||||
career_priorities: userProfile?.career_priorities,
|
||||
|
||||
// IMPORTANT: We convert the newCareerList to JSON if your DB expects text
|
||||
career_list: JSON.stringify(newCareerList),
|
||||
}, {
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('Error saving career_list:', err);
|
||||
// optional: show a user-friendly error
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
const addCareerToList = (career) => {
|
||||
const masterRatings = getCareerRatingsBySocCode(career.code);
|
||||
@ -67,13 +96,18 @@ function CareerExplorer({ handleCareerClick, userState, areaTitle }) {
|
||||
Good: 3,
|
||||
};
|
||||
|
||||
const interestsRating = priorities.interests === 'I’m not sure yet'
|
||||
? parseInt(prompt('Rate your interest in this career (1-5):', '3'), 10)
|
||||
const interestsRating =
|
||||
priorities.interests === "I’m not sure yet"
|
||||
? parseInt(prompt("Rate your interest in this career (1-5):", "3"), 10)
|
||||
: fitRatingMap[career.fit] || masterRatings.interests || 3;
|
||||
|
||||
const meaningRating = parseInt(prompt('How meaningful is this career to you? (1-5):', '3'), 10);
|
||||
const meaningRating = parseInt(
|
||||
prompt("How meaningful is this career to you? (1-5):", "3"),
|
||||
10
|
||||
);
|
||||
|
||||
const stabilityRating = career.ratings && career.ratings.stability !== undefined
|
||||
const stabilityRating =
|
||||
career.ratings && career.ratings.stability !== undefined
|
||||
? career.ratings.stability
|
||||
: masterRatings.stability || 3;
|
||||
|
||||
@ -95,15 +129,25 @@ function CareerExplorer({ handleCareerClick, userState, areaTitle }) {
|
||||
|
||||
setCareerList((prevList) => {
|
||||
if (prevList.some((c) => c.code === career.code)) {
|
||||
alert('Career already in comparison list.');
|
||||
alert("Career already in comparison list.");
|
||||
return prevList;
|
||||
}
|
||||
return [...prevList, careerWithUserRatings];
|
||||
|
||||
const newList = [...prevList, careerWithUserRatings];
|
||||
// Call the API to save
|
||||
saveCareerListToBackend(newList);
|
||||
return newList;
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
const removeCareerFromList = (careerCode) => {
|
||||
setCareerList((prevList) => prevList.filter((c) => c.code !== careerCode));
|
||||
setCareerList((prevList) => {
|
||||
const newList = prevList.filter((c) => c.code !== careerCode);
|
||||
// Call the API to save
|
||||
saveCareerListToBackend(newList);
|
||||
return newList;
|
||||
});
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
@ -118,6 +162,14 @@ function CareerExplorer({ handleCareerClick, userState, areaTitle }) {
|
||||
const profileData = res.data;
|
||||
setUserProfile(profileData);
|
||||
|
||||
if (profileData.career_list) {
|
||||
try {
|
||||
setCareerList(JSON.parse(profileData.career_list));
|
||||
} catch (err) {
|
||||
console.error('Error parsing career_list:', err);
|
||||
}
|
||||
}
|
||||
|
||||
const priorities = profileData.career_priorities
|
||||
? JSON.parse(profileData.career_priorities)
|
||||
: {};
|
||||
|
BIN
user_profile.db
BIN
user_profile.db
Binary file not shown.
Loading…
Reference in New Issue
Block a user