fix(api): stabilize report pagination + declare real { data, meta } envelope on v1 single/write endpoints (#811)

* 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>
This commit is contained in:
Jakob Wennberg
2026-06-28 13:42:50 +02:00
committed by GitHub
parent ae17b304d7
commit fce6faff2c
103 changed files with 738 additions and 334 deletions
@@ -120,7 +120,8 @@ export async function GET(request: Request) {
query = query.neq('journal_entry_id', obEntryId)
}
return query.range(from, to)
// Stable total order for correct paging (see fetch-all.ts).
return query.order('id', { ascending: true }).range(from, to)
})
} catch (err) {
log.error('period activity lookup failed', {
@@ -20,7 +20,7 @@
import { z } from 'zod'
import type { SupabaseClient } from '@supabase/supabase-js'
import { ok } from '@/lib/api/v1/response'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { ownsFiscalPeriod } from '@/lib/api/v1/owns-fiscal-period'
@@ -234,7 +234,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: false,
response: { success: ComplianceCheckResponse },
response: { success: dataEnvelope(ComplianceCheckResponse) },
})
export const GET = withApiV1<{ params: Promise<{ companyId: string }> }>(
@@ -16,7 +16,7 @@ import { z } from 'zod'
import { noContent, ok } from '@/lib/api/v1/response'
import { dryRunPreview } from '@/lib/api/v1/dry-run'
import { parseExpand } from '@/lib/api/v1/expand'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope, NoBodyResponse } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { UpdateCustomerSchema } from '@/lib/api/schemas'
@@ -100,7 +100,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: false,
response: { success: CustomerDetail },
response: { success: dataEnvelope(CustomerDetail) },
})
export const GET = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -244,7 +244,7 @@ registerEndpoint({
reversible: true,
dryRunSupported: true,
request: { body: UpdateCustomerSchema },
response: { success: CustomerDetail },
response: { success: dataEnvelope(CustomerDetail) },
})
const CUSTOMER_UPDATE_RESPONSE_COLUMNS = CUSTOMER_DETAIL_COLUMNS
@@ -438,7 +438,7 @@ registerEndpoint({
idempotent: true,
reversible: true,
dryRunSupported: true,
response: { success: z.object({}) },
response: { success: NoBodyResponse },
})
export const DELETE = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -17,7 +17,7 @@ import { z } from 'zod'
import type { SupabaseClient } from '@supabase/supabase-js'
import { ok } from '@/lib/api/v1/response'
import { dryRunPreview } from '@/lib/api/v1/dry-run'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { CreateCustomerSchema } from '@/lib/api/schemas'
@@ -99,7 +99,7 @@ registerEndpoint({
reversible: true,
dryRunSupported: true,
request: { body: BulkCreateRequest },
response: { success: BulkCreateResponse },
response: { success: dataEnvelope(BulkCreateResponse) },
})
interface ResultItem {
@@ -16,7 +16,7 @@ import {
encodeDefaultCursor,
parsePaginationParams,
} from '@/lib/api/v1/pagination'
import { registerEndpoint, listEnvelope } from '@/lib/api/v1/registry'
import { registerEndpoint, listEnvelope, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { CreateCustomerSchema } from '@/lib/api/schemas'
@@ -301,7 +301,7 @@ registerEndpoint({
reversible: true,
dryRunSupported: true,
request: { body: CreateCustomerSchema },
response: { success: CustomerCreated },
response: { success: dataEnvelope(CustomerCreated) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string }> }>(
@@ -13,7 +13,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { eventBus } from '@/lib/events'
@@ -77,7 +77,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: false,
response: { success: DocumentDownloadResponse },
response: { success: dataEnvelope(DocumentDownloadResponse) },
})
export const GET = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -18,7 +18,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { dryRunPreview } from '@/lib/api/v1/dry-run'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { linkToJournalEntry } from '@/lib/core/documents/document-service'
@@ -71,7 +71,7 @@ registerEndpoint({
reversible: false,
dryRunSupported: true,
request: { body: Body },
response: { success: DocumentLinkedResponse },
response: { success: dataEnvelope(DocumentLinkedResponse) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -31,7 +31,7 @@
import { z } from 'zod'
import { created } from '@/lib/api/v1/response'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import {
@@ -110,7 +110,7 @@ registerEndpoint({
reversible: false,
dryRunSupported: false,
request: { body: MultipartBodySchema, contentType: 'multipart/form-data' },
response: { success: DocumentUploaded },
response: { success: dataEnvelope(DocumentUploaded) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string }> }>(
@@ -20,7 +20,7 @@
import { z } from 'zod'
import { ok, noContent } from '@/lib/api/v1/response'
import { dryRunPreview } from '@/lib/api/v1/dry-run'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope, NoBodyResponse } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { UpdateEmployeeSchema } from '@/lib/api/schemas'
@@ -136,7 +136,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: false,
response: { success: EmployeeDetail },
response: { success: dataEnvelope(EmployeeDetail) },
})
export const GET = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -201,7 +201,7 @@ registerEndpoint({
request: { body: UpdateEmployeeSchema },
// Write responses mask personnummer (GDPR Art.5(1)(c)) — only the GET
// drill-in returns the full value. Symmetric with the POST response.
response: { success: EmployeeWriteResponse },
response: { success: dataEnvelope(EmployeeWriteResponse) },
})
export const PATCH = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -402,7 +402,7 @@ registerEndpoint({
idempotent: true,
reversible: true,
dryRunSupported: true,
response: { success: z.object({}) },
response: { success: NoBodyResponse },
})
export const DELETE = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -24,7 +24,7 @@ import {
encodeDefaultCursor,
parsePaginationParams,
} from '@/lib/api/v1/pagination'
import { registerEndpoint, listEnvelope } from '@/lib/api/v1/registry'
import { registerEndpoint, listEnvelope, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { CreateEmployeeSchema } from '@/lib/api/schemas'
@@ -318,7 +318,7 @@ registerEndpoint({
reversible: true,
dryRunSupported: true,
request: { body: CreateEmployeeSchema },
response: { success: EmployeeCreated },
response: { success: dataEnvelope(EmployeeCreated) },
})
const EMPLOYEE_RESPONSE_COLUMNS =
@@ -11,7 +11,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { closePeriod } from '@/lib/core/bookkeeping/period-service'
@@ -49,7 +49,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: false,
response: { success: PeriodClosedResponse },
response: { success: dataEnvelope(PeriodClosedResponse) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -12,7 +12,7 @@
import { z } from 'zod'
import { accepted } from '@/lib/api/v1/response'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { ownsFiscalPeriod } from '@/lib/api/v1/owns-fiscal-period'
@@ -59,7 +59,7 @@ registerEndpoint({
reversible: true,
dryRunSupported: false,
request: { body: Body },
response: { success: RevaluationAccepted },
response: { success: dataEnvelope(RevaluationAccepted) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -8,7 +8,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { lockPeriod } from '@/lib/core/bookkeeping/period-service'
@@ -46,7 +46,7 @@ registerEndpoint({
idempotent: true,
reversible: true,
dryRunSupported: false,
response: { success: PeriodLockedResponse },
response: { success: dataEnvelope(PeriodLockedResponse) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -12,7 +12,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { ownsFiscalPeriod } from '@/lib/api/v1/owns-fiscal-period'
@@ -56,7 +56,7 @@ registerEndpoint({
reversible: true,
dryRunSupported: false,
request: { body: Body },
response: { success: OpeningBalancesResponse },
response: { success: dataEnvelope(OpeningBalancesResponse) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -15,7 +15,7 @@
import { z } from 'zod'
import { accepted } from '@/lib/api/v1/response'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { ownsFiscalPeriod } from '@/lib/api/v1/owns-fiscal-period'
@@ -62,7 +62,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: false,
response: { success: YearEndAcceptedResponse },
response: { success: dataEnvelope(YearEndAcceptedResponse) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -21,7 +21,7 @@
import { z } from 'zod'
import { accepted } from '@/lib/api/v1/response'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import {
@@ -83,7 +83,7 @@ registerEndpoint({
reversible: false,
dryRunSupported: false,
request: { contentType: 'multipart/form-data' },
response: { success: BankImportAccepted },
response: { success: dataEnvelope(BankImportAccepted) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string }> }>(
@@ -24,7 +24,7 @@
import { z } from 'zod'
import { accepted } from '@/lib/api/v1/response'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import {
@@ -94,7 +94,7 @@ registerEndpoint({
reversible: false,
dryRunSupported: false,
request: { contentType: 'multipart/form-data' },
response: { success: SieImportAccepted },
response: { success: dataEnvelope(SieImportAccepted) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string }> }>(
@@ -27,7 +27,7 @@
import { z } from 'zod'
import { created } from '@/lib/api/v1/response'
import { dryRunPreview } from '@/lib/api/v1/dry-run'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { createCreditNoteJournalEntry } from '@/lib/bookkeeping/invoice-entries'
@@ -96,7 +96,7 @@ registerEndpoint({
reversible: false,
dryRunSupported: true,
request: { body: CreditNoteRequest },
response: { success: CreditNoteCreated },
response: { success: dataEnvelope(CreditNoteCreated) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -29,7 +29,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { dryRunPreview } from '@/lib/api/v1/dry-run'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { MarkInvoicePaidSchema } from '@/lib/api/schemas'
@@ -99,7 +99,7 @@ registerEndpoint({
reversible: false,
dryRunSupported: true,
request: { body: MarkInvoicePaidSchema },
response: { success: InvoiceMarkPaidResponse },
response: { success: dataEnvelope(InvoiceMarkPaidResponse) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -36,7 +36,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { dryRunPreview } from '@/lib/api/v1/dry-run'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { createInvoiceJournalEntry } from '@/lib/bookkeeping/invoice-entries'
@@ -97,7 +97,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: true,
response: { success: InvoiceMarkSentResponse },
response: { success: dataEnvelope(InvoiceMarkSentResponse) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -16,7 +16,7 @@ import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { dryRunPreview } from '@/lib/api/v1/dry-run'
import { parseExpand } from '@/lib/api/v1/expand'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
@@ -108,7 +108,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: false,
response: { success: InvoiceDetail },
response: { success: dataEnvelope(InvoiceDetail) },
})
export const GET = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -210,7 +210,7 @@ registerEndpoint({
reversible: true,
dryRunSupported: true,
request: { body: V1PatchDraftInvoiceSchema },
response: { success: InvoiceDetail },
response: { success: dataEnvelope(InvoiceDetail) },
})
const INVOICE_PATCH_RESPONSE_COLUMNS =
@@ -39,7 +39,7 @@ import { z } from 'zod'
import { renderToBuffer } from '@react-pdf/renderer'
import { ok } from '@/lib/api/v1/response'
import { dryRunPreview } from '@/lib/api/v1/dry-run'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { InvoicePDF } from '@/lib/invoices/pdf-template'
@@ -113,7 +113,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: true,
response: { success: InvoiceSendResponse },
response: { success: dataEnvelope(InvoiceSendResponse) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -36,7 +36,7 @@ import { z } from 'zod'
import type { SupabaseClient } from '@supabase/supabase-js'
import { ok } from '@/lib/api/v1/response'
import { dryRunPreview } from '@/lib/api/v1/dry-run'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { CreateInvoiceSchema } from '@/lib/api/schemas'
@@ -128,7 +128,7 @@ registerEndpoint({
reversible: true,
dryRunSupported: true,
request: { body: BulkCreateRequest },
response: { success: BulkCreateResponse },
response: { success: dataEnvelope(BulkCreateResponse) },
})
interface ResultItem {
@@ -23,7 +23,7 @@ import {
parsePaginationParams,
} from '@/lib/api/v1/pagination'
import { parseExpand } from '@/lib/api/v1/expand'
import { registerEndpoint, listEnvelope } from '@/lib/api/v1/registry'
import { registerEndpoint, listEnvelope, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { CreateInvoiceSchema } from '@/lib/api/schemas'
@@ -372,7 +372,7 @@ registerEndpoint({
reversible: true,
dryRunSupported: true,
request: { body: CreateInvoiceSchema },
response: { success: InvoiceCreated },
response: { success: dataEnvelope(InvoiceCreated) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string }> }>(
@@ -15,7 +15,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { dryRunPreview } from '@/lib/api/v1/dry-run'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { commitEntry, getNextVoucherNumber } from '@/lib/bookkeeping/engine'
@@ -59,7 +59,7 @@ registerEndpoint({
idempotent: true,
reversible: true,
dryRunSupported: true,
response: { success: JournalEntryCommitted },
response: { success: dataEnvelope(JournalEntryCommitted) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -16,7 +16,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { dryRunPreview } from '@/lib/api/v1/dry-run'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { checkPeriodLock } from '@/lib/api/v1/check-period-lock'
@@ -76,7 +76,7 @@ registerEndpoint({
reversible: false,
dryRunSupported: true,
request: { body: CorrectJournalEntrySchema },
response: { success: JournalEntryCorrected },
response: { success: dataEnvelope(JournalEntryCorrected) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -14,7 +14,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { dryRunPreview } from '@/lib/api/v1/dry-run'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { checkPeriodLock } from '@/lib/api/v1/check-period-lock'
@@ -68,7 +68,7 @@ registerEndpoint({
reversible: false,
dryRunSupported: true,
request: { body: ReverseRequest },
response: { success: JournalEntryReversed },
response: { success: dataEnvelope(JournalEntryReversed) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -7,7 +7,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
@@ -86,7 +86,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: false,
response: { success: JournalEntryDetail },
response: { success: dataEnvelope(JournalEntryDetail) },
})
export const GET = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -17,7 +17,7 @@ import { z } from 'zod'
import type { SupabaseClient } from '@supabase/supabase-js'
import { ok } from '@/lib/api/v1/response'
import { dryRunPreview } from '@/lib/api/v1/dry-run'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { ownsFiscalPeriod } from '@/lib/api/v1/owns-fiscal-period'
@@ -89,7 +89,7 @@ registerEndpoint({
reversible: true,
dryRunSupported: true,
request: { body: BulkRequest },
response: { success: BulkResponse },
response: { success: dataEnvelope(BulkResponse) },
})
interface ResultItem {
@@ -21,7 +21,7 @@ import {
encodeDefaultCursor,
parsePaginationParams,
} from '@/lib/api/v1/pagination'
import { registerEndpoint, listEnvelope } from '@/lib/api/v1/registry'
import { registerEndpoint, listEnvelope, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { checkPeriodLock } from '@/lib/api/v1/check-period-lock'
@@ -246,7 +246,7 @@ registerEndpoint({
reversible: true,
dryRunSupported: true,
request: { body: CreateJournalEntrySchema },
response: { success: JournalEntryDetail },
response: { success: dataEnvelope(JournalEntryDetail) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string }> }>(
@@ -12,7 +12,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { dryRunPreview } from '@/lib/api/v1/dry-run'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { runReconciliation } from '@/lib/reconciliation/bank-reconciliation'
@@ -88,7 +88,7 @@ registerEndpoint({
reversible: false,
dryRunSupported: true,
request: { body: RunRequest },
response: { success: RunResponse },
response: { success: dataEnvelope(RunResponse) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string }> }>(
@@ -7,7 +7,7 @@
*/
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { getReconciliationStatus } from '@/lib/reconciliation/bank-reconciliation'
@@ -56,7 +56,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: false,
response: { success: StatusResponse },
response: { success: dataEnvelope(StatusResponse) },
})
export const GET = withApiV1<{ params: Promise<{ companyId: string }> }>(
@@ -7,7 +7,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { safeGenerate } from '@/lib/api/v1/report-period'
@@ -39,7 +39,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: false,
response: { success: z.unknown() },
response: { success: dataEnvelope(z.unknown()) },
})
export const GET = withApiV1<{ params: Promise<{ companyId: string }> }>(
@@ -7,7 +7,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { safeGenerate } from '@/lib/api/v1/report-period'
@@ -39,7 +39,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: false,
response: { success: z.unknown() },
response: { success: dataEnvelope(z.unknown()) },
})
export const GET = withApiV1<{ params: Promise<{ companyId: string }> }>(
@@ -8,7 +8,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { loadPeriodFromQuery, safeGenerate } from '@/lib/api/v1/report-period'
import { generateBalanceSheet } from '@/lib/reports/balance-sheet'
@@ -49,7 +49,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: false,
response: { success: BalanceSheetResponse },
response: { success: dataEnvelope(BalanceSheetResponse) },
})
export const GET = withApiV1<{ params: Promise<{ companyId: string }> }>(
@@ -11,7 +11,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { loadPeriodFromQuery, safeGenerate } from '@/lib/api/v1/report-period'
import { validateBalanceContinuity } from '@/lib/reports/continuity-check'
@@ -42,7 +42,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: false,
response: { success: z.unknown() },
response: { success: dataEnvelope(z.unknown()) },
})
export const GET = withApiV1<{ params: Promise<{ companyId: string }> }>(
@@ -8,7 +8,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { loadPeriodFromQuery, safeGenerate } from '@/lib/api/v1/report-period'
import { v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
@@ -43,7 +43,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: false,
response: { success: GeneralLedgerResponse },
response: { success: dataEnvelope(GeneralLedgerResponse) },
})
export const GET = withApiV1<{ params: Promise<{ companyId: string }> }>(
@@ -8,7 +8,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { loadPeriodFromQuery, safeGenerate } from '@/lib/api/v1/report-period'
import { generateIncomeStatement } from '@/lib/reports/income-statement'
@@ -41,7 +41,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: false,
response: { success: IncomeStatementResponse },
response: { success: dataEnvelope(IncomeStatementResponse) },
})
export const GET = withApiV1<{ params: Promise<{ companyId: string }> }>(
@@ -7,7 +7,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { loadPeriodFromQuery, safeGenerate } from '@/lib/api/v1/report-period'
import { generateJournalRegister } from '@/lib/reports/journal-register'
@@ -39,7 +39,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: false,
response: { success: z.unknown() },
response: { success: dataEnvelope(z.unknown()) },
})
export const GET = withApiV1<{ params: Promise<{ companyId: string }> }>(
@@ -8,7 +8,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { loadPeriodFromQuery, safeGenerate } from '@/lib/api/v1/report-period'
import { generateMonthlyBreakdown } from '@/lib/reports/monthly-breakdown'
@@ -36,7 +36,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: false,
response: { success: z.unknown() },
response: { success: dataEnvelope(z.unknown()) },
})
export const GET = withApiV1<{ params: Promise<{ companyId: string }> }>(
@@ -7,7 +7,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { safeGenerate } from '@/lib/api/v1/report-period'
@@ -41,7 +41,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: false,
response: { success: z.unknown() },
response: { success: dataEnvelope(z.unknown()) },
})
export const GET = withApiV1<{ params: Promise<{ companyId: string }> }>(
@@ -7,7 +7,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { safeGenerate } from '@/lib/api/v1/report-period'
@@ -39,7 +39,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: false,
response: { success: z.unknown() },
response: { success: dataEnvelope(z.unknown()) },
})
export const GET = withApiV1<{ params: Promise<{ companyId: string }> }>(
@@ -9,7 +9,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { loadPeriodFromQuery, safeGenerate } from '@/lib/api/v1/report-period'
import { generateTrialBalance } from '@/lib/reports/trial-balance'
@@ -64,7 +64,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: false,
response: { success: TrialBalanceResponse },
response: { success: dataEnvelope(TrialBalanceResponse) },
})
export const GET = withApiV1<{ params: Promise<{ companyId: string }> }>(
@@ -7,7 +7,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { safeGenerate } from '@/lib/api/v1/report-period'
@@ -39,7 +39,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: false,
response: { success: z.unknown() },
response: { success: dataEnvelope(z.unknown()) },
})
export const GET = withApiV1<{ params: Promise<{ companyId: string }> }>(
@@ -8,7 +8,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { safeGenerate } from '@/lib/api/v1/report-period'
@@ -72,7 +72,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: false,
response: { success: z.unknown() },
response: { success: dataEnvelope(z.unknown()) },
})
export const GET = withApiV1<{ params: Promise<{ companyId: string }> }>(
@@ -18,7 +18,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { dryRunPreview } from '@/lib/api/v1/dry-run'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { eventBus } from '@/lib/events'
@@ -68,7 +68,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: true,
response: { success: SalaryRunApproved },
response: { success: dataEnvelope(SalaryRunApproved) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -29,7 +29,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { dryRunPreview } from '@/lib/api/v1/dry-run'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { checkPeriodLock } from '@/lib/api/v1/check-period-lock'
@@ -99,7 +99,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: true,
response: { success: SalaryRunBooked },
response: { success: dataEnvelope(SalaryRunBooked) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -25,7 +25,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { dryRunPreview } from '@/lib/api/v1/dry-run'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { runSalaryCalculation } from '@/lib/salary/run-calculation'
@@ -85,7 +85,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: true,
response: { success: SalaryRunCalculated },
response: { success: dataEnvelope(SalaryRunCalculated) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -29,7 +29,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { generateAgiDeclaration } from '@/lib/salary/agi/generate-declaration'
@@ -99,7 +99,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: false,
response: { success: AgiGenerated },
response: { success: dataEnvelope(AgiGenerated) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -13,7 +13,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { dryRunPreview } from '@/lib/api/v1/dry-run'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
@@ -51,7 +51,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: true,
response: { success: SalaryRunPaid },
response: { success: dataEnvelope(SalaryRunPaid) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -14,7 +14,7 @@
import { z } from 'zod'
import { ok, noContent } from '@/lib/api/v1/response'
import { dryRunPreview } from '@/lib/api/v1/dry-run'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope, NoBodyResponse } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
@@ -92,7 +92,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: false,
response: { success: SalaryRunDetail },
response: { success: dataEnvelope(SalaryRunDetail) },
})
export const GET = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -160,7 +160,7 @@ registerEndpoint({
reversible: false,
dryRunSupported: true,
request: { body: UpdateSalaryRunSchema },
response: { success: SalaryRunDetail },
response: { success: dataEnvelope(SalaryRunDetail) },
})
export const PATCH = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -306,7 +306,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: true,
response: { success: z.object({}) },
response: { success: NoBodyResponse },
})
export const DELETE = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -20,7 +20,7 @@ import {
encodeDefaultCursor,
parsePaginationParams,
} from '@/lib/api/v1/pagination'
import { registerEndpoint, listEnvelope } from '@/lib/api/v1/registry'
import { registerEndpoint, listEnvelope, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { CreateSalaryRunSchema } from '@/lib/api/schemas'
@@ -229,7 +229,7 @@ registerEndpoint({
reversible: true,
dryRunSupported: true,
request: { body: CreateSalaryRunSchema },
response: { success: SalaryRunCreated },
response: { success: dataEnvelope(SalaryRunCreated) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string }> }>(
@@ -14,7 +14,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { dryRunPreview } from '@/lib/api/v1/dry-run'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { eventBus } from '@/lib/events'
@@ -56,7 +56,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: true,
response: { success: SupplierInvoiceApproved },
response: { success: dataEnvelope(SupplierInvoiceApproved) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -20,7 +20,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { dryRunPreview } from '@/lib/api/v1/dry-run'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { checkPeriodLock } from '@/lib/api/v1/check-period-lock'
@@ -102,7 +102,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: true,
response: { success: SupplierInvoiceCredited },
response: { success: dataEnvelope(SupplierInvoiceCredited) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -17,7 +17,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { dryRunPreview } from '@/lib/api/v1/dry-run'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { checkPeriodLock } from '@/lib/api/v1/check-period-lock'
@@ -85,7 +85,7 @@ registerEndpoint({
reversible: false,
dryRunSupported: true,
request: { body: MarkSupplierInvoicePaidSchema },
response: { success: SupplierInvoicePaidResponse },
response: { success: dataEnvelope(SupplierInvoicePaidResponse) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -16,7 +16,7 @@ import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { dryRunPreview } from '@/lib/api/v1/dry-run'
import { parseExpand } from '@/lib/api/v1/expand'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { UpdateSupplierInvoiceSchema } from '@/lib/api/schemas'
@@ -110,7 +110,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: false,
response: { success: SupplierInvoiceDetail },
response: { success: dataEnvelope(SupplierInvoiceDetail) },
})
export const GET = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -197,7 +197,7 @@ registerEndpoint({
reversible: true,
dryRunSupported: true,
request: { body: V1PatchSupplierInvoiceSchema },
response: { success: SupplierInvoiceDetail },
response: { success: dataEnvelope(SupplierInvoiceDetail) },
})
export const PATCH = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -29,7 +29,7 @@ import {
encodeDefaultCursor,
parsePaginationParams,
} from '@/lib/api/v1/pagination'
import { registerEndpoint, listEnvelope } from '@/lib/api/v1/registry'
import { registerEndpoint, listEnvelope, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { checkPeriodLock } from '@/lib/api/v1/check-period-lock'
@@ -329,7 +329,7 @@ registerEndpoint({
reversible: true,
dryRunSupported: true,
request: { body: CreateSupplierInvoiceSchema },
response: { success: SupplierInvoiceCreated },
response: { success: dataEnvelope(SupplierInvoiceCreated) },
})
interface ComputedItem {
@@ -16,7 +16,7 @@ import { z } from 'zod'
import { noContent, ok } from '@/lib/api/v1/response'
import { dryRunPreview } from '@/lib/api/v1/dry-run'
import { parseExpand } from '@/lib/api/v1/expand'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope, NoBodyResponse } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { UpdateSupplierSchema } from '@/lib/api/schemas'
@@ -112,7 +112,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: false,
response: { success: SupplierDetail },
response: { success: dataEnvelope(SupplierDetail) },
})
export const GET = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -242,7 +242,7 @@ registerEndpoint({
reversible: true,
dryRunSupported: true,
request: { body: UpdateSupplierSchema },
response: { success: SupplierDetail },
response: { success: dataEnvelope(SupplierDetail) },
})
export const PATCH = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -452,7 +452,7 @@ registerEndpoint({
idempotent: true,
reversible: true,
dryRunSupported: true,
response: { success: z.object({}) },
response: { success: NoBodyResponse },
})
export const DELETE = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -17,7 +17,7 @@ import { z } from 'zod'
import type { SupabaseClient } from '@supabase/supabase-js'
import { ok } from '@/lib/api/v1/response'
import { dryRunPreview } from '@/lib/api/v1/dry-run'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { CreateSupplierSchema } from '@/lib/api/schemas'
@@ -96,7 +96,7 @@ registerEndpoint({
reversible: true,
dryRunSupported: true,
request: { body: BulkCreateRequest },
response: { success: BulkCreateResponse },
response: { success: dataEnvelope(BulkCreateResponse) },
})
interface ResultItem {
@@ -21,7 +21,7 @@ import {
encodeDefaultCursor,
parsePaginationParams,
} from '@/lib/api/v1/pagination'
import { registerEndpoint, listEnvelope } from '@/lib/api/v1/registry'
import { registerEndpoint, listEnvelope, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { CreateSupplierSchema } from '@/lib/api/schemas'
@@ -307,7 +307,7 @@ registerEndpoint({
reversible: true,
dryRunSupported: true,
request: { body: CreateSupplierSchema },
response: { success: SupplierCreated },
response: { success: dataEnvelope(SupplierCreated) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string }> }>(
@@ -16,7 +16,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { dryRunPreview } from '@/lib/api/v1/dry-run'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { checkPeriodLock } from '@/lib/api/v1/check-period-lock'
@@ -89,7 +89,7 @@ registerEndpoint({
reversible: true,
dryRunSupported: true,
request: { body: CategorizeTransactionSchema },
response: { success: CategorizeResponse },
response: { success: dataEnvelope(CategorizeResponse) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -18,7 +18,7 @@
*/
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { MatchInvoiceSchema } from '@/lib/api/schemas'
@@ -87,7 +87,7 @@ registerEndpoint({
reversible: false,
dryRunSupported: false,
request: { body: MatchInvoiceSchema },
response: { success: MatchInvoiceResponse },
response: { success: dataEnvelope(MatchInvoiceResponse) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -7,7 +7,7 @@
*/
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { MatchSupplierInvoiceSchema } from '@/lib/api/schemas'
@@ -67,7 +67,7 @@ registerEndpoint({
reversible: false,
dryRunSupported: false,
request: { body: MatchSupplierInvoiceSchema },
response: { success: MatchSIResponse },
response: { success: dataEnvelope(MatchSIResponse) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -6,7 +6,7 @@
*/
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
@@ -75,7 +75,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: false,
response: { success: TransactionDetail },
response: { success: dataEnvelope(TransactionDetail) },
})
export const GET = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -12,7 +12,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { dryRunPreview } from '@/lib/api/v1/dry-run'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { reverseEntry } from '@/lib/bookkeeping/engine'
@@ -51,7 +51,7 @@ registerEndpoint({
idempotent: true,
reversible: false, // The reversal itself cannot be reversed via this endpoint.
dryRunSupported: true,
response: { success: UncategorizeResponse },
response: { success: dataEnvelope(UncategorizeResponse) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -12,7 +12,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { dryRunPreview } from '@/lib/api/v1/dry-run'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { checkPeriodLock } from '@/lib/api/v1/check-period-lock'
@@ -102,7 +102,7 @@ registerEndpoint({
reversible: true,
dryRunSupported: true,
request: { body: BatchRequest },
response: { success: BatchResponse },
response: { success: dataEnvelope(BatchResponse) },
})
interface Item {
@@ -18,7 +18,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { dryRunPreview } from '@/lib/api/v1/dry-run'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { ingestTransactions } from '@/lib/transactions/ingest'
@@ -99,7 +99,7 @@ registerEndpoint({
reversible: false,
dryRunSupported: true,
request: { body: IngestRequest },
response: { success: IngestResponse },
response: { success: dataEnvelope(IngestResponse) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string }> }>(
@@ -19,7 +19,7 @@
import { z } from 'zod'
import { created } from '@/lib/api/v1/response'
import { dryRunPreview } from '@/lib/api/v1/dry-run'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
@@ -83,7 +83,7 @@ registerEndpoint({
reversible: false,
dryRunSupported: true,
request: { body: CreateVoucherGapExplanation },
response: { success: VoucherGapExplanationCreated },
response: { success: dataEnvelope(VoucherGapExplanationCreated) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string }> }>(
@@ -25,7 +25,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { generateWebhookSecret } from '@/lib/webhooks/signing'
@@ -66,7 +66,7 @@ registerEndpoint({
idempotent: false,
reversible: false,
dryRunSupported: false,
response: { success: RotateSecretResponse },
response: { success: dataEnvelope(RotateSecretResponse) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -19,7 +19,7 @@
import { z } from 'zod'
import { ok, noContent } from '@/lib/api/v1/response'
import { dryRunPreview } from '@/lib/api/v1/dry-run'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope, NoBodyResponse } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { validateWebhookUrl } from '@/lib/webhooks/url-guard'
@@ -91,7 +91,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: false,
response: { success: WebhookDetail },
response: { success: dataEnvelope(WebhookDetail) },
})
export const GET = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -153,7 +153,7 @@ registerEndpoint({
reversible: true,
dryRunSupported: true,
request: { body: PatchWebhookSchema },
response: { success: WebhookDetail },
response: { success: dataEnvelope(WebhookDetail) },
})
export const PATCH = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -299,7 +299,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: false,
response: { success: z.object({ deleted: z.boolean() }) },
response: { success: NoBodyResponse },
})
export const DELETE = withApiV1<{ params: Promise<{ companyId: string; id: string }> }>(
@@ -10,7 +10,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
@@ -49,10 +49,10 @@ registerEndpoint({
reversible: false,
dryRunSupported: false,
response: {
success: z.object({
success: dataEnvelope(z.object({
webhook_delivery_id: z.string().uuid(),
status: z.literal('pending'),
}),
})),
},
})
@@ -209,7 +209,7 @@ registerEndpoint({
reversible: true,
dryRunSupported: true,
request: { body: CreateWebhookSchema },
response: { success: WebhookCreated },
response: { success: dataEnvelope(WebhookCreated) },
})
export const POST = withApiV1<{ params: Promise<{ companyId: string }> }>(
+2 -2
View File
@@ -7,7 +7,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { registerEndpoint } from '@/lib/api/v1/registry'
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'
@@ -45,7 +45,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: false,
response: { success: HealthResponse },
response: { success: dataEnvelope(HealthResponse) },
})
export const GET = withApiV1('health.check', async (_request, ctx) => {
+2 -2
View File
@@ -16,7 +16,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { getOperation } from '@/lib/api/v1/operations'
@@ -76,7 +76,7 @@ registerEndpoint({
idempotent: true,
reversible: false,
dryRunSupported: false,
response: { success: OperationDetail },
response: { success: dataEnvelope(OperationDetail) },
})
export const GET = withApiV1<{ params: Promise<{ id: string }> }>(
@@ -20,7 +20,7 @@
import { z } from 'zod'
import { ok } from '@/lib/api/v1/response'
import { registerEndpoint } from '@/lib/api/v1/registry'
import { registerEndpoint, dataEnvelope } from '@/lib/api/v1/registry'
import { withApiV1 } from '@/lib/api/v1/with-api-v1'
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
import { minimisePayload } from '@/lib/webhooks/handler'
@@ -59,10 +59,10 @@ registerEndpoint({
reversible: false,
dryRunSupported: false,
response: {
success: z.object({
success: dataEnvelope(z.object({
webhook_delivery_id: z.string().uuid(),
status: z.literal('pending'),
}),
})),
},
})
@@ -0,0 +1,131 @@
/**
* Response-envelope contract test.
*
* Every v1 handler returns the canonical `{ data, meta }` envelope `ok()` and
* `created()` wrap a single object, `paginated()` wraps an array, both stamping
* the shared `meta` block (see `lib/api/v1/response.ts`). The OpenAPI generator,
* however, derives each endpoint's documented body purely from its registered
* `response.success` Zod schema, and that schema is NOT validated at runtime
* so nothing stops a route from declaring a shape the handler never sends.
*
* That is exactly what issue #794 found: every list endpoint declared a bare
* `{ <name>: [...] }` object that no handler emits. #802 fixed the list
* endpoints (via `listEnvelope`/`dataEnvelope`); the same drift was latent on
* the single-resource and write endpoints, which declared the bare resource
* schema instead of `{ data, meta }`.
*
* This test is the regression guard the issue asked for. It asserts EVERY
* JSON-returning endpoint declares the `{ data, meta }` envelope with the shared
* `ResponseMetaSchema` so a new endpoint that forgets to wrap its schema
* (list OR single) fails CI here instead of shipping a lying spec. Binary
* downloads (`response.contentType`) and 204 No Content endpoints
* (`NoBodyResponse`) carry no JSON body and are the only exemptions.
*/
import { describe, expect, it } from 'vitest'
import { z } from 'zod'
import { listEndpoints, ResponseMetaSchema, NoBodyResponse, listEnvelope, dataEnvelope } from '../registry'
// Side-effect import — every route file's registerEndpoint() runs at module
// load time and populates the shared ENDPOINTS map.
import '../load-routes'
/** Binary downloads (PDF, SIE text) declare a non-JSON contentType. */
function isBinary(success: { contentType?: string }): boolean {
return !!success.contentType && success.contentType !== 'application/json'
}
describe('v1 response envelope contract', () => {
const endpoints = listEndpoints()
it('every JSON endpoint declares the { data, meta } envelope with the shared meta schema', () => {
// Accumulate every violation so a failing run names ALL offending endpoints
// at once (a fresh route that forgets to wrap, plus any that drift later),
// instead of failing one-at-a-time across many edit cycles.
const violations: string[] = []
for (const ep of endpoints) {
const ctx = `${ep.method} ${ep.path} (${ep.operation})`
// Exemptions: binary bodies and 204-no-content have no JSON envelope.
if (isBinary(ep.response)) continue
if (ep.response.success === NoBodyResponse) continue
const success = ep.response.success
if (!(success instanceof z.ZodObject)) {
violations.push(`${ctx}: response.success is not a { data, meta } object — wrap it with listEnvelope()/dataEnvelope() (or use NoBodyResponse for 204 / response.contentType for binary).`)
continue
}
const shape = (success as z.ZodObject<z.ZodRawShape>).shape
const keys = Object.keys(shape).sort()
if (keys.length !== 2 || keys[0] !== 'data' || keys[1] !== 'meta') {
violations.push(`${ctx}: top-level keys must be [data, meta] — found [${keys.join(', ')}]. The handler returns { data, meta }; declare it with listEnvelope()/dataEnvelope().`)
continue
}
// Reference equality: both envelope helpers wire in this exact schema, so
// a hand-rolled `{ data, meta: z.object({...}) }` that drifts from the
// real meta block is rejected too.
if (shape.meta !== ResponseMetaSchema) {
violations.push(`${ctx}: meta is not the shared ResponseMetaSchema (use listEnvelope()/dataEnvelope(), don't hand-roll the envelope).`)
}
}
expect(
violations,
`\n${violations.length} v1 endpoint(s) declare a response.success that doesn't match the { data, meta } envelope the handler actually returns:\n\n${violations.map((v) => `${v}`).join('\n')}\n`,
).toEqual([])
})
it('list endpoints expose data as an array (the paginated() envelope)', () => {
// Detect list endpoints structurally: their `data` is a Zod array. This is
// the half of the contract that maps onto paginated() specifically — guards
// against a list endpoint drifting from `{ data: [...] }` back to a bare
// `{ <name>: [...] }` (which would drop the array out of `data` entirely).
const arrayDataEndpoints = endpoints.filter((ep) => {
const s = ep.response.success
return s instanceof z.ZodObject && (s as z.ZodObject<z.ZodRawShape>).shape.data instanceof z.ZodArray
})
// The 10 cursor-paginated list endpoints (companies, customers, suppliers,
// invoices, supplier-invoices, journal-entries, transactions, employees,
// salary-runs, webhook deliveries). accounts/fiscal-periods/webhooks nest
// their array under a named key inside `data`, so they use dataEnvelope and
// are intentionally NOT counted here. A drop below this floor means a
// paginated endpoint silently lost its `data: [...]` shape.
expect(
arrayDataEndpoints.length,
`expected the known paginated list endpoints to keep data: z.array(...); found only ${arrayDataEndpoints.length}`,
).toBeGreaterThanOrEqual(10)
for (const ep of arrayDataEndpoints) {
const shape = (ep.response.success as z.ZodObject<z.ZodRawShape>).shape
expect(
shape.meta === ResponseMetaSchema,
`${ep.method} ${ep.path}: list envelope meta must be the shared ResponseMetaSchema`,
).toBe(true)
}
})
it('listEnvelope() and dataEnvelope() produce the canonical { data, meta } shape', () => {
const list = listEnvelope(z.object({ id: z.string() }))
expect(list instanceof z.ZodObject).toBe(true)
expect(Object.keys(list.shape).sort()).toEqual(['data', 'meta'])
expect(list.shape.data instanceof z.ZodArray).toBe(true)
expect(list.shape.meta === ResponseMetaSchema).toBe(true)
const data = dataEnvelope(z.object({ id: z.string() }))
expect(data instanceof z.ZodObject).toBe(true)
expect(Object.keys(data.shape).sort()).toEqual(['data', 'meta'])
expect(data.shape.data instanceof z.ZodObject).toBe(true)
expect(data.shape.meta === ResponseMetaSchema).toBe(true)
})
it('the shared meta schema carries request_id + api_version', () => {
// The envelope helpers are only correct if meta itself is well-formed.
expect(ResponseMetaSchema instanceof z.ZodObject).toBe(true)
const metaKeys = Object.keys(ResponseMetaSchema.shape)
expect(metaKeys).toContain('request_id')
expect(metaKeys).toContain('api_version')
})
})
+37 -5
View File
@@ -21,15 +21,31 @@ import type { ZodTypeAny } from 'zod'
import type { ApiKeyScope } from '@/lib/auth/api-keys'
import { API_V1_VERSION } from './version'
/**
* The audit block surfaced inline on write responses (see `AuditBlock` in
* `lib/api/v1/response.ts`) so an agent gets the voucher number / audit-trail
* URL without a second round-trip. Every field is optional.
*/
const ResponseAuditSchema = z.object({
voucher_number: z.string().optional(),
voucher_url: z.string().optional(),
audit_trail_url: z.string().optional(),
immutable_at: z.string().optional(),
})
/**
* The `meta` block echoed in every v1 response envelope (see
* `lib/api/v1/response.ts`). List endpoints additionally populate
* `next_cursor`; it is absent on the final page.
* `next_cursor`; it is absent on the final page. Writes may surface an
* `audit` block, and soft-degraded `?expand=` responses a `partial_expansions`
* list both optional, so reads and lists omit them.
*/
export const ResponseMetaSchema = z.object({
request_id: z.string(),
api_version: z.string(),
next_cursor: z.string().nullable().optional(),
audit: ResponseAuditSchema.optional(),
partial_expansions: z.array(z.string()).optional(),
})
/**
@@ -66,6 +82,18 @@ export function dataEnvelope<T extends ZodTypeAny>(data: T) {
})
}
/**
* Sentinel `response.success` for endpoints that return 204 No Content with an
* empty body e.g. DELETE handlers calling `noContent()`. The OpenAPI
* generator emits a bare `204` response (no schema) for these instead of a
* `200 { data, meta }`, and the envelope contract test exempts them.
*
* Identified by REFERENCE equality, so every 204 route MUST import this exact
* constant rather than declaring its own `z.object({})` that is what lets the
* generator and the contract test recognise the "no body" intent.
*/
export const NoBodyResponse = z.object({})
export type HttpMethod = 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE'
export type ActionRisk = 'low' | 'medium' | 'high'
@@ -327,6 +355,13 @@ export function generateOpenApiSpec(serverUrl: string): OpenApiSpec {
? { [def.response.contentType]: { schema: { type: 'string', format: 'binary' } } }
: { 'application/json': { schema: zodToJsonSchema(def.response.success) } }
// 204 No Content endpoints (DELETEs returning noContent()) carry no body —
// emit a bare 204 instead of a 200 { data, meta } so the spec stops
// advertising a response shape these handlers never send.
const successResponse = def.response.success === NoBodyResponse
? { '204': { description: 'No Content' } }
: { '200': { description: 'Success', content: successContent } }
const operationDef: Record<string, unknown> = {
operationId: def.operation,
summary: def.summary,
@@ -343,10 +378,7 @@ export function generateOpenApiSpec(serverUrl: string): OpenApiSpec {
'x-dry-run-supported': def.dryRunSupported,
...(def.scope ? { 'x-required-scope': def.scope } : {}),
responses: {
'200': {
description: 'Success',
content: successContent,
},
...successResponse,
'400': { description: 'Validation error', $ref: '#/components/responses/Error' },
'401': { description: 'Unauthorized', $ref: '#/components/responses/Error' },
'403': { description: 'Insufficient scope', $ref: '#/components/responses/Error' },
+1 -1
View File
@@ -9,7 +9,7 @@ let results: Array<{ data?: unknown; error?: unknown }>
function makeBuilder() {
const b: Record<string, unknown> = {}
for (const m of ['select', 'eq', 'in', 'range']) {
for (const m of ['select', 'eq', 'in', 'order', 'range']) {
b[m] = vi.fn().mockReturnValue(b)
}
b.single = vi.fn().mockImplementation(async () => results[resultIdx++] ?? { data: null, error: null })
@@ -9,7 +9,7 @@ let mockResults: Record<string, MockResult[]>
function makeBuilder(tableName: string) {
const b: Record<string, unknown> = {}
for (const m of ['select', 'eq', 'in', 'lt', 'neq', 'range', 'update']) {
for (const m of ['select', 'eq', 'in', 'lt', 'neq', 'order', 'range', 'update']) {
b[m] = vi.fn().mockReturnValue(b)
}
const consume = (): MockResult => {
@@ -124,6 +124,54 @@ describe('generateGeneralLedger', () => {
expect(acc1930.closing_balance).toBe(1250)
})
it('does not double a balance when an unstable page boundary re-serves a line (#790/#791)', async () => {
// Reproduces the doubling bug's mechanism: a paginated query whose order
// was not stable can return the same journal_entry_line on two pages.
// Page 1 must be a FULL page (PAGE_SIZE rows) so fetchAllRows fetches a
// second page; page 2 re-serves the 5010 line. dedupeBy(line id) must
// collapse it so the single 4000 posting totals 4000, not 8000.
const PAGE_SIZE = 1000
const filler = Array.from({ length: PAGE_SIZE - 1 }, (_, i) => ({
id: `f${i}`,
account_number: '1930',
debit_amount: 0,
credit_amount: 0,
journal_entries: { entry_date: '2024-01-02', voucher_number: 1, voucher_series: 'A', description: 'filler', source_type: 'manual' },
}))
const rentLine = {
id: 'rent-line-1',
account_number: '5010',
debit_amount: 4000,
credit_amount: 0,
journal_entries: { entry_date: '2024-01-15', voucher_number: 2, voucher_series: 'A', description: 'Lokalhyra', source_type: 'manual' },
}
mockResults = {
fiscal_periods: [
{ data: { period_start: '2024-01-01', period_end: '2024-12-31', opening_balance_entry_id: null }, error: null },
],
journal_entry_lines: [
{ data: [...filler, rentLine], error: null }, // page 1 — full → triggers page 2
{ data: [rentLine], error: null }, // page 2 — duplicate of the 5010 line
],
chart_of_accounts: [
{
data: [
{ account_number: '1930', account_name: 'Företagskonto' },
{ account_number: '5010', account_name: 'Lokalhyra' },
],
error: null,
},
],
}
const report = await generateGeneralLedger(supabase, 'company-1', 'period-1')
const acc5010 = report.accounts.find((a) => a.account_number === '5010')!
expect(acc5010.total_debit).toBe(4000) // not 8000
expect(acc5010.lines).toHaveLength(1) // verifikat listed once, not twice
})
it('computes opening balance from prior period entries', async () => {
mockResults = {
fiscal_periods: [
@@ -67,6 +67,54 @@ describe('generateJournalRegister', () => {
expect(report.period).toEqual({ start: '2024-01-01', end: '2024-12-31' })
})
it('does not double an entry when an unstable page boundary re-serves a line (#790/#793)', async () => {
// Page 1 is a FULL page so fetchAllRows fetches page 2, which re-serves
// the 5010 line of voucher 2. dedupeBy(line id) must collapse it so the
// grundbok lists the voucher once with a 4000 (not 8000) total.
const PAGE_SIZE = 1000
const filler = Array.from({ length: PAGE_SIZE - 1 }, (_, i) => ({
id: `f${i}`,
account_number: '1930',
debit_amount: 0,
credit_amount: 0,
journal_entry_id: 'e0',
journal_entries: { id: 'e0', entry_date: '2024-01-02', voucher_number: 1, voucher_series: 'A', description: 'filler', source_type: 'manual', status: 'posted' },
}))
const rentLine = {
id: 'rent-line-1',
account_number: '5010',
debit_amount: 4000,
credit_amount: 0,
journal_entry_id: 'e1',
journal_entries: { id: 'e1', entry_date: '2024-01-15', voucher_number: 2, voucher_series: 'A', description: 'Lokalhyra', source_type: 'manual', status: 'posted' },
}
mockResults = {
fiscal_periods: [
{ data: { period_start: '2024-01-01', period_end: '2024-12-31' }, error: null },
],
journal_entry_lines: [
{ data: [...filler, rentLine], error: null }, // page 1 — full → triggers page 2
{ data: [rentLine], error: null }, // page 2 — duplicate of the 5010 line
],
chart_of_accounts: [
{
data: [
{ account_number: '1930', account_name: 'Företagskonto' },
{ account_number: '5010', account_name: 'Lokalhyra' },
],
error: null,
},
],
}
const report = await generateJournalRegister(supabase, 'company-1', 'period-1')
const voucher2 = report.entries.find((e) => e.voucher_number === 2)!
expect(voucher2.total_debit).toBe(4000) // not 8000
expect(voucher2.lines).toHaveLength(1) // listed once, not twice
})
it('produces entries in registration order with correct totals', async () => {
mockResults = {
fiscal_periods: [
+79 -142
View File
@@ -5,6 +5,20 @@ const { supabase, mockResult } = createMockSupabase()
import { generateMonthlyBreakdown } from '../monthly-breakdown'
// Minimal chainable query mock: every filter/order method returns the same
// object; .single()/.range() resolve to the queued result. Tolerant of
// query-shape changes such as an added .order() (see fetch-all.ts ordering
// invariant) so the tests don't hardcode the exact method chain.
function chain(result: unknown) {
const c: Record<string, unknown> = {}
for (const m of ['select', 'eq', 'in', 'gte', 'lte', 'lt', 'neq', 'order']) {
c[m] = () => c
}
c.single = () => Promise.resolve(result)
c.range = () => Promise.resolve(result)
return c
}
beforeEach(() => {
vi.clearAllMocks()
})
@@ -33,38 +47,9 @@ describe('generateMonthlyBreakdown', () => {
let callCount = 0
supabase.from.mockImplementation(() => {
callCount++
if (callCount === 1) {
// fiscal_periods query
return {
select: () => ({
eq: () => ({
eq: () => ({
single: () =>
Promise.resolve({
data: { period_start: '2024-01-01', period_end: '2024-12-31' },
error: null,
}),
}),
}),
}),
}
}
// journal_entry_lines query
return {
select: () => ({
eq: () => ({
eq: () => ({
eq: () => ({
range: () =>
Promise.resolve({
data: [],
error: null,
}),
}),
}),
}),
}),
}
return callCount === 1
? chain({ data: { period_start: '2024-01-01', period_end: '2024-12-31' }, error: null })
: chain({ data: [], error: null })
})
const result = await generateMonthlyBreakdown(supabase as never, 'company-1', 'period-1')
@@ -79,61 +64,37 @@ describe('generateMonthlyBreakdown', () => {
let callCount = 0
supabase.from.mockImplementation(() => {
callCount++
if (callCount === 1) {
return {
select: () => ({
eq: () => ({
eq: () => ({
single: () =>
Promise.resolve({
data: { period_start: '2024-01-01', period_end: '2024-03-31' },
error: null,
}),
}),
}),
}),
}
}
return {
select: () => ({
eq: () => ({
eq: () => ({
eq: () => ({
range: () =>
Promise.resolve({
data: [
{
account_number: '3001',
debit_amount: 0,
credit_amount: 10000,
journal_entry: { entry_date: '2024-01-15', status: 'posted', user_id: 'user-1', fiscal_period_id: 'period-1' },
},
{
account_number: '5010',
debit_amount: 3000,
credit_amount: 0,
journal_entry: { entry_date: '2024-01-20', status: 'posted', user_id: 'user-1', fiscal_period_id: 'period-1' },
},
{
account_number: '3001',
debit_amount: 0,
credit_amount: 5000,
journal_entry: { entry_date: '2024-02-10', status: 'posted', user_id: 'user-1', fiscal_period_id: 'period-1' },
},
{
account_number: '6200',
debit_amount: 1500,
credit_amount: 0,
journal_entry: { entry_date: '2024-02-15', status: 'posted', user_id: 'user-1', fiscal_period_id: 'period-1' },
},
],
error: null,
}),
}),
}),
}),
}),
}
return callCount === 1
? chain({ data: { period_start: '2024-01-01', period_end: '2024-03-31' }, error: null })
: chain({
data: [
{
account_number: '3001',
debit_amount: 0,
credit_amount: 10000,
journal_entry: { entry_date: '2024-01-15', status: 'posted', user_id: 'user-1', fiscal_period_id: 'period-1' },
},
{
account_number: '5010',
debit_amount: 3000,
credit_amount: 0,
journal_entry: { entry_date: '2024-01-20', status: 'posted', user_id: 'user-1', fiscal_period_id: 'period-1' },
},
{
account_number: '3001',
debit_amount: 0,
credit_amount: 5000,
journal_entry: { entry_date: '2024-02-10', status: 'posted', user_id: 'user-1', fiscal_period_id: 'period-1' },
},
{
account_number: '6200',
debit_amount: 1500,
credit_amount: 0,
journal_entry: { entry_date: '2024-02-15', status: 'posted', user_id: 'user-1', fiscal_period_id: 'period-1' },
},
],
error: null,
})
})
const result = await generateMonthlyBreakdown(supabase as never, 'company-1', 'period-1')
@@ -160,61 +121,37 @@ describe('generateMonthlyBreakdown', () => {
let callCount = 0
supabase.from.mockImplementation(() => {
callCount++
if (callCount === 1) {
return {
select: () => ({
eq: () => ({
eq: () => ({
single: () =>
Promise.resolve({
data: { period_start: '2024-01-01', period_end: '2024-01-31' },
error: null,
}),
}),
}),
}),
}
}
return {
select: () => ({
eq: () => ({
eq: () => ({
eq: () => ({
range: () =>
Promise.resolve({
data: [
{
account_number: '1930',
debit_amount: 10000,
credit_amount: 0,
journal_entry: { entry_date: '2024-01-15', status: 'posted', user_id: 'user-1', fiscal_period_id: 'period-1' },
},
{
account_number: '2611',
debit_amount: 0,
credit_amount: 2500,
journal_entry: { entry_date: '2024-01-15', status: 'posted', user_id: 'user-1', fiscal_period_id: 'period-1' },
},
{
account_number: '8400',
debit_amount: 500,
credit_amount: 0,
journal_entry: { entry_date: '2024-01-20', status: 'posted', user_id: 'user-1', fiscal_period_id: 'period-1' },
},
{
account_number: '8300',
debit_amount: 0,
credit_amount: 200,
journal_entry: { entry_date: '2024-01-25', status: 'posted', user_id: 'user-1', fiscal_period_id: 'period-1' },
},
],
error: null,
}),
}),
}),
}),
}),
}
return callCount === 1
? chain({ data: { period_start: '2024-01-01', period_end: '2024-01-31' }, error: null })
: chain({
data: [
{
account_number: '1930',
debit_amount: 10000,
credit_amount: 0,
journal_entry: { entry_date: '2024-01-15', status: 'posted', user_id: 'user-1', fiscal_period_id: 'period-1' },
},
{
account_number: '2611',
debit_amount: 0,
credit_amount: 2500,
journal_entry: { entry_date: '2024-01-15', status: 'posted', user_id: 'user-1', fiscal_period_id: 'period-1' },
},
{
account_number: '8400',
debit_amount: 500,
credit_amount: 0,
journal_entry: { entry_date: '2024-01-20', status: 'posted', user_id: 'user-1', fiscal_period_id: 'period-1' },
},
{
account_number: '8300',
debit_amount: 0,
credit_amount: 200,
journal_entry: { entry_date: '2024-01-25', status: 'posted', user_id: 'user-1', fiscal_period_id: 'period-1' },
},
],
error: null,
})
})
const result = await generateMonthlyBreakdown(supabase as never, 'company-1', 'period-1')
@@ -9,7 +9,7 @@ let results: Array<{ data?: unknown; error?: unknown }>
function makeBuilder() {
const b: Record<string, unknown> = {}
for (const m of ['select', 'eq', 'in', 'gte', 'lte', 'lt', 'or', 'not', 'range']) {
for (const m of ['select', 'eq', 'in', 'gte', 'lte', 'lt', 'or', 'not', 'order', 'range']) {
b[m] = vi.fn().mockReturnValue(b)
}
b.single = vi.fn().mockImplementation(async () => results[resultIdx++] ?? { data: null, error: null })
@@ -9,7 +9,7 @@ let results: Array<{ data?: unknown; error?: unknown }>
function makeBuilder() {
const b: Record<string, unknown> = {}
for (const m of ['select', 'eq', 'in', 'range']) {
for (const m of ['select', 'eq', 'in', 'order', 'range']) {
b[m] = vi.fn().mockReturnValue(b)
}
b.single = vi.fn().mockImplementation(async () => results[resultIdx++] ?? { data: null, error: null })
+1 -1
View File
@@ -11,7 +11,7 @@ let mockResults: Record<string, MockResult[]>
function makeBuilder(tableName: string) {
const b: Record<string, unknown> = {}
for (const m of ['select', 'eq', 'in', 'lt', 'lte', 'gte', 'neq', 'range']) {
for (const m of ['select', 'eq', 'in', 'lt', 'lte', 'gte', 'neq', 'order', 'range']) {
b[m] = vi.fn().mockReturnValue(b)
}
const consume = (): MockResult => {
@@ -9,7 +9,7 @@ let results: Array<{ data?: unknown; error?: unknown }>
function makeBuilder() {
const b: Record<string, unknown> = {}
for (const m of ['select', 'eq', 'in', 'gte', 'lte', 'lt', 'or', 'not', 'range']) {
for (const m of ['select', 'eq', 'in', 'gte', 'lte', 'lt', 'or', 'not', 'order', 'range']) {
b[m] = vi.fn().mockReturnValue(b)
}
b.single = vi.fn().mockImplementation(async () => results[resultIdx++] ?? { data: null, error: null })
+2
View File
@@ -69,6 +69,8 @@ export async function generateARLedger(
.select('*, customer:customers(id, name)')
.eq('company_id', companyId)
.in('status', ['sent', 'overdue', 'credited'])
// Stable total order for correct paging (see fetch-all.ts).
.order('id', { ascending: true })
.range(from, to)
)
} catch {
+7
View File
@@ -209,6 +209,8 @@ export async function estimateArchiveSize(
.eq('company_id', companyId)
.eq('fiscal_period_id', periodId)
.in('status', ['posted', 'reversed'])
// Stable total order for correct paging (see fetch-all.ts).
.order('id', { ascending: true })
.range(from, to)
)
const ids = periodEntryIds.map((e) => e.id)
@@ -345,6 +347,8 @@ async function writeDocuments(
)
.eq('company_id', companyId)
.not('journal_entry_id', 'is', null)
// Stable total order for correct paging (see fetch-all.ts).
.order('id', { ascending: true })
.range(from, to)
)
@@ -730,6 +734,9 @@ async function buildEntryToPeriodMap(
query = query.in('fiscal_period_id', periodIds)
}
// Stable total order for correct paging (see fetch-all.ts).
query = query.order('id', { ascending: true })
const entries = await fetchAllRows<{ id: string; fiscal_period_id: string }>(({ from, to }) =>
query.range(from, to)
)
+9 -3
View File
@@ -83,6 +83,7 @@ export async function generateGeneralLedger(
// Supabase types !inner joins as arrays; for many-to-one (line → entry)
// it returns a single object at runtime. Cast via `as any` on the query.
const rawLines = await fetchAllRows<{
id: string
account_number: string
debit_amount: number
credit_amount: number
@@ -97,7 +98,7 @@ export async function generateGeneralLedger(
}>(({ from, to }) => {
let query = supabase
.from('journal_entry_lines')
.select('account_number, debit_amount, credit_amount, journal_entry_id, journal_entries!inner(entry_date, voucher_number, voucher_series, description, source_type, company_id, fiscal_period_id, status)')
.select('id, account_number, debit_amount, credit_amount, journal_entry_id, journal_entries!inner(entry_date, voucher_number, voucher_series, description, source_type, company_id, fiscal_period_id, status)')
.eq('journal_entries.company_id', companyId)
.eq('journal_entries.fiscal_period_id', periodId)
.in('journal_entries.status', ['posted', 'reversed'])
@@ -106,9 +107,13 @@ export async function generateGeneralLedger(
query = query.neq('journal_entry_id', obEntryId)
}
// Stable total order on the line PK — paging is only correct with a
// deterministic order, else rows duplicate/skip across pages and balances
// double or accounts vanish (see fetch-all.ts ordering invariant). The
// report re-sorts lines per account below, so this order is invisible.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return query.range(from, to) as any
})
return query.order('id', { ascending: true }).range(from, to) as any
}, { dedupeBy: (r) => r.id })
if (rawLines.length === 0 && openingBalances.size === 0) {
return { accounts: [], period: { start: period.period_start, end: period.period_end } }
@@ -120,6 +125,7 @@ export async function generateGeneralLedger(
.from('chart_of_accounts')
.select('account_number, account_name')
.eq('company_id', companyId)
.order('account_number', { ascending: true })
.range(from, to)
)
+1
View File
@@ -745,6 +745,7 @@ export async function generateINK2Declaration(
.from('chart_of_accounts')
.select('account_number, account_name')
.eq('company_id', companyId)
.order('account_number', { ascending: true })
.range(from, to)
)
+9 -5
View File
@@ -62,6 +62,7 @@ export async function generateJournalRegister(
// Supabase types !inner joins as arrays; for many-to-one (line → entry)
// it returns a single object at runtime. Cast via `as any` on the query.
const rawLines = await fetchAllRows<{
id: string
account_number: string
debit_amount: number
credit_amount: number
@@ -75,16 +76,18 @@ export async function generateJournalRegister(
source_type: string
status: string
}
}>(({ from, to }) =>
supabase
}>(({ from, to }) => {
return supabase
.from('journal_entry_lines')
.select('account_number, debit_amount, credit_amount, journal_entry_id, journal_entries!inner(id, entry_date, voucher_number, voucher_series, description, source_type, status, company_id, fiscal_period_id)')
.select('id, account_number, debit_amount, credit_amount, journal_entry_id, journal_entries!inner(id, entry_date, voucher_number, voucher_series, description, source_type, status, company_id, fiscal_period_id)')
.eq('journal_entries.company_id', companyId)
.eq('journal_entries.fiscal_period_id', periodId)
.in('journal_entries.status', ['posted', 'reversed'])
// Stable total order on the line PK — without it, rows duplicate/skip
// across pages and entries appear twice or go missing (see fetch-all.ts).
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.range(from, to) as any
)
.order('id', { ascending: true }).range(from, to) as any
}, { dedupeBy: (r) => r.id })
if (rawLines.length === 0) {
return { entries: [], total_entries: 0, total_debit: 0, total_credit: 0, period: { start: period.period_start, end: period.period_end } }
@@ -96,6 +99,7 @@ export async function generateJournalRegister(
.from('chart_of_accounts')
.select('account_number, account_name')
.eq('company_id', companyId)
.order('account_number', { ascending: true })
.range(from, to)
)
+2
View File
@@ -63,6 +63,8 @@ export async function generateMonthlyBreakdown(
.eq('journal_entries.fiscal_period_id', fiscalPeriodId)
.eq('journal_entries.company_id', companyId)
.eq('journal_entries.status', 'posted')
// Stable total order for correct paging (see fetch-all.ts).
.order('id', { ascending: true })
.range(from, to)
)
} catch {
+1
View File
@@ -213,6 +213,7 @@ export async function generateNEDeclaration(
.from('chart_of_accounts')
.select('account_number, account_name')
.eq('company_id', companyId)
.order('account_number', { ascending: true })
.range(from, to)
)
+6 -2
View File
@@ -41,16 +41,20 @@ export async function getOpeningBalances(
// for consistency (avoids silent truncation) and joins journal_entries
// to enforce company_id ownership (defense in depth alongside RLS).
const obLines = await fetchAllRows<{
id: string
account_number: string
debit_amount: number
credit_amount: number
}>(({ from, to }) =>
supabase
.from('journal_entry_lines')
.select('account_number, debit_amount, credit_amount, journal_entries!inner(company_id)')
.select('id, account_number, debit_amount, credit_amount, journal_entries!inner(company_id)')
.eq('journal_entry_id', obEntryId)
.eq('journal_entries.company_id', companyId)
.range(from, to)
// Stable total order for correct paging (see fetch-all.ts).
.order('id', { ascending: true })
.range(from, to),
{ dedupeBy: (r) => r.id }
)
for (const line of obLines) {
+4
View File
@@ -200,6 +200,8 @@ export async function generatePeriodiskSammanstallning(
.in('journal_entries.source_type', ['invoice_created', 'credit_note'])
.gte('journal_entries.entry_date', start)
.lte('journal_entries.entry_date', end)
// Stable total order for correct paging (see fetch-all.ts).
.order('id', { ascending: true })
.range(from, to) as unknown as PromiseLike<{ data: RawLine[] | null; error: { message: string } | null }>,
)
@@ -229,6 +231,8 @@ export async function generatePeriodiskSammanstallning(
)
`)
.in('id', invoiceIds)
// Stable total order for correct paging (see fetch-all.ts).
.order('id', { ascending: true })
.range(from, to) as unknown as PromiseLike<{ data: RawInvoice[] | null; error: { message: string } | null }>,
)
for (const inv of invoices) invoiceMap.set(inv.id, inv)
+7 -1
View File
@@ -90,6 +90,7 @@ function pickEntry(row: RcLineRow): EntryFields | null {
}
interface SiblingLineRow {
id: string
journal_entry_id: string
account_number: string
debit_amount: number
@@ -122,6 +123,8 @@ export async function findRcBasisGaps(
.eq('journal_entries.status', 'posted')
.gte('journal_entries.entry_date', start)
.lte('journal_entries.entry_date', end)
// Stable total order for correct paging (see fetch-all.ts).
.order('id', { ascending: true })
.range(from, to),
)) as RcLineRow[]
@@ -132,9 +135,12 @@ export async function findRcBasisGaps(
const siblingLines = await fetchAllRows<SiblingLineRow>(({ from, to }) =>
supabase
.from('journal_entry_lines')
.select('journal_entry_id, account_number, debit_amount, credit_amount')
.select('id, journal_entry_id, account_number, debit_amount, credit_amount')
.in('journal_entry_id', entryIds)
// Stable total order for correct paging (see fetch-all.ts).
.order('id', { ascending: true })
.range(from, to),
{ dedupeBy: (r) => r.id },
)
const basisByEntry = new Map<string, number>()
+3
View File
@@ -73,6 +73,9 @@ export async function generateSalaryJournal(
.eq('salary_runs.period_year', year)
.eq('salary_runs.status', 'booked')
.order('created_at')
// id tiebreaker — created_at is not unique, so it alone is not a stable
// total order for paging (see fetch-all.ts).
.order('id', { ascending: true })
.range(from, to)
)
+2
View File
@@ -47,6 +47,8 @@ export async function generateSupplierLedger(
.select('*, supplier:suppliers(id, name)')
.eq('company_id', companyId)
.in('status', ['registered', 'approved', 'partially_paid', 'overdue'])
// Stable total order for correct paging (see fetch-all.ts).
.order('id', { ascending: true })
.range(from, to)
)
} catch {
+11 -6
View File
@@ -60,13 +60,14 @@ export async function generateTrialBalance(
options.fromDate > period.period_start
) {
const priorLines = await fetchAllRows<{
id: string
account_number: string
debit_amount: number
credit_amount: number
}>(({ from, to }) => {
let query = supabase
.from('journal_entry_lines')
.select('account_number, debit_amount, credit_amount, journal_entries!inner(company_id, fiscal_period_id, status, source_type, entry_date)')
.select('id, account_number, debit_amount, credit_amount, journal_entries!inner(company_id, fiscal_period_id, status, source_type, entry_date)')
.eq('journal_entries.company_id', companyId)
.eq('journal_entries.fiscal_period_id', fiscalPeriodId)
.in('journal_entries.status', ['posted', 'reversed'])
@@ -81,8 +82,9 @@ export async function generateTrialBalance(
query = query.neq('journal_entries.source_type', 'year_end')
}
return query.range(from, to)
})
// Stable total order on the line PK for correct paging (see fetch-all.ts).
return query.order('id', { ascending: true }).range(from, to)
}, { dedupeBy: (r) => r.id })
for (const line of priorLines) {
const existing = openingBalances.get(line.account_number) || { debit: 0, credit: 0 }
@@ -100,13 +102,14 @@ export async function generateTrialBalance(
// be missed from both IB and period. The window is sub-second and the
// consequence is a single stale report — acceptable.
const lines = await fetchAllRows<{
id: string
account_number: string
debit_amount: number
credit_amount: number
}>(({ from, to }) => {
let query = supabase
.from('journal_entry_lines')
.select('account_number, debit_amount, credit_amount, journal_entries!inner(company_id, fiscal_period_id, status, source_type, entry_date)')
.select('id, account_number, debit_amount, credit_amount, journal_entries!inner(company_id, fiscal_period_id, status, source_type, entry_date)')
.eq('journal_entries.company_id', companyId)
.eq('journal_entries.fiscal_period_id', fiscalPeriodId)
.in('journal_entries.status', ['posted', 'reversed'])
@@ -132,8 +135,9 @@ export async function generateTrialBalance(
query = query.neq('journal_entries.source_type', 'year_end')
}
return query.range(from, to)
})
// Stable total order on the line PK for correct paging (see fetch-all.ts).
return query.order('id', { ascending: true }).range(from, to)
}, { dedupeBy: (r) => r.id })
if (lines.length === 0 && openingBalances.size === 0) {
return { rows: [], totalDebit: 0, totalCredit: 0, isBalanced: true }
@@ -149,6 +153,7 @@ export async function generateTrialBalance(
.from('chart_of_accounts')
.select('account_number, account_name, account_class')
.eq('company_id', companyId)
.order('account_number', { ascending: true })
.range(from, to)
)
+5
View File
@@ -61,6 +61,9 @@ export async function generateVacationLiability(
.eq('is_active', true)
.not('vacation_rule', 'in', '(none,semesterersattning)')
.order('last_name')
// id tiebreaker — last_name is not unique, so it alone is not a stable
// total order for paging (see fetch-all.ts).
.order('id', { ascending: true })
.range(from, to)
)
@@ -80,6 +83,8 @@ export async function generateVacationLiability(
.eq('company_id', companyId)
.eq('salary_runs.period_year', year)
.eq('salary_runs.status', 'booked')
// Stable total order for correct paging (see fetch-all.ts).
.order('id', { ascending: true })
.range(from, to)
)

Some files were not shown because too many files have changed in this diff Show More