|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#25576] The note an `admin_rescue` request writes on its own timeline, as a |
| 5 | + * stored code and its parameters, rendered in the READER's language. |
| 6 | + * |
| 7 | + * ## What was wrong |
| 8 | + * |
| 9 | + * When an approver slate named no one and the request opened on the |
| 10 | + * organization's administrators (#22824), `openNodeRequest` composed one |
| 11 | + * English sentence ("Routed to this organization's administrators because the |
| 12 | + * approver slate named no one: position 'X' has no holder.") and stored it as |
| 13 | + * the `comment` of a system `sys_approval_action` row. The approvals door |
| 14 | + * served that comment as stored, so a zh-CN, ja-JP or es-ES reader of the |
| 15 | + * timeline read English. |
| 16 | + * |
| 17 | + * ## The stored shape |
| 18 | + * |
| 19 | + * The request's node-config snapshot carries the note as data, under |
| 20 | + * {@link ADMIN_RESCUE_NOTE_SNAPSHOT_KEY}: the id of the timeline row it |
| 21 | + * explains, the abandoned slate and why a declared `fallback` did not help. |
| 22 | + * The snapshot, not a new column, is where a request already keeps what it |
| 23 | + * resolved at open time (`__flowLabel`, `__round`, `__resolvedFrom`), so no |
| 24 | + * schema moves. The row's `comment` stays the English rendering: the audit |
| 25 | + * text the generic data door, the action-history list views and every reader |
| 26 | + * without a locale are served, exactly as before. |
| 27 | + * |
| 28 | + * ## The render, reused |
| 29 | + * |
| 30 | + * The sentence and its clauses are catalog entries in this plugin's `messages` |
| 31 | + * face (`./translations/messages.ts`, `approvalAdminRescue*`). The English is |
| 32 | + * those `en` entries interpolated here, ⛔ never a second spelling; the |
| 33 | + * reader's language is the same entries through `II18nService.t()` at the |
| 34 | + * reader's negotiated locale (`ReaderLabels.sentence`, `reader-locale-labels.ts` |
| 35 | + * — the one way the approvals door picks a reader's language). A clause the |
| 36 | + * pack does not hold falls back to its English; a note sentence the pack does |
| 37 | + * not hold leaves the stored comment as it is. |
| 38 | + * |
| 39 | + * A row written before this note existed has no snapshot entry, so it is served |
| 40 | + * as stored: the English it always carried. Its clauses survive only inside that |
| 41 | + * prose, and parsing prose back into a code is the defect this module replaces, |
| 42 | + * so no backfill recovers them. |
| 43 | + */ |
| 44 | + |
| 45 | +import { approvalMessageKey, interpolate } from './approval-notification-text.js'; |
| 46 | +import { enMessages, type ApprovalMessageId } from './translations/messages.js'; |
| 47 | + |
| 48 | +/** Why a declared `onEmptyApprovers: 'fallback'` did not name anyone. */ |
| 49 | +export type AdminRescueFallback = 'not_declared' | 'resolved_to_no_one'; |
| 50 | + |
| 51 | +/** The node-config snapshot key the note rides under. */ |
| 52 | +export const ADMIN_RESCUE_NOTE_SNAPSHOT_KEY = '__adminRescueNote'; |
| 53 | + |
| 54 | +/** The note as the request's snapshot stores it. */ |
| 55 | +export interface AdminRescueNote { |
| 56 | + /** The `sys_approval_action` row (an `action: 'comment'` with no actor) this note is. */ |
| 57 | + readonly action_id: string; |
| 58 | + /** The slate that named no one, as resolved: `type:value` slots and anything else it held. */ |
| 59 | + readonly abandoned: readonly string[]; |
| 60 | + /** Set when the node declared `onEmptyApprovers: 'fallback'` and it did not help. */ |
| 61 | + readonly fallback?: AdminRescueFallback; |
| 62 | +} |
| 63 | + |
| 64 | +/** One catalog sentence and its params. */ |
| 65 | +interface Clause { |
| 66 | + readonly id: ApprovalMessageId; |
| 67 | + readonly params: Readonly<Record<string, string>>; |
| 68 | +} |
| 69 | + |
| 70 | +/** Renders a catalog sentence in some language, or answers `undefined` when it cannot. */ |
| 71 | +export type ApprovalSentenceRender = ( |
| 72 | + key: string, |
| 73 | + params: Readonly<Record<string, string>>, |
| 74 | +) => string | undefined; |
| 75 | + |
| 76 | +/** The reason's clauses: one per abandoned `type:value` slot, then the fallback's. */ |
| 77 | +function reasonClauses(abandoned: readonly string[], fallback: AdminRescueFallback | undefined): Clause[] { |
| 78 | + const clauses: Clause[] = abandoned |
| 79 | + .filter((slot) => !!slot && slot.includes(':')) |
| 80 | + .map((slot): Clause => { |
| 81 | + const at = slot.indexOf(':'); |
| 82 | + const type = slot.slice(0, at); |
| 83 | + const value = slot.slice(at + 1); |
| 84 | + return type === 'position' |
| 85 | + ? { id: 'approvalAdminRescuePositionUnheld', params: { value } } |
| 86 | + : { id: 'approvalAdminRescueSlotUnresolved', params: { type, value } }; |
| 87 | + }); |
| 88 | + if (!clauses.length) clauses.push({ id: 'approvalAdminRescueNoApprover', params: {} }); |
| 89 | + if (fallback === 'resolved_to_no_one') clauses.push({ id: 'approvalAdminRescueFallbackUnresolved', params: {} }); |
| 90 | + if (fallback === 'not_declared') clauses.push({ id: 'approvalAdminRescueFallbackUndeclared', params: {} }); |
| 91 | + return clauses; |
| 92 | +} |
| 93 | + |
| 94 | +const english = (clause: Clause): string => interpolate(enMessages[clause.id], clause.params); |
| 95 | + |
| 96 | +/** |
| 97 | + * Why the slate named no one, in English: the audit snapshot's |
| 98 | + * `admin_rescue:reason` and the log lines. |
| 99 | + */ |
| 100 | +export function adminRescueReason(abandoned: readonly string[], fallback: AdminRescueFallback | undefined): string { |
| 101 | + return reasonClauses(abandoned, fallback).map(english).join(enMessages.approvalAdminRescueReasonSeparator); |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * The note's text. With no `render`, the English the row stores; with one, |
| 106 | + * the reader's language, or `undefined` when the note sentence itself is not |
| 107 | + * rendered (the stored comment then stands). |
| 108 | + */ |
| 109 | +export function adminRescueNoteText(note: Omit<AdminRescueNote, 'action_id'>): string; |
| 110 | +export function adminRescueNoteText( |
| 111 | + note: Omit<AdminRescueNote, 'action_id'>, |
| 112 | + render: ApprovalSentenceRender, |
| 113 | +): string | undefined; |
| 114 | +export function adminRescueNoteText( |
| 115 | + note: Omit<AdminRescueNote, 'action_id'>, |
| 116 | + render?: ApprovalSentenceRender, |
| 117 | +): string | undefined { |
| 118 | + const clauses = reasonClauses(note.abandoned, note.fallback); |
| 119 | + if (!render) { |
| 120 | + const reason = clauses.map(english).join(enMessages.approvalAdminRescueReasonSeparator); |
| 121 | + return interpolate(enMessages.approvalAdminRescueNote, { reason }); |
| 122 | + } |
| 123 | + const separator = render(approvalMessageKey('approvalAdminRescueReasonSeparator'), {}) |
| 124 | + ?? enMessages.approvalAdminRescueReasonSeparator; |
| 125 | + const reason = clauses |
| 126 | + .map((clause) => render(approvalMessageKey(clause.id), clause.params) ?? english(clause)) |
| 127 | + .join(separator); |
| 128 | + return render(approvalMessageKey('approvalAdminRescueNote'), { reason }); |
| 129 | +} |
| 130 | + |
| 131 | +/** |
| 132 | + * The note a parsed node-config snapshot carries, or `undefined` when it |
| 133 | + * carries none or one this module did not write (the stored comment stands). |
| 134 | + */ |
| 135 | +export function adminRescueNoteOf(snapshot: unknown): AdminRescueNote | undefined { |
| 136 | + if (!snapshot || typeof snapshot !== 'object') return undefined; |
| 137 | + const note = (snapshot as Record<string, unknown>)[ADMIN_RESCUE_NOTE_SNAPSHOT_KEY]; |
| 138 | + if (!note || typeof note !== 'object') return undefined; |
| 139 | + const { action_id, abandoned, fallback } = note as Record<string, unknown>; |
| 140 | + if (typeof action_id !== 'string' || !action_id) return undefined; |
| 141 | + if (!Array.isArray(abandoned) || !abandoned.every((slot) => typeof slot === 'string')) return undefined; |
| 142 | + if (fallback !== undefined && fallback !== 'not_declared' && fallback !== 'resolved_to_no_one') return undefined; |
| 143 | + return { action_id, abandoned, ...(fallback ? { fallback } : {}) }; |
| 144 | +} |
0 commit comments