Files
vitfit-r2/V2/app/modules/sysadmin/routes.js
T
2026-06-16 07:17:49 -04:00

88 lines
3.1 KiB
JavaScript

'use strict';
const express = require('express');
const { models } = require('../../core/db');
const { authenticateToken, authenticateRole } = require('../../core/auth');
const { ok, collection } = require('../../core/respond');
const { AppError } = require('../../core/errors');
const { getStorageInfo } = require('../../core/storage');
const { getActiveSession } = require('../timers/service');
const router = express.Router();
router.prefix = 'sysadmin';
const SA = ['SystemAdmin'];
router.get('/orgs', authenticateToken, authenticateRole(SA), async (req, res, next) => {
try {
const orgs = await models.Organization.findAll({
include: [{ model: models.Branding, required: false }],
order: [['createdAt', 'DESC']],
});
const result = await Promise.all(orgs.map(async org => {
const counts = await models.User.findAll({
where: { organizationId: org.id },
attributes: ['role', [models.User.sequelize.fn('COUNT', models.User.sequelize.col('id')), 'count']],
group: ['role'],
raw: true,
});
return { ...org.toJSON(), userCounts: counts };
}));
ok(res, result);
} catch (err) { next(err); }
});
router.get('/orgs/:id', authenticateToken, authenticateRole(SA), async (req, res, next) => {
try {
const org = await models.Organization.findByPk(req.params.id, {
include: [{ model: models.Branding, required: false }],
});
if (!org) return next(new AppError('NOT_FOUND', 'Organization not found.', 404));
const users = await models.User.findAll({ where: { organizationId: org.id } });
ok(res, { ...org.toJSON(), users: users.map(u => u.toPublic()) });
} catch (err) { next(err); }
});
router.patch('/orgs/:id', authenticateToken, authenticateRole(SA), async (req, res, next) => {
try {
const org = await models.Organization.findByPk(req.params.id);
if (!org) return next(new AppError('NOT_FOUND', 'Organization not found.', 404));
await org.update(req.body);
ok(res, org);
} catch (err) { next(err); }
});
router.get('/health', authenticateToken, authenticateRole(SA), async (req, res, next) => {
try {
const storage = await getStorageInfo();
const orgCount = await models.Organization.count();
// Count active timer sessions across all orgs
// (getActiveSession checks per-org; do a rough count via the activeSessions map)
let activeTimerSessions = 0;
const allOrgs = await models.Organization.findAll({ attributes: ['id'] });
for (const org of allOrgs) {
if (getActiveSession(org.id)) activeTimerSessions++;
}
const mem = process.memoryUsage();
ok(res, {
status: 'ok',
version: require('../../package.json').version,
nodeVersion: process.version,
uptimeSeconds: Math.floor(process.uptime()),
timestamp: new Date().toISOString(),
memoryMb: { rss: Math.round(mem.rss / 1_048_576), heapUsed: Math.round(mem.heapUsed / 1_048_576) },
db: { status: 'ok' },
disk: { free: storage.disk.free, usedPercent: storage.disk.usedPercent },
activeTimerSessions,
orgCount,
});
} catch (err) { next(err); }
});
module.exports = router;