@@ -148,42 +148,45 @@ public void uploadResume(UUID sessionId, String userEmail, MultipartFile file) {
148148
149149
150150 private UUID resolveOrCreateResumeId (String userEmail ) {
151- List <UUID > existing = jdbcTemplate .query (
152- "SELECT id FROM user_resumes WHERE user_email = ? ORDER BY updated_at DESC NULLS LAST LIMIT 1" ,
153- (rs , rowNum ) -> (UUID ) rs .getObject ("id" ),
154- userEmail
151+ List <Map <String , Object >> cols = jdbcTemplate .queryForList (
152+ "SELECT column_name, is_nullable, data_type, column_default FROM information_schema.columns WHERE table_schema='public' AND table_name='user_resumes'"
155153 );
156154
157- if (!existing .isEmpty () && existing .get (0 ) != null ) {
158- return existing .get (0 );
155+ UUID existingId = findAnyExistingResumeId (cols );
156+ if (existingId != null ) {
157+ return existingId ;
159158 }
160159
161160 UUID newResumeId = UUID .randomUUID ();
162161 LocalDateTime now = LocalDateTime .now ();
162+ return insertDynamicMockResume (userEmail , newResumeId , now , cols );
163+ }
163164
165+ private UUID findAnyExistingResumeId (List <Map <String , Object >> cols ) {
164166 try {
165- jdbcTemplate .update (
166- "INSERT INTO user_resumes (id, user_email, created_at, updated_at, extracted_text) VALUES (?, ?, ?, ?, ?)" ,
167- newResumeId , userEmail , now , now , "Local mock resume for interview prep"
168- );
169- return newResumeId ;
167+ boolean hasUpdatedAt = cols .stream ().anyMatch (c -> "updated_at" .equalsIgnoreCase (String .valueOf (c .get ("column_name" ))));
168+ String sql = hasUpdatedAt
169+ ? "SELECT id FROM user_resumes ORDER BY updated_at DESC LIMIT 1"
170+ : "SELECT id FROM user_resumes LIMIT 1" ;
171+
172+ List <UUID > existing = jdbcTemplate .query (sql , (rs , rowNum ) -> (UUID ) rs .getObject ("id" ));
173+ return existing .isEmpty () ? null : existing .get (0 );
170174 } catch (Exception ex ) {
171- log .warn ("Could not auto-create mock user resume for {}. Falling back to dynamic insert. Reason: {}" , userEmail , ex .getMessage ());
172- return insertDynamicMockResume ( userEmail , newResumeId , now ) ;
175+ log .warn ("Could not query existing user_resumes row. Will attempt mock insert. Reason: {}" , ex .getMessage ());
176+ return null ;
173177 }
174178 }
175179
176- private UUID insertDynamicMockResume (String userEmail , UUID resumeId , LocalDateTime now ) {
177- List <Map <String , Object >> cols = jdbcTemplate .queryForList (
178- "SELECT column_name, is_nullable, data_type, column_default FROM information_schema.columns WHERE table_schema='public' AND table_name='user_resumes'"
179- );
180-
180+ private UUID insertDynamicMockResume (String userEmail , UUID resumeId , LocalDateTime now , List <Map <String , Object >> cols ) {
181181 Map <String , Object > values = new LinkedHashMap <>();
182- values .put ("id" , resumeId );
183- values .put ("user_email" , userEmail );
184- values .put ("created_at" , now );
185- values .put ("updated_at" , now );
186- values .put ("extracted_text" , "Local mock resume for interview prep" );
182+ Set <String > colNames = cols .stream ().map (c -> String .valueOf (c .get ("column_name" ))).collect (java .util .stream .Collectors .toSet ());
183+
184+ if (colNames .contains ("id" )) values .put ("id" , resumeId );
185+ if (colNames .contains ("user_email" )) values .put ("user_email" , userEmail );
186+ if (colNames .contains ("email" )) values .put ("email" , userEmail );
187+ if (colNames .contains ("created_at" )) values .put ("created_at" , now );
188+ if (colNames .contains ("updated_at" )) values .put ("updated_at" , now );
189+ if (colNames .contains ("extracted_text" )) values .put ("extracted_text" , "Local mock resume for interview prep" );
187190
188191 for (Map <String , Object > col : cols ) {
189192 String name = String .valueOf (col .get ("column_name" ));
@@ -198,6 +201,10 @@ private UUID insertDynamicMockResume(String userEmail, UUID resumeId, LocalDateT
198201 values .put (name , defaultForType (dataType , now ));
199202 }
200203
204+ if (values .isEmpty ()) {
205+ throw new IllegalStateException ("user_resumes schema has no writable columns" );
206+ }
207+
201208 String columnsSql = String .join (", " , values .keySet ());
202209 String placeholders = String .join (", " , Collections .nCopies (values .size (), "?" ));
203210 String sql = "INSERT INTO user_resumes (" + columnsSql + ") VALUES (" + placeholders + ")" ;
0 commit comments