From 48ebfcf0c089d4b5973594e686249b8eb3e17ad7 Mon Sep 17 00:00:00 2001 From: ghzhost Date: Fri, 21 Aug 2026 20:06:34 +0000 Subject: [PATCH] fix(backend): validate apportionBasisPoints sum, pin node engines and fix dockerignore (#169, #173, #176) - Validate that apportionBasisPoints percentage input sums to 100 within tolerance, failing fast with BadRequestException and O(1) error handling (#169) - Pin Node.js >=24 in package.json engines field matching CI and Dockerfile (#173) - Remove tsconfig/nest-cli files from .dockerignore so docker runner/dev builds find TS configs (#176) --- .dockerignore | 3 --- package.json | 5 ++++- src/escrow/split-math.util.spec.ts | 7 +++++++ src/escrow/split-math.util.ts | 7 +++++++ 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/.dockerignore b/.dockerignore index 1e0aac9..d7d6c9c 100644 --- a/.dockerignore +++ b/.dockerignore @@ -32,9 +32,6 @@ Thumbs.db # Documentation and configs not needed in production container README.md -nest-cli.json -tsconfig.json -tsconfig.build.json eslint.config.mjs .prettierrc test/ diff --git a/package.json b/package.json index e65c3bc..e2414dd 100644 --- a/package.json +++ b/package.json @@ -107,5 +107,8 @@ ], "coverageDirectory": "../coverage", "testEnvironment": "node" + }, + "engines": { + "node": ">=24" } -} +} \ No newline at end of file diff --git a/src/escrow/split-math.util.spec.ts b/src/escrow/split-math.util.spec.ts index aa1da7f..fb7e946 100644 --- a/src/escrow/split-math.util.spec.ts +++ b/src/escrow/split-math.util.spec.ts @@ -28,6 +28,13 @@ describe('apportionBasisPoints', () => { it('rejects an empty percentage list', () => { expect(() => apportionBasisPoints([])).toThrow(BadRequestException); }); + + it('rejects percentages that do not sum to 100', () => { + expect(() => apportionBasisPoints([20, 30])).toThrow(BadRequestException); + expect(() => apportionBasisPoints([50, 50, 10])).toThrow( + BadRequestException, + ); + }); }); describe('splitStroops', () => { diff --git a/src/escrow/split-math.util.ts b/src/escrow/split-math.util.ts index c107f94..e11b908 100644 --- a/src/escrow/split-math.util.ts +++ b/src/escrow/split-math.util.ts @@ -19,6 +19,13 @@ export function apportionBasisPoints(percentages: number[]): number[] { throw new BadRequestException('At least one percentage is required'); } + const total = percentages.reduce((sum, p) => sum + p, 0); + if (Math.abs(total - 100) > 0.01) { + throw new BadRequestException( + `Percentages must sum to 100, got ${total.toFixed(2)}`, + ); + } + const bps = percentages.map((p) => Math.round(p * 100)); const delta = TOTAL_BASIS_POINTS - bps.reduce((sum, b) => sum + b, 0);