diff --git a/ui/pages/pages.json b/ui/pages/pages.json index 88b4e47d6..a62f7f6cc 100644 --- a/ui/pages/pages.json +++ b/ui/pages/pages.json @@ -72,7 +72,7 @@ { "path": "/org/approvals", "label": "Approvals", - "description": "Default owner, escalation path and approval deadline." + "description": "Default owner, escalation path, approval deadline and the approvals that earn an autonomy proposal." }, { "path": "/org/connections", diff --git a/ui/src/pages/org/approvals.vue b/ui/src/pages/org/approvals.vue index e0f06c688..31312329b 100644 --- a/ui/src/pages/org/approvals.vue +++ b/ui/src/pages/org/approvals.vue @@ -5,7 +5,7 @@ definePage({ section: "org_config", label: "Approvals", navPath: "/org", - description: "Default owner, escalation path and approval deadline.", + description: "Default owner, escalation path, approval deadline and the approvals that earn an autonomy proposal.", }, }); @@ -17,11 +17,13 @@ import PageHeader from "@/base/PageHeader.vue"; import { Alert, AlertDescription } from "@/components/ui/alert"; import { Button } from "@/components/ui/button"; import { DurationInput } from "@/components/ui/duration-input"; +import { NumberField, NumberFieldContent, NumberFieldDecrement, NumberFieldIncrement, NumberFieldInput } from "@/components/ui/number-field"; import { ReadError } from "@/components/ui/read-error"; import { Section } from "@/components/ui/section"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { SettingRow } from "@/components/ui/setting-row"; import { errors } from "@/copy/errors"; +import { AUTONOMY_RANGE, autonomyCount, toDraft, toPayload, type ApprovalsDraft } from "./approvals/lib/settings"; import { api, describeError, type OrgSettings } from "@/lib/api"; import { useOrgStore } from "@/stores/org"; import type { Member } from "@/org/org"; @@ -31,18 +33,12 @@ const NONE = "none"; // Default deadline for a new step. The server accepts one minute to 30 days. const NEW_STEP_DEADLINE = "4h"; -interface Draft { - approval_deadline: string; - default_owner: string; - escalation: { account: string; deadline: string }[]; -} - const orgStore = useOrgStore(); const { org } = storeToRefs(orgStore); const saved = ref(null); const people = ref([]); -const draft = ref(null); +const draft = ref(null); const loadError = ref(""); const saveError = ref(""); const busy = ref(false); @@ -51,14 +47,6 @@ const busy = ref(false); const eligible = computed(() => people.value.filter((member) => member.kind === "user" && !member.workspace_id)); const nameOf = (id: string) => eligible.value.find((member) => member.identity_id === id)?.display_name ?? id; -function toDraft(settings: OrgSettings): Draft { - return { - approval_deadline: settings.approval_deadline, - default_owner: settings.default_owner, - escalation: settings.escalation.map((step) => ({ ...step })), - }; -} - const dirty = computed(() => saved.value !== null && JSON.stringify(draft.value) !== JSON.stringify(toDraft(saved.value))); async function load(): Promise { @@ -84,6 +72,12 @@ function choices(account: string) { const canAdd = computed(() => draft.value !== null && choices("").length > 0); +// The control clamps what it can, and this keeps the draft inside the range the +// server accepts whatever the control allows. +function setAutonomy(value: number | null | undefined): void { + if (draft.value) draft.value.propose_autonomy_after = autonomyCount(value ?? AUTONOMY_RANGE.min); +} + function addStep(): void { const next = choices("")[0]; if (draft.value && next) draft.value.escalation.push({ account: next.identity_id, deadline: NEW_STEP_DEADLINE }); @@ -94,7 +88,7 @@ async function save(): Promise { busy.value = true; saveError.value = ""; try { - const updated = await api.putOrgSettings({ ...saved.value, ...draft.value }); + const updated = await api.putOrgSettings(toPayload(saved.value, draft.value)); saved.value = updated; draft.value = toDraft(updated); } catch (cause) { @@ -132,6 +126,22 @@ onMounted(load); + + + + + + + + +
diff --git a/ui/src/pages/org/approvals/lib/settings.ts b/ui/src/pages/org/approvals/lib/settings.ts new file mode 100644 index 000000000..9509840b5 --- /dev/null +++ b/ui/src/pages/org/approvals/lib/settings.ts @@ -0,0 +1,48 @@ +/** + * The approvals settings form: what the page edits, and what it sends back. + * + * The server replaces all of the organisation settings on save, so a page that + * edits three of them sends the rest as it read them. Held apart from the page + * so the shape of a save can be tested without a browser. + */ +import type { OrgSettings } from "@/lib/api"; + +/** What the approvals page edits. Everything else travels back untouched. */ +export interface ApprovalsDraft { + approval_deadline: string; + propose_autonomy_after: number; + default_owner: string; + escalation: { account: string; deadline: string }[]; +} + +/** + * The range the server accepts for propose_autonomy_after. One is the fewest + * approvals that can be a row, and past a hundred no action type could ever + * earn a proposal. + */ +export const AUTONOMY_RANGE = { min: 1, max: 100 } as const; + +/** autonomyCount keeps a typed count inside the range the server accepts. */ +export function autonomyCount(value: number): number { + if (!Number.isFinite(value)) return AUTONOMY_RANGE.min; + return Math.min(AUTONOMY_RANGE.max, Math.max(AUTONOMY_RANGE.min, Math.round(value))); +} + +/** toDraft reads the settings into the fields the page edits. */ +export function toDraft(settings: OrgSettings): ApprovalsDraft { + return { + approval_deadline: settings.approval_deadline, + propose_autonomy_after: autonomyCount(Number(settings.propose_autonomy_after ?? AUTONOMY_RANGE.min)), + default_owner: settings.default_owner, + escalation: settings.escalation.map((step) => ({ ...step })), + }; +} + +/** toPayload is the save: the edited fields over the settings as they were read. */ +export function toPayload(saved: OrgSettings, draft: ApprovalsDraft): OrgSettings { + return { + ...saved, + ...draft, + propose_autonomy_after: autonomyCount(draft.propose_autonomy_after), + }; +} diff --git a/ui/test/approvals-settings.test.ts b/ui/test/approvals-settings.test.ts new file mode 100644 index 000000000..8b460259f --- /dev/null +++ b/ui/test/approvals-settings.test.ts @@ -0,0 +1,48 @@ +import assert from "node:assert/strict"; +import test from "node:test"; + +import { + autonomyCount, + toDraft, + toPayload, +} from "../src/pages/org/approvals/lib/settings.ts"; + +// The organisation settings as the server returns them. The page edits some +// fields and sends the rest back as it read them. +const saved = { + approval_deadline: "24h", + default_owner: "someone", + escalation: [{ account: "someone", deadline: "4h" }], + propose_autonomy_after: 3, + frozen: false, +}; + +// Guards #2124: how many approvals earn an autonomy proposal is part of the +// organisation settings but had no control on the approvals page, so an +// operator could read it only through the API and could not change it. +test("the approvals draft carries the count that earns a proposal", () => { + assert.equal(toDraft(saved).propose_autonomy_after, 3); + assert.equal(toDraft({ ...saved, propose_autonomy_after: 12 }).propose_autonomy_after, 12); +}); + +test("saving sends the edited count with the fields the page does not edit", () => { + const draft = { ...toDraft(saved), propose_autonomy_after: 7 }; + const body = toPayload(saved, draft); + assert.equal(body.propose_autonomy_after, 7); + assert.equal(body.frozen, false, "a field the page does not edit is sent back as it was read"); + assert.equal(body.approval_deadline, "24h"); +}); + +// The server accepts one to a hundred approvals; past that no action type could +// ever earn a proposal, so the field never sends a value outside the range. +test("a count outside the server's range becomes the nearest bound", () => { + for (const [typed, want] of [ + [0, 1], + [-4, 1], + [1, 1], + [100, 100], + [101, 100], + ] as const) { + assert.equal(autonomyCount(typed), want, String(typed)); + } +});