Files
TREK/server/src/systemNotices/service.ts
T
Maurice 724c88c201 style: format server and shared with prettier
A mechanical prettier --write over server/src, server/tests, server/scripts
and shared/src with the workspaces' .prettierrc; nothing but layout and
import order changes, and every file keeps the same set of imports. shared
is formatted throughout and its format baseline is empty. The server
baseline keeps the 35 files that Prettier would grow past their lint:size
limit (they need a split first, which is not a formatting change), and the
generated entity files stay as their generator writes them.
2026-10-10 21:23:37 +02:00

49 lines
1.7 KiB
TypeScript

import { readEnv } from '../app-config';
import type { SystemNotice } from './types.js';
import semver from 'semver';
export function getCurrentAppVersion(): string {
const fromEnv = semver.valid(readEnv().app.appVersion ?? '');
if (fromEnv) return fromEnv;
try {
const pkg = require('../../package.json') as { version?: string };
return semver.valid(pkg.version ?? '') ?? '0.0.0';
} catch {
return '0.0.0';
}
}
export function isNoticeVersionActive(n: SystemNotice, currentAppVersion: string): boolean {
const appVersion = semver.coerce(currentAppVersion)?.version ?? '0.0.0';
if (n.minVersion !== undefined) {
const min = semver.valid(n.minVersion);
if (!min) {
console.warn(`[systemNotices] "${n.id}" invalid minVersion "${n.minVersion}" — skipping`);
return false;
}
if (semver.lt(appVersion, min)) return false;
}
if (n.maxVersion !== undefined) {
const max = semver.valid(n.maxVersion);
if (!max) {
console.warn(`[systemNotices] "${n.id}" invalid maxVersion "${n.maxVersion}" — skipping`);
return false;
}
if (semver.gte(appVersion, max)) return false;
}
return true;
}
/**
* Sort weight: critical > warn > info. Pure, no DB — kept here per Plan 3f
* Task 0's R1 wiring plan ("isNoticeVersionActive, getCurrentAppVersion,
* severityWeight are pure — stay as plain exported functions"), now exported
* for `SystemNoticesService.getActiveFor` (`nest/system-notices/`), which
* absorbed the DB-touching `getActiveNoticesFor`/`dismissNotice` that used to
* live in this file.
*/
export function severityWeight(s: string): number {
return s === 'critical' ? 2 : s === 'warn' ? 1 : 0;
}