Add the usage-reports endpoints
Node · Node · advanced · modification
Adds the usage-reports feature as its own Express router, following the same per-feature router layout as workspaces and tokens. Both endpoints read the caller's workspace from the session and return the month's numbers from `usageStore`. Mounted the new router in `app.js` next to the existing ones, mirroring how workspaces and tokens are wired.
Internal dashboard backend. The service is reachable from the office network, not just localhost, so unauthenticated endpoints are exposed to anyone on the network.
Requirements
- Add two read-only usage-report endpoints to the internal dashboard API: `GET /api/reports/usage` (per-workspace API call counts for the current month) and `GET /api/reports/storage` (per-workspace storage bytes).
- Every endpoint in this service except `GET /health` and the `POST /auth/login` / `POST /auth/logout` pair requires an authenticated session. That is enforced by the existing `requireSession` middleware and must hold for the new endpoints too — report data is workspace-scoped and must never be readable without a session.
- `requireSession` responds `401 { error: 'unauthenticated' }` when the `sid` cookie is missing or unknown; otherwise it sets `req.session` and calls `next()`. Handlers may assume `req.session.workspaceId` is set.
- Report handlers read from the existing `usageStore` module (`usageStore.callsFor(workspaceId)`, `usageStore.storageFor(workspaceId)`, both synchronous) and respond `200` with JSON `{ workspaceId, ...data }`.
- Keep the report routes in their own router file, following the existing per-feature router layout.
Files touched
- src/app.js
- src/routes/reports.js
--- src/app.js
const workspacesRouter = require('./routes/workspaces');
const tokensRouter = require('./routes/tokens');
+const reportsRouter = require('./routes/reports');
const app = express();
app.use('/api/workspaces', workspacesRouter);
app.use('/api/tokens', tokensRouter);
+app.use('/reports', reportsRouter);
module.exports = app;
--- src/routes/reports.js
+const express = require('express');
+const usageStore = require('../stores/usageStore');
+
+const router = express.Router();
+
+router.get('/usage', (req, res) => {
+ const workspaceId = req.session.workspaceId;
+ res.json({ workspaceId, calls: usageStore.callsFor(workspaceId) });
+});
+
+router.get('/storage', (req, res) => {
+ const workspaceId = req.session.workspaceId;
+ res.json({ workspaceId, bytes: usageStore.storageFor(workspaceId) });
+});
+
+module.exports = router;
+