The eighteen routes of docs/website/TEAMS.md §2.11, their OpenAPI annotations,
and the staff screen that drives them.
Two rules shape the read model. Hidden means absent from every public surface --
the index, the lookup and the roster alike, and a hidden Team 404s
indistinguishably from one that does not exist, because "absent" includes not
confirming it is there. And staleness is surfaced rather than silent: every
public payload carries { configured, stale, lastSyncAt }, so a page can say how
recently the projection was confirmed instead of presenting stale data as
current.
The public roster withholds both the member key and the user id -- one is a
game-internal identifier, the other names a site account. `linked` answers the
only question a public page has without publishing which account. The module's
per-audience field projection is phase 3's; this is a conservative core one.
The §2.9 gate is enforced per REQUEST, not per route. A moderator may call all
eighteen; three of them mean something different when they do, and the server
decides from the role it re-validates on every request rather than from a token
claim. The client has no "file as request" argument to get wrong.
Found by booting the real server against the real database, and not by any test:
**the index and the by-slug lookup disagreed about what exists.** listPublic was
keyed on a registered team provider while findBySlug is not, so with no module
installed `/teams` returned an empty list while `/teams/:slug/members` served a
full roster -- the index denying a Team that direct URLs answered for in full.
The rows are core's and they outlive the module that filled them: an uninstalled
module leaves a projection that is unmaintained, not one that stopped existing,
and `configured: false` is how a client learns that. The read side no longer
takes the provider into account at all. There is now a test named for the
property.
Also verified live: the public routes answer anonymously, an unknown and a hidden
slug both 404, the player and admin tiers 401 an anonymous caller, a seeded
roster projects correctly, and the reconciler logs that it is staying idle with
no provider registered rather than failing a boot.
Process obligations, all done: #swagger.* annotations on every route, `npm run
swagger` regenerated (18 paths in the spec, no dangling $refs, and the schemas
they reference added), `npm run routes:manifest` regenerated -- additions only,
184 public routes -- and BACKEND_DESIGN.md updated across the schema section and
all three tier tables.
Admin -> Teams follows the ModulesAdmin precedent: everything that decides what a
row SAYS lives in lib/teamAdmin.js, which is plain JS with tests, and the view
renders it. That split earns itself here specifically -- the screen's job is to
make "the shard has no Teams" and "core has not been able to ask for two hours"
impossible to confuse, and those two produce the same empty table. The four
freshness states are named and tested for exactly that reason, and the last
provider error is shown verbatim rather than paraphrased.
The button labels follow the caller's role: a moderator sees "Request publish",
so the pending result is not a surprise. Hiding is offered to everyone with no
gate, matching the server.
Server 894 passed, client 206 passed, client build clean. 17 route tests, 20
client display tests.
Refs docs/website/TEAMS.md §2.11, Part 12 phase 2
Co-Authored-By: Claude <noreply@anthropic.com>
141 lines
5.3 KiB
JavaScript
141 lines
5.3 KiB
JavaScript
// What Admin → Teams SAYS, separated from how it renders (docs/website/TEAMS.md
|
||
// §2.4, §2.8, §2.9).
|
||
//
|
||
// Plain JS with tests, following lib/moduleAdmin.js. The reason it is worth
|
||
// splitting here specifically: this screen's job is to tell an operator the
|
||
// difference between "the shard has no Teams" and "core has not been able to ask
|
||
// for two hours", and those two produce almost the same page. Getting that
|
||
// wording right is logic, not markup.
|
||
|
||
/** Tones the screen uses. Names, not colours — the view maps them. */
|
||
export const TONE = { ok: 'ok', warn: 'warn', bad: 'bad', idle: 'idle' }
|
||
|
||
/**
|
||
* How to describe the projection's freshness.
|
||
*
|
||
* The four states are genuinely different and an operator needs to tell them
|
||
* apart:
|
||
*
|
||
* - no provider registered — nothing to sync, and not a fault;
|
||
* - never synced — core has an empty projection it has never confirmed, which
|
||
* must NOT read as "there are no Teams";
|
||
* - stale — the projection is real but old, and the reason is usually in
|
||
* `lastError`;
|
||
* - current.
|
||
*/
|
||
export function freshnessOf(sync = {}) {
|
||
if (!sync.configured) {
|
||
return { tone: TONE.idle, label: 'No Team provider', detail: 'No installed module supplies Teams.' }
|
||
}
|
||
if (!sync.lastSyncAt) {
|
||
return {
|
||
tone: TONE.bad,
|
||
label: 'Never synced',
|
||
detail: 'Core has never had an answer it could trust. What is shown below is not a confirmed empty shard.',
|
||
}
|
||
}
|
||
if (sync.stale) {
|
||
return {
|
||
tone: TONE.warn,
|
||
label: 'Stale',
|
||
detail: `Last confirmed ${ago(sync.lastSyncAt)}. Rosters below may be out of date.`,
|
||
}
|
||
}
|
||
return { tone: TONE.ok, label: 'Current', detail: `Last confirmed ${ago(sync.lastSyncAt)}.` }
|
||
}
|
||
|
||
/**
|
||
* A short, human age. Deliberately coarse: this exists so a sentence reads
|
||
* "confirmed 14 minutes ago", and second-level precision would be false comfort
|
||
* about a projection whose interval is fifteen minutes.
|
||
*/
|
||
export function ago(value) {
|
||
if (!value) return 'never'
|
||
const seconds = Math.max(0, Math.round((Date.now() - new Date(value).getTime()) / 1000))
|
||
if (seconds < 90) return 'just now'
|
||
const minutes = Math.round(seconds / 60)
|
||
if (minutes < 60) return `${minutes} minutes ago`
|
||
const hours = Math.round(minutes / 60)
|
||
if (hours < 48) return `${hours} hour${hours === 1 ? '' : 's'} ago`
|
||
return `${Math.round(hours / 24)} days ago`
|
||
}
|
||
|
||
/** The status pill for one Team row. */
|
||
export function statusOf(team = {}) {
|
||
if (team.status === 'archived') {
|
||
return { tone: TONE.idle, label: team.archivedReason === 'renamed' ? 'Renamed' : 'Archived' }
|
||
}
|
||
if (team.hidden && team.hiddenReason === 'reserved_name') {
|
||
return { tone: TONE.bad, label: 'Hidden — reserved name' }
|
||
}
|
||
if (team.hidden) return { tone: TONE.warn, label: 'Hidden by staff' }
|
||
return { tone: TONE.ok, label: 'Public' }
|
||
}
|
||
|
||
/**
|
||
* What a staff member is told will happen when they press the button.
|
||
*
|
||
* The gate is decided server-side from the caller's live role, so this only
|
||
* describes it. Saying "Request" to a moderator and "Apply" to an admin is what
|
||
* stops the pending result being a surprise.
|
||
*/
|
||
export function gateLabelFor(role, verb) {
|
||
return role === 'admin' ? verb : `Request ${verb.toLowerCase()}`
|
||
}
|
||
|
||
/** The three gated actions, for the note under the buttons. */
|
||
export const GATED_NOTE =
|
||
'Publishing a game-written name needs an admin: a moderator’s un-hide or display-name change '
|
||
+ 'is filed for approval. Hiding is not gated — suppression is always safe.'
|
||
|
||
/** A one-line description of a queued request, for the approval queue. */
|
||
export function describeRequest(request = {}) {
|
||
const payload = parsePayload(request.payload)
|
||
const who = request.requested_username || 'a deleted user'
|
||
switch (request.action) {
|
||
case 'unhide':
|
||
return `${who} asks to publish “${request.team_name}”`
|
||
case 'display_name_override':
|
||
return `${who} asks to display “${request.team_name}” as “${payload.displayName || ''}”`
|
||
case 'clear_display_name_override':
|
||
return `${who} asks to clear the display name on “${request.team_name}”`
|
||
default:
|
||
return `${who} asks for “${request.action}” on “${request.team_name}”`
|
||
}
|
||
}
|
||
|
||
/**
|
||
* The payload may arrive parsed or as a JSON string depending on the driver, so
|
||
* this normalises rather than assuming either. The server has the same note.
|
||
*/
|
||
export function parsePayload(payload) {
|
||
if (payload == null) return {}
|
||
if (typeof payload === 'object') return payload
|
||
try {
|
||
return JSON.parse(payload)
|
||
} catch {
|
||
return {}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* How a member's leadership should read.
|
||
*
|
||
* An override is shown AS an override rather than folded into the answer: staff
|
||
* looking at a roster need to see that a decision was made, not a fact that looks
|
||
* like the game's.
|
||
*/
|
||
export function leadershipOf(member = {}) {
|
||
if (!member.leaderOverride) {
|
||
return { isLeader: Boolean(member.isLeader), overridden: false, note: null }
|
||
}
|
||
const granted = member.leaderOverride.effect === 'grant'
|
||
return {
|
||
isLeader: granted,
|
||
overridden: true,
|
||
note: `${granted ? 'Granted' : 'Denied'} by ${member.leaderOverride.by || 'a deleted user'}`
|
||
+ `${member.leaderOverride.reason ? ` — ${member.leaderOverride.reason}` : ''}`
|
||
+ ` (the game says ${member.isLeaderSynced ? 'leader' : 'not a leader'})`,
|
||
}
|
||
}
|