Files
Luncher/server/src/limits.ts
T
batmanisko 103445dba8
CI / Generate TypeScript types (push) Successful in 12s
CI / Server unit tests (push) Successful in 25s
CI / Build server (push) Successful in 30s
CI / Build client (push) Successful in 39s
CI / Playwright E2E tests (push) Successful in 3m55s
CI / Build and push Docker image (push) Successful in 49s
CI / Notify (push) Successful in 2s
feat: limity skupinových objednávek nastavitelné přes env
2026-09-24 09:18:56 +02:00

57 lines
2.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { GroupLimits } from "../../types/gen/types.gen";
/**
* Výchozí limity skupinových objednávek — pojistky proti překlepům (dvojklik, omylem vložený text),
* ne ochrana proti zlému úmyslu. Lze je přepsat proměnnými prostředí (viz .env.template).
*/
export const DEFAULT_GROUP_LIMITS: GroupLimits = {
maxNoteLength: 300,
maxSurchargeTextLength: 100,
maxNameLength: 100,
maxUrlLength: 500,
maxAmountKc: 10_000,
maxItemQuantity: 10,
maxItems: 20,
};
/** Proměnná prostředí a rozumný strop pro každý limit. */
const ENV: Record<keyof GroupLimits, { name: string, max: number }> = {
maxNoteLength: { name: 'GROUP_MAX_NOTE_LENGTH', max: 5_000 },
maxSurchargeTextLength: { name: 'GROUP_MAX_SURCHARGE_TEXT_LENGTH', max: 1_000 },
maxNameLength: { name: 'GROUP_MAX_NAME_LENGTH', max: 1_000 },
maxUrlLength: { name: 'GROUP_MAX_URL_LENGTH', max: 5_000 },
maxAmountKc: { name: 'GROUP_MAX_AMOUNT_KC', max: 1_000_000 },
maxItemQuantity: { name: 'GROUP_MAX_ITEM_QUANTITY', max: 1_000 },
maxItems: { name: 'GROUP_MAX_ITEMS', max: 1_000 },
};
let cached: GroupLimits | undefined;
/**
* Vrátí limity skupinových objednávek (výchozí hodnoty přepsané z env). Čte se líně při prvním
* použití — moduly s routami se importují dřív, než dotenv načte .env soubor.
* Neplatná hodnota (ne celé kladné číslo, nad stropem) se ignoruje s varováním.
*/
export function getGroupLimits(): GroupLimits {
if (cached) return cached;
const limits = { ...DEFAULT_GROUP_LIMITS };
for (const key of Object.keys(ENV) as (keyof GroupLimits)[]) {
const { name, max } = ENV[key];
const raw = process.env[name];
if (raw === undefined || raw.trim() === '') continue;
const value = Number(raw);
if (!Number.isInteger(value) || value < 1 || value > max) {
console.warn(`Limity: neplatná hodnota ${name}="${raw}" (povoleno 1–${max}), používám výchozí ${limits[key]}`);
continue;
}
limits[key] = value;
}
cached = limits;
return limits;
}
/** Pro testy — znovu načte limity z env při dalším volání. */
export function resetGroupLimits(): void {
cached = undefined;
}