Admin guide
Version 7.1.1 BETA · for people who deploy, configure, or maintain this tool. End-user help is on the Help page.
Architecture
The shape of the thing
- Frontend: SvelteKit (Svelte 5), built as a fully static SPA (
@sveltejs/adapter-static,fallback: 404.html,prerender = true,appDir: 'app'). No SvelteKit server code. - Backend: Azure Functions v4 (plain JS ESM) in
api/, backed by Azure Cosmos DB. - Two deployment targets: GitHub Pages (static only, no API) and Azure
Static Web Apps (static +
api/). The wizard probesGET /api/isotopesat runtime and only shows catalog features when the API answers. - Physics:
src/lib/NAAMath/, layered by what inputs each step needs, each module exposinggetAll();everythingMath.tscombines them. - The wizard:
src/routes/+page.svelteis the whole UI. Auth state issrc/lib/utils/swaAuth.svelte.ts; catalog reads/writes aresrc/lib/utils/catalogWrite.ts; the autosaved draft issrc/lib/utils/analysisDraft.ts.
CLAUDE.md in the repo is the condensed architecture reference and is kept current
with structural changes.
Local development
Running it on your machine
Node 20.19+, 22.13+, or 24+ is required (the build toolchain — Vite 8,
ESLint 10 — needs it). The repo has a .nvmrc pinned to Node 22, so nvm use picks the right version. Older Node fails the build with an Unsupported engine / syntax error before it starts.
Frontend (repo root):
npm install
npm run dev # dev server
npm run build # production build (output: build/)
npm run check # svelte-check — this is the gate to keep green
npm run lint # prettier + eslint (not CI-gated; has pre-existing drift)
npm run test:unit -- --run # vitest, single pass API (separate package):
cd api
npm install
npm test # node --test
npm start # Azure Functions Core Tools (func start) For the full local combo (SPA + functions + emulated auth) use the Azure Static Web Apps CLI (swa start) pointing at the built
app and api/.
No database needed: set MOCK_COSMOS=true (env var or api/local.settings.json, sample in api/local.settings.sample.json)
and the API serves in-memory mock data and logs writes instead of calling Cosmos.
Data model (Cosmos DB)
Containers and document shapes
One database (default NAA-db), four containers. Names come from env vars with
these defaults:
COSMOSDB_CONTAINER→isotopesCOSMOSDB_REFERENCE_CONTAINER→reference-materialsCOSMOSDB_DATASHEET_CONTAINER→reference-datasheetsCOSMOSDB_ISOTOPE_MEASUREMENTS_CONTAINER→isotope-measurements
Document identity / shape:
- isotope — identity is
(shortName, massNumber, suffix)(soAg-110mis distinct fromAg-110); carriesenergies[],halfLife/halfLifeUnit/halfLifeSeconds. - reference-material —
referenceKeyis a SHA-256 fingerprint of all the counting metadata + isotope set (recomputed server-side; the client'sreferenceKeyis ignored). Containsisotopes[]andcountings[]; each counting has its owncountingIdand areferenceMaterialwithcounts[],knownConcentration[], etc. - reference-datasheet —
sampleNameandentries[](label,concentration,uncertainty,unit). Referenced byreferenceDatasheetIdon each counting. - isotope-measurement-link —
measuredIsotope.isotopeId+targetIsotope.isotopeId(catalog ids) +notes. Upserts by the pair.
Normalization / merge logic lives in api/src/lib/*WritePayload.js (unit-tested, separate
from the handlers). All Cosmos queries are parameterised — no string interpolation of user input.
API endpoints & access control
Endpoints
GET/POST /api/isotopesGET/POST /api/reference-materialsGET/POST /api/reference-datasheetsGET/POST /api/isotope-measurements
Every GET is public — it's all shared catalog data. Only the POSTs need the writer role, and all four are guarded twice: a POST-only route rule in staticwebapp.config.json plus an
in-function x-ms-client-principal check on the POST path. Only GET and POST are registered — other methods 404.
POST payload shapes are documented in README.md. Update-in-place
flags: POST /api/isotopes with "mode":"replace" overwrites
element/half-life/energy list; POST /api/reference-materials with "mode":"replace-counting" + targetItemId/targetCountingId swaps one counting on a document by id.
The two-layer write guard (keep both in sync)
The functions stay at authLevel: 'anonymous' — Azure SWA is the authenticating layer.
Writes are protected twice:
staticwebapp.config.jsonhas aPOST-only route rule for all four endpoints restricting them to theisotope_writerrole (withresponseOverridesfor 401 → login and 403 → SPA).GETmatches no rule.- Each handler independently re-validates the SWA-forwarded
x-ms-client-principalheader viaapi/src/lib/staticWebAppsAuth.js(canWriteIsotopes) — on thePOSTpath only, soGETstays public.
Why both: the in-function check is the backstop against a config regression
silently opening write access. Only GET and POST are registered on any
function; other methods 404. Auth is required only to write.
The role name is isotope_writer unless overridden by ISOTOPE_WRITE_ROLE.
Deploying
The two GitHub Actions workflows
- GitHub Pages —
.github/workflows/deploy.yml, on push tomain. Runsnpm run build, publishesbuild/. Static only, no API. If hosting under a repo sub-path, setpaths.baseinsvelte.config.js. - Azure Static Web Apps —
.github/workflows/azure-static-web-apps-*.yml. UsesAzure/static-web-apps-deploy@v1with the deployment token in repo secretAZURE_STATIC_WEB_APPS_API_TOKEN_*; builds and deploys both the SPA (output_location: "build") andapi/.
Both fire on main. A PR against main gets an SWA preview environment;
it's torn down when the PR closes.
Preview environments do not inherit production app settings. A fresh preview
env has no COSMOSDB_* values, so the API can't reach the database and falls back
to a handful of built-in sample isotopes / reference materials (the app shows an amber
"sample data" banner). Add the settings to that environment's Configuration, or just test on
production. The real catalog is never touched by this.
Azure Portal tasks
Where things live
- Static Web App resource — hosts the SPA and the managed Functions. This is where you set app settings, manage roles, see the deployment token, and add custom domains.
- Cosmos DB account — a separate resource. Data Explorer here to inspect or fix documents.
- Application Insights (if linked) — function logs and failures.
Set or change environment variables
Static Web App → Settings → Environment variables (a.k.a. Configuration →
Application settings). These are read by the Functions as process.env.*:
COSMOSDB_ENDPOINT,COSMOSDB_KEY,COSMOSDB_DATABASE— required.COSMOSDB_CONTAINERand the*_REFERENCE_CONTAINER/*_DATASHEET_CONTAINER/*_ISOTOPE_MEASUREMENTS_CONTAINER— optional, defaults above.MOCK_COSMOS— leave unset /falsein production.ISOTOPE_WRITE_ROLE— optional override ofisotope_writer.
Saving app settings restarts the Functions. Production and each preview environment have their own set.
Grant someone catalog write access
- Static Web App → Role management → Invite.
- Pick the identity provider (Azure AD), enter their email, and add the role
isotope_writer. - Send them the generated invitation link (one-time, expires). They open it, sign in, and the role is bound to their account.
To revoke: Role management → the user → remove. This is separate from Azure RBAC on the resource — it only controls what they can do through the app.
See logs / diagnose a failing function
- Static Web App → Functions → the function → Invocations / Logs, or the linked Application Insights → Failures / Logs (KQL).
- Handlers log with
context.log/context.error; write failures log"Failed to write … to Cosmos DB."with the error. - A 500 on a write is almost always Cosmos config (endpoint/key/database/container) or a Cosmos-side permission / firewall issue.
Managing access to Azure
Service account vs. individual accounts
Don't share the service account's username / password. One shared credential means no audit trail (everything looks like "the service account"), you can't revoke one person without rotating for everyone, and it fights MFA and most university policies.
Instead, add each admin's own account with a scoped Azure RBAC role, on the specific resources — not the whole subscription:
- Manage the app / Functions / config / deployments → Contributor on the Static Web App (or the resource group).
- Manage the Cosmos account (not read data) → Cosmos DB Operator.
- Read / write catalog documents directly → Cosmos DB Built-in Data Contributor (a data-plane role).
- Look only → Reader on the resource group.
Assigning roles needs Owner or User Access Administrator on the resource / resource group. If the service account doesn't have that, UT IT or the subscription owner does the assignments.
The app's isotope_writer role is separate from Azure RBAC.
Being an Azure admin does not grant it — assign it explicitly in Role management to anyone
who should also contribute catalog data through the UI.
Keep the service account for automation only, and tighten it: enable MFA, and prefer GitHub Actions OIDC / federated credentials (a service principal with no stored password) over an account password for deployments. The SWA deployment token is independent of the service account and can be rotated on its own.
Two things to check with UT's cloud team: whether the other admins' accounts are in the same Entra tenant (direct assignment) or need guest invites, and whether their policy lets you self-manage role assignments on the resource group.
Common admin tasks
Rotate the SWA deployment token
Static Web App → Overview → Manage deployment token → Reset. Then update
the repo secret AZURE_STATIC_WEB_APPS_API_TOKEN_* (repo → Settings → Secrets and
variables → Actions).
Inspect or fix a bad catalog document
- Cosmos DB account → Data Explorer → the container → query or edit the JSON directly.
- To find a reference material, search its
countings[].referenceMaterial.NETL_codeorsampleName; the top-levelreferenceKeyis an opaque hash. - To find an isotope, match
shortName+massNumber+suffix. - Deleting a datasheet that a counting still references will break loading that reference material.
Roll back a deployment
Revert the offending commit on main and let the workflows redeploy, or (SWA)
re-run an older successful "Build And Deploy" job from the Actions tab. GitHub Pages
similarly redeploys from whatever main points at.
The deployed app shows no catalog search
- Confirm you're on the Azure SWA URL, not the GitHub Pages one (Pages has no API).
- Hit
/api/isotopesdirectly — a 200 with JSON means the API is up. - If it 500s: check
COSMOSDB_*app settings and the function logs. - Check the Cosmos account's networking/firewall allows the SWA.
Gotchas
Things that have bitten people
- Isotope identity is
(shortName, massNumber, suffix).Ag-110m≠Ag-110. The metastable "m" is part of the key. - Reference-material identity is a hash of all metadata. Editing any field makes a new catalog entry unless you use the wizard's "update existing" (which targets the document by id).
- Isotope append-mode never overwrites half-life or element name — only
"mode":"replace"does, and the wizard makes you confirm the full energy list first. - A reference material only carries counts for the isotopes it covers. The
wizard slices the per-isotope arrays to the covered set before upload so
isotopes.length === counts.lengthfor the API. npm run lintis not CI-gated and has pre-existing prettier drift. Gate onnpm run check. The client (browser) vitest project is currently broken on an env import; theserverproject is the one that matters.- Update
README.mdwhen changing thePOSTpayload shapes or Azure settings, andCLAUDE.mdfor structural changes.