generated from bastian/boilerplate
szenario
This commit is contained in:
@@ -36,3 +36,9 @@ export function sumMoney(values: readonly (string | number)[]): string {
|
||||
const total = values.reduce<number>((sum, value) => sum + cents(value), 0);
|
||||
return `${Math.floor(total / 100)}.${String(total % 100).padStart(2, '0')}`;
|
||||
}
|
||||
|
||||
export function formatOptionalMoney(
|
||||
value: number | null | undefined,
|
||||
): string | null {
|
||||
return value == null ? null : value.toFixed(2);
|
||||
}
|
||||
|
||||
@@ -34,7 +34,11 @@ import {
|
||||
FurnitureScenarioStatus,
|
||||
FurnitureScenarioType,
|
||||
} from './entities/furniture.entities';
|
||||
import { calculateFurnitureTotal, sumMoney } from './furniture-pricing';
|
||||
import {
|
||||
calculateFurnitureTotal,
|
||||
formatOptionalMoney,
|
||||
sumMoney,
|
||||
} from './furniture-pricing';
|
||||
import { FurnitureRepository } from './furniture.repository';
|
||||
import { RenovationRepository } from './renovation.repository';
|
||||
|
||||
@@ -1317,8 +1321,8 @@ export class FurnitureService {
|
||||
'Dieser Datensatz wurde zwischenzeitlich geändert. Laden Sie die aktuellen Daten neu.',
|
||||
);
|
||||
}
|
||||
private decimal(value?: number) {
|
||||
return value === undefined ? null : value.toFixed(2);
|
||||
private decimal(value?: number | null) {
|
||||
return formatOptionalMoney(value);
|
||||
}
|
||||
private validation(message: string): never {
|
||||
throw new ApiError(ErrorCode.ValidationFailed, message, 400);
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { calculateFurnitureTotal, sumMoney } from '../furniture-pricing';
|
||||
import {
|
||||
calculateFurnitureTotal,
|
||||
formatOptionalMoney,
|
||||
sumMoney,
|
||||
} from '../furniture-pricing';
|
||||
|
||||
describe('furniture price calculation', () => {
|
||||
it('includes quantity, shipping and additional costs and subtracts discounts cent-exactly', () => {
|
||||
@@ -40,4 +44,10 @@ describe('furniture price calculation', () => {
|
||||
it('sums decimal money without binary floating point drift', () => {
|
||||
expect(sumMoney(['0.10', '0.20', '1299.99'])).toBe('1300.29');
|
||||
});
|
||||
|
||||
it('formats optional scenario price overrides without crashing for missing values', () => {
|
||||
expect(formatOptionalMoney(undefined)).toBeNull();
|
||||
expect(formatOptionalMoney(null)).toBeNull();
|
||||
expect(formatOptionalMoney(12.5)).toBe('12.50');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user