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) => {
|
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,75 +262,69 @@ app.post('/api/user-profile', (req, res) => {
|
|||||||
career_list,
|
career_list,
|
||||||
} = req.body;
|
} = req.body;
|
||||||
|
|
||||||
if (!firstName || !lastName || !email || !zipCode || !state || !area) {
|
// Check existing profile explicitly first
|
||||||
return res.status(400).json({ error: 'All fields are required (except interest_inventory_answers, career_priorities, career_list)' });
|
db.get(`SELECT * FROM user_profile WHERE user_id = ?;`, [userId], (err, existingRow) => {
|
||||||
}
|
|
||||||
|
|
||||||
// Check for existing user profile
|
|
||||||
const checkQuery = `SELECT * FROM user_profile WHERE user_id = ?;`;
|
|
||||||
db.get(checkQuery, [userId], (err, existingRow) => {
|
|
||||||
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' });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Preserve existing data if not provided
|
// Require fields ONLY if no existing profile found
|
||||||
const finalAnswers = interest_inventory_answers === undefined
|
if (!existingRow && (!firstName || !lastName || !email || !zipCode || !state || !area)) {
|
||||||
? existingRow?.interest_inventory_answers || null
|
return res.status(400).json({ error: 'All fields are required for initial profile creation.' });
|
||||||
: interest_inventory_answers;
|
}
|
||||||
|
|
||||||
const finalCareerPriorities = career_priorities === undefined
|
const finalAnswers = interest_inventory_answers !== undefined
|
||||||
? existingRow?.career_priorities || null
|
? interest_inventory_answers
|
||||||
: career_priorities;
|
: existingRow?.interest_inventory_answers || null;
|
||||||
|
|
||||||
const finalCareerList = career_list === undefined
|
const finalCareerPriorities = career_priorities !== undefined
|
||||||
? existingRow?.career_list || null
|
? career_priorities
|
||||||
: career_list;
|
: existingRow?.career_priorities || null;
|
||||||
|
|
||||||
|
const finalCareerList = career_list !== undefined
|
||||||
|
? career_list
|
||||||
|
: existingRow?.career_list || null;
|
||||||
|
|
||||||
if (existingRow) {
|
if (existingRow) {
|
||||||
// Update existing profile
|
// Update existing profile clearly
|
||||||
const updateQuery = `
|
const updateQuery = `
|
||||||
UPDATE user_profile
|
UPDATE user_profile
|
||||||
SET firstname = ?,
|
SET firstname = ?, lastname = ?, email = ?, zipcode = ?, state = ?, area = ?, career_situation = ?,
|
||||||
lastname = ?,
|
interest_inventory_answers = ?, career_priorities = ?, career_list = ?
|
||||||
email = ?,
|
|
||||||
zipcode = ?,
|
|
||||||
state = ?,
|
|
||||||
area = ?,
|
|
||||||
career_situation = ?,
|
|
||||||
interest_inventory_answers = ?,
|
|
||||||
career_priorities = ?,
|
|
||||||
career_list = ?
|
|
||||||
WHERE user_id = ?
|
WHERE user_id = ?
|
||||||
`;
|
`;
|
||||||
const params = [
|
const params = [
|
||||||
firstName,
|
firstName || existingRow.firstname,
|
||||||
lastName,
|
lastName || existingRow.lastname,
|
||||||
email,
|
email || existingRow.email,
|
||||||
zipCode,
|
zipCode || existingRow.zipcode,
|
||||||
state,
|
state || existingRow.state,
|
||||||
area,
|
area || existingRow.area,
|
||||||
careerSituation || existingRow.career_situation,
|
careerSituation || existingRow.career_situation,
|
||||||
finalAnswers,
|
finalAnswers,
|
||||||
finalCareerPriorities,
|
finalCareerPriorities,
|
||||||
finalCareerList,
|
finalCareerList,
|
||||||
userId,
|
userId,
|
||||||
];
|
];
|
||||||
|
|
||||||
db.run(updateQuery, params, function (err) {
|
db.run(updateQuery, params, function (err) {
|
||||||
if (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(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 {
|
} else {
|
||||||
// Insert new profile
|
// Insert new profile clearly
|
||||||
const insertQuery = `
|
const insertQuery = `
|
||||||
INSERT INTO user_profile
|
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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
`;
|
`;
|
||||||
const params = [
|
const params = [
|
||||||
|
userId,
|
||||||
firstName,
|
firstName,
|
||||||
lastName,
|
lastName,
|
||||||
email,
|
email,
|
||||||
@ -341,23 +335,20 @@ app.post('/api/user-profile', (req, res) => {
|
|||||||
finalAnswers,
|
finalAnswers,
|
||||||
finalCareerPriorities,
|
finalCareerPriorities,
|
||||||
finalCareerList,
|
finalCareerList,
|
||||||
userId,
|
|
||||||
];
|
];
|
||||||
|
|
||||||
db.run(insertQuery, params, function (err) {
|
db.run(insertQuery, params, function (err) {
|
||||||
if (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(500).json({ error: 'Failed to create user profile' });
|
||||||
}
|
}
|
||||||
return res
|
res.status(201).json({ message: 'User profile created successfully', id: this.lastID });
|
||||||
.status(201)
|
|
||||||
.json({ message: 'User profile created successfully', id: this.lastID });
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Route to fetch user profile
|
// 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];
|
||||||
|
@ -57,6 +57,35 @@ function CareerExplorer({ handleCareerClick, userState, areaTitle }) {
|
|||||||
return masterCareerRatings.find(c => c.soc_code === socCode)?.ratings || {};
|
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 addCareerToList = (career) => {
|
||||||
const masterRatings = getCareerRatingsBySocCode(career.code);
|
const masterRatings = getCareerRatingsBySocCode(career.code);
|
||||||
@ -67,13 +96,18 @@ function CareerExplorer({ handleCareerClick, userState, areaTitle }) {
|
|||||||
Good: 3,
|
Good: 3,
|
||||||
};
|
};
|
||||||
|
|
||||||
const interestsRating = priorities.interests === 'I’m not sure yet'
|
const interestsRating =
|
||||||
? parseInt(prompt('Rate your interest in this career (1-5):', '3'), 10)
|
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;
|
: 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
|
? career.ratings.stability
|
||||||
: masterRatings.stability || 3;
|
: masterRatings.stability || 3;
|
||||||
|
|
||||||
@ -95,15 +129,25 @@ function CareerExplorer({ handleCareerClick, userState, areaTitle }) {
|
|||||||
|
|
||||||
setCareerList((prevList) => {
|
setCareerList((prevList) => {
|
||||||
if (prevList.some((c) => c.code === career.code)) {
|
if (prevList.some((c) => c.code === career.code)) {
|
||||||
alert('Career already in comparison list.');
|
alert("Career already in comparison list.");
|
||||||
return prevList;
|
return prevList;
|
||||||
}
|
}
|
||||||
return [...prevList, careerWithUserRatings];
|
|
||||||
|
const newList = [...prevList, careerWithUserRatings];
|
||||||
|
// Call the API to save
|
||||||
|
saveCareerListToBackend(newList);
|
||||||
|
return newList;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
const removeCareerFromList = (careerCode) => {
|
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(() => {
|
useEffect(() => {
|
||||||
@ -118,6 +162,14 @@ function CareerExplorer({ handleCareerClick, userState, areaTitle }) {
|
|||||||
const profileData = res.data;
|
const profileData = res.data;
|
||||||
setUserProfile(profileData);
|
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
|
const priorities = profileData.career_priorities
|
||||||
? JSON.parse(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