85 lines
2.9 KiB
JavaScript
85 lines
2.9 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++;
|
|
}
|
|
|
|
ok(res, {
|
|
status: 'ok',
|
|
version: require('../../package.json').version,
|
|
nodeVersion: process.version,
|
|
uptime: Math.floor(process.uptime()),
|
|
db: { status: 'ok' },
|
|
disk: { free: storage.disk.free, usedPercent: storage.disk.usedPercent },
|
|
activeTimerSessions,
|
|
orgCount,
|
|
});
|
|
} catch (err) { next(err); }
|
|
});
|
|
|
|
module.exports = router;
|