Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions pm-stack/aggregator/src/server-decision-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
// routes do not match. Split out of server.ts (#13052).
import { decodePathSegment } from "./path-segment.ts";
import {
getNeedsDrew,
listOpenNeedsDrew,
getNeedsUser,
listOpenNeedsUser,
needsDrewIncidentKey,
needsUserIncidentKey,
recordAnswer,
type NeedsDrewQuestion,
type NeedsUserQuestion,
} from "./needs-drew.ts";
import { validateNeedsDrewChoice } from "./needs-drew-answer.ts";
import { validateNeedsUserChoice } from "./needs-drew-answer.ts";
import {
decisionSetAging,
decisionSetOpenState,
Expand Down Expand Up @@ -81,7 +81,7 @@ import { alertsIntakeAuthorized } from "./server-alert-routes.ts";
* key needs-user:<ledgerId> (needs-drew: before #13507 A3). #14084 S1: "Send back" returns the item
* to review; any other answer resolves it, keyed on the recorded
* (first) answer. */
export async function afterNeedsDrewAnswered(item: NeedsDrewQuestion, answered: NeedsDrewQuestion): Promise<void> {
export async function afterNeedsDrewAnswered(item: NeedsUserQuestion, answered: NeedsUserQuestion): Promise<void> {
// #14043 S3: a recorded broker-sourced answer is carried to the
// decide verb by the applier; kick it now rather than waiting for
// the periodic pass.
Expand Down Expand Up @@ -110,11 +110,17 @@ export async function afterNeedsDrewAnswered(item: NeedsDrewQuestion, answered:
}
}

/** #13507 item 4: canonical needsUser spelling of the answer
* side-effects hook, aliased by identity so consumers can migrate
* incrementally during the alias window; the needsDrew spelling
* stays until close-out. */
export const afterNeedsUserAnswered = afterNeedsDrewAnswered;

export async function handleDecisionRoutes(req: Request, url: URL, path: string): Promise<Response | null> {
// --- Needs-Drew answers (#8568) --------------------------------------
const needsDrewAnswerMatch = /^\/api\/needs-(?:drew|user)\/([^/]+)\/answer$/.exec(path);
if (req.method === "POST" && needsDrewAnswerMatch) {
const ledgerId = decodePathSegment(needsDrewAnswerMatch[1]);
const needsUserAnswerMatch = /^\/api\/needs-(?:drew|user)\/([^/]+)\/answer$/.exec(path);
if (req.method === "POST" && needsUserAnswerMatch) {
const ledgerId = decodePathSegment(needsUserAnswerMatch[1]);
if (ledgerId === null) return json({ error: "bad ledgerId" }, 400);
let body: { token?: unknown; optionId?: unknown; optionIds?: unknown; freeText?: unknown };
try {
Expand All @@ -123,11 +129,11 @@ export async function handleDecisionRoutes(req: Request, url: URL, path: string)
return json({ error: "invalid answer body" }, 400);
}
// No bearer: the per-question token is the authority.
const item = await getNeedsDrew(cfg.needsDrewFile, ledgerId).catch(() => null);
const item = await getNeedsUser(cfg.needsDrewFile, ledgerId).catch(() => null);
if (!item || typeof body.token !== "string" || body.token !== item.token) {
return json({ error: "unknown question or bad token" }, 403);
}
const choice = validateNeedsDrewChoice(item, body);
const choice = validateNeedsUserChoice(item, body);
if (!choice.ok) return json({ error: choice.error }, 400);
const updated = await recordAnswer(cfg.needsDrewFile, ledgerId, { ...choice.value, via: "api" });
if (!updated?.answer) return json({ error: "answer not recorded" }, 500);
Expand All @@ -149,7 +155,7 @@ export async function handleDecisionRoutes(req: Request, url: URL, path: string)
if (path === "/api/needs-user" || path === "/needs-user") {
if (req.method !== "GET") return json({ error: "method not allowed" }, 405);
const store = await getAlertsStore();
const questions = await listOpenNeedsDrew(cfg.needsDrewFile).catch(() => []);
const questions = await listOpenNeedsUser(cfg.needsDrewFile).catch(() => []);
let items: NeedsUserListItem[];
let stagesAvailable = true;
try {
Expand Down Expand Up @@ -180,7 +186,7 @@ export async function handleDecisionRoutes(req: Request, url: URL, path: string)
} catch {
return json({ error: "invalid body" }, 400);
}
const item = await getNeedsDrew(cfg.needsDrewFile, ledgerId).catch(() => null);
const item = await getNeedsUser(cfg.needsDrewFile, ledgerId).catch(() => null);
if (!item) return json({ error: "unknown question" }, 404);
if (item.answer) return json({ error: "question already answered" }, 409);
const store = await getAlertsStore();
Expand All @@ -202,7 +208,7 @@ export async function handleDecisionRoutes(req: Request, url: URL, path: string)
if (req.method === "GET" && answerPageMatch) {
const ledgerId = decodePathSegment(answerPageMatch[1]);
const token = url.searchParams.get("t") ?? "";
const item = ledgerId === null ? null : await getNeedsDrew(cfg.needsDrewFile, ledgerId).catch(() => null);
const item = ledgerId === null ? null : await getNeedsUser(cfg.needsDrewFile, ledgerId).catch(() => null);
// Unknown question or token mismatch: 403, and the question is
// never rendered — the token is the only authority here.
if (!item || !token || token !== item.token) return html(renderAnswerForbidden(), 403);
Expand Down
8 changes: 8 additions & 0 deletions pm-stack/aggregator/test/needs-user-aliases-13507.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { describe, expect, test } from "bun:test";
import * as drew from "../src/needs-drew.ts";
import * as answer from "../src/needs-drew-answer.ts";
import { validateNeedsUserChoice } from "../src/needs-drew-answer.ts";
import * as decisionRoutes from "../src/server-decision-routes.ts";
import { afterNeedsUserAnswered } from "../src/server-decision-routes.ts";
import {
NEEDS_USER_ANSWER_TYPES,
NEEDS_USER_LABELS,
Expand Down Expand Up @@ -90,3 +92,9 @@ describe("needs-drew-answer module: needsUser aliases", () => {
expect(validateNeedsUserChoice(item, {}).ok).toBe(false);
});
});

describe("server-decision-routes module: needsUser aliases", () => {
test("afterNeedsUserAnswered is afterNeedsDrewAnswered by identity", () => {
expect(afterNeedsUserAnswered).toBe(decisionRoutes.afterNeedsDrewAnswered);
});
});