Spaces:
Running
Running
Expose saved session and action feedback
Browse files- src/server.ts +47 -0
src/server.ts
CHANGED
|
@@ -563,6 +563,53 @@ app.get("/api/sessions/:sessionId/reviews", async (req, res, next) => {
|
|
| 563 |
} catch (error) { next(error); }
|
| 564 |
});
|
| 565 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 566 |
app.put("/api/sessions/:sessionId/actions/:actionId/review", async (req, res, next) => {
|
| 567 |
try {
|
| 568 |
const current = await requireActiveSubscription(req, res);
|
|
|
|
| 563 |
} catch (error) { next(error); }
|
| 564 |
});
|
| 565 |
|
| 566 |
+
app.get("/api/sessions/:sessionId/feedbacks", async (req, res, next) => {
|
| 567 |
+
try {
|
| 568 |
+
const current = await authenticatedUser(req, res);
|
| 569 |
+
if (!current) return res.status(401).json({ error: "Authentication required." });
|
| 570 |
+
const sessionId = z.uuid().safeParse(req.params.sessionId);
|
| 571 |
+
if (!sessionId.success) return res.sendStatus(404);
|
| 572 |
+
|
| 573 |
+
const { data: session, error: sessionError } = await supabaseAdmin.from("sessions")
|
| 574 |
+
.select("session_id,user_id").eq("session_id", sessionId.data).maybeSingle();
|
| 575 |
+
if (sessionError) throw new Error(`Unable to verify feedback session: ${sessionError.message}`);
|
| 576 |
+
if (!session) return res.sendStatus(404);
|
| 577 |
+
if (session.user_id !== current.user.id && !await activeSubscriptionFor(current.user.id, { refresh: true })) {
|
| 578 |
+
return res.status(403).json({ error: "An active subscription is required to view shared sessions.", code: "subscription_required" });
|
| 579 |
+
}
|
| 580 |
+
|
| 581 |
+
const requestedActionId = req.query.actionId === undefined ? null : z.uuid().safeParse(req.query.actionId);
|
| 582 |
+
if (requestedActionId && !requestedActionId.success) return res.sendStatus(404);
|
| 583 |
+
let actionIds: string[] = [];
|
| 584 |
+
if (requestedActionId) {
|
| 585 |
+
const { data: action, error: actionError } = await supabaseAdmin.from("actions")
|
| 586 |
+
.select("action_id").eq("session_id", sessionId.data).eq("action_id", requestedActionId.data).maybeSingle();
|
| 587 |
+
if (actionError) throw new Error(`Unable to verify feedback action: ${actionError.message}`);
|
| 588 |
+
if (!action) return res.sendStatus(404);
|
| 589 |
+
actionIds = [action.action_id];
|
| 590 |
+
} else {
|
| 591 |
+
const { data: actions, error: actionsError } = await supabaseAdmin.from("actions")
|
| 592 |
+
.select("action_id").eq("session_id", sessionId.data);
|
| 593 |
+
if (actionsError) throw new Error(`Unable to load feedback actions: ${actionsError.message}`);
|
| 594 |
+
actionIds = (actions ?? []).map((action) => action.action_id);
|
| 595 |
+
}
|
| 596 |
+
const [sessionResult, actionResult] = await Promise.all([
|
| 597 |
+
requestedActionId
|
| 598 |
+
? Promise.resolve({ data: [], error: null })
|
| 599 |
+
: supabaseAdmin.from("sessions_feedbacks").select("feedback").eq("session_id", sessionId.data).order("feedback"),
|
| 600 |
+
actionIds.length
|
| 601 |
+
? supabaseAdmin.from("actions_feedbacks").select("action_id,feedback").in("action_id", actionIds).order("feedback")
|
| 602 |
+
: Promise.resolve({ data: [], error: null }),
|
| 603 |
+
]);
|
| 604 |
+
if (sessionResult.error) throw new Error(`Unable to load session feedback: ${sessionResult.error.message}`);
|
| 605 |
+
if (actionResult.error) throw new Error(`Unable to load action feedback: ${actionResult.error.message}`);
|
| 606 |
+
|
| 607 |
+
const actionFeedback: Record<string, string[]> = Object.fromEntries(actionIds.map((actionId) => [actionId, []]));
|
| 608 |
+
for (const row of actionResult.data ?? []) actionFeedback[row.action_id]?.push(row.feedback);
|
| 609 |
+
return res.json({ sessionFeedback: (sessionResult.data ?? []).map((row) => row.feedback), actionFeedback });
|
| 610 |
+
} catch (error) { next(error); }
|
| 611 |
+
});
|
| 612 |
+
|
| 613 |
app.put("/api/sessions/:sessionId/actions/:actionId/review", async (req, res, next) => {
|
| 614 |
try {
|
| 615 |
const current = await requireActiveSubscription(req, res);
|