* fix(reports): stabilize fetchAllRows paging to stop doubled/dropped balances (#790, #791) PostgREST `.range()` paging is only correct when the underlying query has a stable TOTAL order. Several aggregating report queries (general ledger, trial balance, grundbok, supplier/AR ledgers, etc.) paginated without `.order()`, so on datasets larger than one 1000-row page Postgres could return rows in a different order between requests — silently DUPLICATING or SKIPPING rows on a page boundary and doubling or dropping financial totals. - fetch-all.ts: document the ordering invariant and add an optional `dedupeBy` defense-in-depth that drops cross-page duplicates and warns when it fires (surfaces a missing `.order()` in logs instead of corrupting money). - Add a stable `.order()` (line PK or account_number) to every paginated query in lib/reports/ and the account-balances route; pass `dedupeBy` on the money-aggregating line queries. - Add fetch-all unit tests and update report test fixtures to carry row ids. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(api): declare the real { data, meta } envelope on v1 single/write/204 endpoints (#794) The OpenAPI generator derives each endpoint's documented body purely from its registered `response.success` Zod schema, and that schema is never validated at runtime — so a route could advertise a shape its handler never sends. #802 fixed this for list endpoints; the same drift was latent on single-resource and write endpoints, which declared the bare resource schema instead of the `{ data, meta }` envelope the handlers actually return. - registry.ts: extend `ResponseMetaSchema` with the optional `audit` block and `partial_expansions` list that writes/expansions emit; add the `NoBodyResponse` sentinel so 204 DELETE handlers document a bare 204 instead of a phantom 200. - Wrap every single/write endpoint's `response.success` in `dataEnvelope(...)` (or `NoBodyResponse` for 204s) across the v1 routes. - Add a response-envelope contract test that fails CI if any JSON endpoint forgets to wrap its schema, with binary downloads and 204s as the only exemptions. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(reports): extend paging dedupeBy to rc-basis-gaps and opening-balances Address PR review: these two money-aggregating line queries already had the stable `.order('id')` (so paging was correct) but didn't carry `id` in the select, so they couldn't use the `dedupeBy` defense-in-depth that general-ledger and trial-balance got. Select `id` and pass `dedupeBy: r => r.id` so the whole report layer applies the ordering invariant consistently. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
62 lines
1.8 KiB
TypeScript
62 lines
1.8 KiB
TypeScript
/**
|
|
* GET /api/v1/health — public health check.
|
|
*
|
|
* Returns service status. No auth required. Used by load balancers, uptime
|
|
* checks, and as a smoke test for the v1 wrapper itself.
|
|
*/
|
|
|
|
import { z } from 'zod'
|
|
import { ok } from '@/lib/api/v1/response'
|
|
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
|
|
import { API_V1_VERSION } from '@/lib/api/v1/version'
|
|
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
|
|
|
|
const HealthResponse = z.object({
|
|
status: z.enum(['ok', 'degraded']),
|
|
service: z.literal('gnubok'),
|
|
api_version: z.string(),
|
|
timestamp: z.string(),
|
|
})
|
|
|
|
registerEndpoint({
|
|
operation: 'health.check',
|
|
method: 'GET',
|
|
path: '/api/v1/health',
|
|
summary: 'Health check.',
|
|
description: 'Reports the API is reachable and what version is currently served. Public; no auth required.',
|
|
useWhen: 'You want to verify connectivity, latency, or which API version is live before issuing other requests.',
|
|
doNotUseFor: 'Anything that needs authenticated data. This endpoint returns no company-specific information.',
|
|
pitfalls: [
|
|
'A 200 here only means the API process responds — downstream Postgres/Supabase may still be degraded.',
|
|
],
|
|
example: {
|
|
response: {
|
|
data: {
|
|
status: 'ok',
|
|
service: 'gnubok',
|
|
api_version: '2026-05-12',
|
|
timestamp: '2026-05-12T16:25:06Z',
|
|
},
|
|
meta: { request_id: 'req_…', api_version: '2026-05-12' },
|
|
},
|
|
},
|
|
scope: null,
|
|
risk: 'low',
|
|
idempotent: true,
|
|
reversible: false,
|
|
dryRunSupported: false,
|
|
response: { success: dataEnvelope(HealthResponse) },
|
|
})
|
|
|
|
export const GET = withApiV1('health.check', async (_request, ctx) => {
|
|
return ok(
|
|
{
|
|
status: 'ok' as const,
|
|
service: 'gnubok' as const,
|
|
api_version: API_V1_VERSION,
|
|
timestamp: new Date().toISOString(),
|
|
},
|
|
{ requestId: ctx.requestId },
|
|
)
|
|
})
|