Shoraky commited on
Commit
618f1a0
·
1 Parent(s): eb5c1fd

Return action review author names

Browse files
Files changed (1) hide show
  1. src/server.ts +21 -5
src/server.ts CHANGED
@@ -297,17 +297,29 @@ app.get("/api/sessions/:sessionId/reviews", async (req, res, next) => {
297
  if (!actionIds.length) return res.json({ reviews: {} });
298
 
299
  const [{ data: annotations, error: annotationsError }, { data: notes, error: notesError }] = await Promise.all([
300
- supabaseAdmin.from("annotations").select("action_id,score").in("action_id", actionIds),
301
  supabaseAdmin.from("annotations_notes").select("action_id,note").in("action_id", actionIds),
302
  ]);
303
  if (annotationsError) throw new Error(`Unable to load action reviews: ${annotationsError.message}`);
304
  if (notesError) throw new Error(`Unable to load action notes: ${notesError.message}`);
305
 
306
- const reviews: Record<string, { coachScore: number | null; feedback: string[] }> = Object.fromEntries(
307
- actionIds.map((actionId) => [actionId, { coachScore: null, feedback: [] }]),
 
 
 
 
 
 
 
308
  );
309
  for (const annotation of annotations ?? []) {
310
- if (reviews[annotation.action_id]) reviews[annotation.action_id].coachScore = annotation.score == null ? null : Number(annotation.score);
 
 
 
 
 
311
  }
312
  for (const note of notes ?? []) reviews[note.action_id]?.feedback.push(note.note);
313
  return res.json({ reviews });
@@ -342,7 +354,11 @@ app.put("/api/sessions/:sessionId/actions/:actionId/review", async (req, res, ne
342
  p_notes: [...new Set(input.data.feedback)],
343
  });
344
  if (error) throw new Error(`Unable to save action review: ${error.message}`);
345
- return res.json({ ok: true });
 
 
 
 
346
  } catch (error) { next(error); }
347
  });
348
 
 
297
  if (!actionIds.length) return res.json({ reviews: {} });
298
 
299
  const [{ data: annotations, error: annotationsError }, { data: notes, error: notesError }] = await Promise.all([
300
+ supabaseAdmin.from("annotations").select("action_id,score,user_id").in("action_id", actionIds),
301
  supabaseAdmin.from("annotations_notes").select("action_id,note").in("action_id", actionIds),
302
  ]);
303
  if (annotationsError) throw new Error(`Unable to load action reviews: ${annotationsError.message}`);
304
  if (notesError) throw new Error(`Unable to load action notes: ${notesError.message}`);
305
 
306
+ const userIds = [...new Set((annotations ?? []).map((annotation) => annotation.user_id).filter((id): id is string => Boolean(id)))];
307
+ const { data: users, error: usersError } = userIds.length
308
+ ? await supabaseAdmin.from("users").select("user_id,fname,lname").in("user_id", userIds)
309
+ : { data: [], error: null };
310
+ if (usersError) throw new Error(`Unable to load review authors: ${usersError.message}`);
311
+ const authorNames = new Map((users ?? []).map((user) => [user.user_id, [user.fname, user.lname].filter(Boolean).join(" ").trim()]));
312
+
313
+ const reviews: Record<string, { coachScore: number | null; feedback: string[]; feedbackAuthorName: string }> = Object.fromEntries(
314
+ actionIds.map((actionId) => [actionId, { coachScore: null, feedback: [], feedbackAuthorName: "Unknown user" }]),
315
  );
316
  for (const annotation of annotations ?? []) {
317
+ if (reviews[annotation.action_id]) {
318
+ reviews[annotation.action_id].coachScore = annotation.score == null ? null : Number(annotation.score);
319
+ reviews[annotation.action_id].feedbackAuthorName = annotation.user_id
320
+ ? authorNames.get(annotation.user_id) || "Former user"
321
+ : "Former user";
322
+ }
323
  }
324
  for (const note of notes ?? []) reviews[note.action_id]?.feedback.push(note.note);
325
  return res.json({ reviews });
 
354
  p_notes: [...new Set(input.data.feedback)],
355
  });
356
  if (error) throw new Error(`Unable to save action review: ${error.message}`);
357
+ const { data: author, error: authorError } = await supabaseAdmin.from("users")
358
+ .select("fname,lname").eq("user_id", current.user.id).maybeSingle();
359
+ if (authorError) throw new Error(`Unable to load review author: ${authorError.message}`);
360
+ const authorName = [author?.fname, author?.lname].filter(Boolean).join(" ").trim() || "Unknown user";
361
+ return res.json({ ok: true, authorName });
362
  } catch (error) { next(error); }
363
  });
364