85e039035d
* feat(reports): custom date ranges on report endpoints in REST v1 and MCP, plus PDF export via API
Requested by a v1/MCP user: the web UI can produce resultat- and
balansrapport for a custom period with PDF export, but REST v1 and the
MCP tools only served whole fiscal years and silently ignored
from_date/to_date.
- v1 income-statement: optional from_date/to_date (validated against the
fiscal period via the same parseReportDateRange the dashboard uses)
- v1 balance-sheet: same, plus as_of as the natural alias for to_date
(mutually exclusive with it)
- Unknown query params on these report routes now return
VALIDATION_ERROR with the unknown and allowed names instead of being
silently dropped (scoped to these routes, not a global v1 change)
- MCP gnubok_get_income_statement: from_date/to_date;
gnubok_get_balance_sheet: as_of_date; both validate format, in-period
and ordering, and reject unknown args (tools/list payload bench held
under the ceiling by trimming the same tools' descriptions)
- New v1 PDF endpoints reports/{income-statement,balance-sheet}/pdf,
byte-equivalent to the dashboard export: the K2/K3 grouping and the
balance gate moved to lib/reports/financial-statement-pdf.ts, shared
by both surfaces
- Both JSON endpoints echo the effective range in data.period
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
* fix(reports): range semantics, empty-date validation, and review findings on PR #1909
Consolidated resolution of the skeptic refutations, CI failures, and
CodeRabbit findings:
- Ranged income statement summed closing balances, so from_date after
period start returned year-to-date figures mislabeled as the range
(July revenue reported as Jan-Jul on JSON, PDF, and MCP). The trial
balance rolls pre-range P&L activity into opening columns, so
generateIncomeStatement now builds from period movements whenever
fromDate is set, matching the resultatrapport convention. Full-period
behavior is unchanged; generator-level regression tests added.
- from_date dropped from the v1 balance-sheet routes (JSON + PDF): a
balansraking is a cumulative position, not a flow over a window
(ÅRL 3 kap); matches the MCP tool's as_of_date-only surface.
- Empty date values (from_date=) now fail validation instead of
silently producing a full-period report with an empty period echo
(null-check instead of truthiness in parseReportDateRange).
- dry_run, read by the withApiV1 wrapper on every request, is tolerated
by the strict param check instead of being rejected as unknown.
- Unbalanced balansrakning on the v1 PDF route returns 400 (caller-data
condition), matching the dashboard export, instead of 500.
- skills/accounted-api regenerated (apiskill:check gate).
- Removed the ISO_DATE_RE import that collided with the pre-existing
local declaration in the MCP server (TS2440 on core build).
- CodeRabbit: 401 tests for both PDF endpoints; event bus cleared in
the new MCP test's beforeEach.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
56 lines
2.1 KiB
TypeScript
56 lines
2.1 KiB
TypeScript
/**
|
|
* Parse and validate optional `from_date` / `to_date` query params for the
|
|
* date-range-aware financial reports (resultat- and balansrapport).
|
|
*
|
|
* Returns the bounds clamped against the fiscal period. Both params are
|
|
* optional: when omitted, the report falls back to the period as a whole.
|
|
* Returns a `{ error }` shape on invalid input so callers can map it to a
|
|
* 400 response without each route duplicating the same checks.
|
|
*/
|
|
export type DateRange = { fromDate?: string; toDate?: string }
|
|
|
|
export type DateRangeResult =
|
|
| { ok: true; range: DateRange }
|
|
| { ok: false; error: string }
|
|
|
|
const ISO_DATE = /^\d{4}-\d{2}-\d{2}$/
|
|
|
|
export function parseReportDateRange(
|
|
searchParams: URLSearchParams,
|
|
period: { period_start: string; period_end: string }
|
|
): DateRangeResult {
|
|
const rawFrom = searchParams.get('from_date')
|
|
const rawTo = searchParams.get('to_date')
|
|
|
|
// Null-check, not truthiness: an empty value (`from_date=`, the classic
|
|
// unfilled template variable) must fail the format check rather than slip
|
|
// through as a silent full-period request with an empty period echo.
|
|
if (rawFrom !== null && !ISO_DATE.test(rawFrom)) {
|
|
return { ok: false, error: 'from_date måste vara på formen YYYY-MM-DD.' }
|
|
}
|
|
if (rawTo !== null && !ISO_DATE.test(rawTo)) {
|
|
return { ok: false, error: 'to_date måste vara på formen YYYY-MM-DD.' }
|
|
}
|
|
|
|
const fromDate = rawFrom ?? undefined
|
|
const toDate = rawTo ?? undefined
|
|
|
|
if (fromDate && (fromDate < period.period_start || fromDate > period.period_end)) {
|
|
return {
|
|
ok: false,
|
|
error: `from_date måste ligga inom räkenskapsåret (${period.period_start}: ${period.period_end}).`,
|
|
}
|
|
}
|
|
if (toDate && (toDate < period.period_start || toDate > period.period_end)) {
|
|
return {
|
|
ok: false,
|
|
error: `to_date måste ligga inom räkenskapsåret (${period.period_start}: ${period.period_end}).`,
|
|
}
|
|
}
|
|
if (fromDate && toDate && fromDate > toDate) {
|
|
return { ok: false, error: 'from_date får inte vara efter to_date.' }
|
|
}
|
|
|
|
return { ok: true, range: { fromDate, toDate } }
|
|
}
|