Files
bricktrack/src/lib/money.ts
T
unknownandClaude Opus 5 4c78c5672b feat(auth): better-auth mit passwort, passkeys und app-shell
Registrierung des ersten Nutzers macht ihn zum Administrator, danach
ist die Registrierung geschlossen (Einladungen folgen in Phase 2).
Login per Passwort oder Passkey inklusive Conditional UI, Verwaltung
der Passkeys unter "Konto" mit Umbenennen und Loeschen.

Rate-Limiting liegt in der Datenbank und ueberlebt Neustarts;
Session-Cookies sind httpOnly, sameSite=lax und im Produktivbetrieb
secure. Die Client-IP wird hinter dem Reverse Proxy aus
x-forwarded-for gelesen.

Dazu die mobile-first Grundstruktur: Bottom-Navigation, Header mit
Konto-Menue, Healthcheck unter /api/health und ein Dashboard mit den
vier Einstiegspunkten. Bedienelemente sind auf 44px Touch-Groesse
angehoben.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-06 22:40:54 +02:00

63 lines
2.1 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.
/**
* Money handling. Amounts live in the database as integer cents; euros only
* exist for display and for what the user types. Every conversion goes through
* this module.
*/
const currencyFormatter = new Intl.NumberFormat('de-DE', {
style: 'currency',
currency: 'EUR',
});
const decimalFormatter = new Intl.NumberFormat('de-DE', {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
});
/** "12,99 €" - the canonical display form. */
export function formatEuro(cents: number | null | undefined): string {
if (cents === null || cents === undefined) return '–';
return currencyFormatter.format(cents / 100);
}
/** "12,99" - for prefilling a number input without the currency symbol. */
export function centsToInput(cents: number | null | undefined): string {
if (cents === null || cents === undefined) return '';
return decimalFormatter.format(cents / 100);
}
/**
* Parses user input into cents. Accepts both German and English decimal
* separators as well as thousands separators, because phone keyboards differ.
* Returns null for empty input and NaN-ish garbage.
*/
export function parseEuroInput(value: string | null | undefined): number | null {
if (value === null || value === undefined) return null;
const trimmed = value.trim().replace(/[€\s]/g, '');
if (trimmed === '') return null;
// "1.234,56" -> "1234.56" ; "1,234.56" -> "1234.56" ; "12,99" -> "12.99"
const lastComma = trimmed.lastIndexOf(',');
const lastDot = trimmed.lastIndexOf('.');
let normalized: string;
if (lastComma > lastDot) {
normalized = trimmed.replace(/\./g, '').replace(',', '.');
} else if (lastDot > lastComma) {
normalized = trimmed.replace(/,/g, '');
} else {
normalized = trimmed;
}
const parsed = Number.parseFloat(normalized);
if (!Number.isFinite(parsed) || parsed < 0) return null;
return Math.round(parsed * 100);
}
/** Sum of a list of optional cent amounts, ignoring the unknown ones. */
export function sumCents(values: Array<number | null | undefined>): number {
return values.reduce<number>((total, value) => total + (value ?? 0), 0);
}