Files
backend/src/modules/banking/bank-cards/domain/rub-money.ts
T
ksv741 e7f75ebda1
Backend verify / commits (pull_request) Successful in 1m28s
Backend verify / security (pull_request) Canceled after 0s
Backend verify / verify (pull_request) Canceled after 0s
refactor: replace deep imports with native node aliases (WealthLedger/backend#165)
2026-11-04 14:28:52 +03:00

40 lines
1.2 KiB
TypeScript

import type { Money } from '#src/common/money.types';
import { DECIMAL_RADIX, decimalFromString, formatDecimal } from '#src/common/decimal';
export const ZERO_KOPECKS = 0n;
const RUB_DECIMAL_PLACES = 2;
const MIN_KOPECKS = -9_223_372_036_854_775_808n;
const MAX_KOPECKS = 9_223_372_036_854_775_807n;
export function parseRubMoney(money: Money, positive = false): bigint {
if (
money?.currency !== 'RUB' ||
typeof money.value !== 'string' ||
money.value !== money.value.trim()
) {
throw new TypeError('Expected RUB money with a decimal string');
}
const amount = decimalFromString(money.value);
if (amount.scale > RUB_DECIMAL_PLACES) {
throw new RangeError('RUB money supports at most two decimal places');
}
const kopecks = amount.value * DECIMAL_RADIX ** BigInt(RUB_DECIMAL_PLACES - amount.scale);
if (kopecks < MIN_KOPECKS || kopecks > MAX_KOPECKS) {
throw new RangeError('RUB money is outside the signed 64-bit range');
}
if (positive && kopecks <= ZERO_KOPECKS) {
throw new RangeError('Operation amount must be positive');
}
return kopecks;
}
export function rubMoney(kopecks: bigint): Money {
return {
currency: 'RUB',
value: formatDecimal({ value: kopecks, scale: RUB_DECIMAL_PLACES }),
};
}