Skip to content

feat(printing): add versioned semantic merchant print template model #447

Closed

Description

@khaira777

Refs: #438
Depends on: #442
Can parallel with: E, F, G (schema work only; UI lands after D)
Related: #284

Context

Merchants eventually need custom receipt layouts. Today the only extensibility is compliance templates in installed_print_templates — rows that exist solely as denormalized children of signed country-pack artifacts (Ed25519-verified at install, re-verified at activation, revocation-aware reads). That table must never become generic storage for tenant JSON: its trust model depends on every row tracing to a verified pack version.

This issue establishes the renderer-independent semantic merchant template model: versioned storage, provenance, validation, and selection identity — built on PrintDocument block vocabulary, deliberately independent of HTML/ESC-POS specifics so one template can feed every renderer.

Architectural Decisions Relevant to This Issue

  • Four provenance classes stay distinct: core / compliance-pack / merchant-created / imported.
  • Merchant schema describes semantic structure (ordered blocks from the PrintDocument v1 vocabulary + presentation options like visibility/order/label-variants), never printer commands or HTML.
  • Compliance cloning: derivedFrom may reference a pack template id for user information; no compliance trust transfers — required legal blocks in compliance templates are enforced by the compliance system itself, and merchant copies are ordinary editable documents (with a documented warning when derived from a tax-invoice template).
  • Selection identity is structured, not concatenated strings: bill_template migrates to structured JSON { source: 'core' | 'pack' | 'merchant', id } (pack ids remain globally unique today — template_id is the table PK — but persisted semantics should not rely on that accident).
  • Schema is versioned from day one with explicit compatibility policy (this becomes a public-facing contract per epic principle; stable semantic field names; internal i18n keys never exposed).

Scope / What to do

  1. DB migration: merchant_print_templates table — id (uuid), business_id scoping consistent with multi-tenant patterns used elsewhere, name, origin (created|imported|cloned), derived_from nullable structured ref, document_type (receipt v1), schema_version, payload_json, status (draft|active|archived), previous_payload_json (single-step rollback), checksum, audit timestamps/actor columns.
  2. Validation layer: schema-version gate (fail-closed unknown major versions), allowed-blocks whitelist matching PrintDocument v1, property whitelists per block, size cap (~256 KB payload), unknown-field policy = reject on write/import (stricter than render-time tolerance elsewhere).
  3. Selection identity refactor: settings reader/writer accepts legacy bare values (classic, compact, <pack-template-id>) and upgrades transparently on next save; isAvailableBillTemplate extended to merchant source; template-picker UI lists core/pack/merchant with provenance badges.
  4. CRUD API (owner role): create/draft → activate → archive with rollback-to-previous; render path resolves merchant templates through the document pipeline (works because D made classic document-driven; compact/KOT rendering of merchant docs follows their owning issues if not yet migrated).
  5. Golden fixtures: sample JSON template exercising each block type; negative fixtures per validation rule.

Compatibility / Migration

  • One-time settings migration for bill_template values (idempotent; legacy strings keep resolving during transition).
  • Pack/compliance flows untouched; no change to installed_print_templates.

What NOT to do

  • Do NOT store anything in installed_print_templates.
  • Do NOT base the merchant schema on ESC-POS tokens/commands or embed renderer snippets.
  • Do NOT expose internal translation keys as template fields (stable semantic label identifiers only).
  • Do NOT implement file import/export UX here (feat(printing): add validated offline import/export for merchant templates #448) — this issue defines the model those use.
  • Do NOT add a visual editor (future epic).

Acceptance Criteria

  • Migration creates the table; fresh-install schema matches (both paths tested per repo migration conventions).
  • CRUD lifecycle works: draft→active→archive→rollback with checksum verification.
  • Structured selection identity live; legacy values auto-upgrade; pack/core resolution unchanged.
  • Validation rejects: unknown schema major, unknown blocks/fields, oversized payloads, non-object roots — with actionable errors.
  • A merchant receipt renders via document pipeline identically across available renderers (parity harness extended with a merchant-template mode).
  • Provenance badges visible in picker; cloned-from-compliance shows informational origin without trust claims.

Verification

npm run build && npm run lint && npm run audit:db
npx ts-node --transpile-only -P tests/tsconfig.json tests/<merchant-template-tests>.test.ts
npx ts-node --transpile-only -P tests/tsconfig.json tests/print-parity.test.ts

Documentation

Schema + trust/provenance section drafted in PR (docs issue #449 finalizes placement).

Activity

  1. added
    enhancement
    area:frontend
    area:backend
    on Aug 21, 2026
  2. added a commit that references this issue on Aug 23, 2026
  3. self-assigned this
    on Aug 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:backendarea:frontendenhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions