@@ -4,6 +4,14 @@ const express = require('express');
44const router = express . Router ( ) ;
55const { requireAuth } = require ( '../middleware/auth' ) ;
66
7+ function normalizeUserType ( value ) {
8+ const normalized = String ( value || '' ) . trim ( ) . toLowerCase ( ) ;
9+ if ( normalized === 'high_school' || normalized === 'university' || normalized === 'professional' ) {
10+ return normalized ;
11+ }
12+ return null ;
13+ }
14+
715router . get ( '/:id' , requireAuth , ( req , res ) => {
816 const { id } = req . params ;
917 if ( req . user . user_type !== 'admin' && String ( req . user . user_id ) !== String ( id ) ) {
@@ -22,6 +30,7 @@ router.put('/:id', requireAuth, (req, res) => {
2230 const { id } = req . params ;
2331 const targetId = req . user . user_type === 'admin' ? id : req . user . user_id ;
2432 const { skills, interests, education_level, current_grade, work_experience_years, preferred_work_style } = req . body ;
33+ const normalizedEducationLevel = normalizeUserType ( education_level ) ;
2534
2635 const query = `
2736 INSERT INTO profiles (user_id, skills, interests, education_level, current_grade, work_experience_years, preferred_work_style, updated_at)
@@ -36,13 +45,28 @@ router.put('/:id', requireAuth, (req, res) => {
3645 global . db . run (
3746 query ,
3847 [
39- targetId , skillsJson , interestsJson , education_level , current_grade || null , work_experience_years || null , preferred_work_style || null ,
40- skillsJson , interestsJson , education_level , current_grade || null , work_experience_years || null , preferred_work_style || null
48+ targetId , skillsJson , interestsJson , normalizedEducationLevel , current_grade || null , work_experience_years || null , preferred_work_style || null ,
49+ skillsJson , interestsJson , normalizedEducationLevel , current_grade || null , work_experience_years || null , preferred_work_style || null
4150 ] ,
4251 function ( err ) {
4352 if ( err ) {
4453 return res . status ( 500 ) . json ( { success : false , error : err . message } ) ;
4554 }
55+
56+ if ( normalizedEducationLevel ) {
57+ global . db . run (
58+ 'UPDATE users SET user_type = ? WHERE id = ?' ,
59+ [ normalizedEducationLevel , targetId ] ,
60+ ( userErr ) => {
61+ if ( userErr ) {
62+ return res . status ( 500 ) . json ( { success : false , error : userErr . message } ) ;
63+ }
64+ res . json ( { success : true , data : { updated : true , user_type : normalizedEducationLevel } } ) ;
65+ }
66+ ) ;
67+ return ;
68+ }
69+
4670 res . json ( { success : true , data : { updated : true } } ) ;
4771 }
4872 ) ;
0 commit comments