feat(erp): always-on ERP link, usage events, outbox, CLI licences #25

Merged
sharang merged 3 commits from feat/m-mig-7-erp-link into main 2026-09-12 08:35:14 +00:00
Owner

M-MIG-7 — the always-on customer ⇄ CRM ⇄ ERP link (ERP/02; decisions 3, 12, 13, 18). Stacked on #23 (base branch = feat/m-mig-5-products-entitlements); retarget to main once #23 is merged.

Schema (migration 0004)

subscriptions (tenant × SKU × Sales Order), usage_events (unique idempotency_key, erp_synced_at), erp_outbox (retry/backoff), erp_webhook_receipts (idempotent inbound), licences (revocation + heartbeat), tenants.status_changed_at (trigger) for the time-based ladder.

ERP link

  • Client internal/erp: Frappe REST (Authorization: token key:secret) — Customer upsert keyed by the custom field tenant_id, Subscription list, Usage Record, Lead. Webhook HMAC signing/verification (X-Frappe-Webhook-Signature base64 or X-Hub-Signature-256).
  • Outbox worker: tenant create → customer.upsert (writes back erp_customer_id), usage event → usage.record, catalog request → lead.create; backoff 1 m / 5 m / 15 m / 1 h; idle when ERP_BASE_URL is unset (rows accumulate).
  • Webhook receiver POST /v1/internal/erp/webhook (PUBLIC_EXPLICIT, HMAC, idempotent by event id): sales_order.submit → subscriptions + tenant_products (SKU → product, -STD/-BUS/-ENT → plan, add-on SKUs → limits such as runners) + active; sales_invoice.overdue → warning / restricted (≥ ERP_RESTRICT_AFTER_DAYS) / halted (≥ ERP_HALT_AFTER_DAYS), never moving a tenant back up; payment_entry.submit → active (only with an active subscription); subscription.cancel → halted, ERP-sourced entitlements disabled (trials untouched).
  • Reconcile (every ERP_RECONCILE_INTERVAL, also POST /v1/internal/erp/reconcile): mirrors ERP Subscriptions per customer (safety net for lost webhooks) and applies the time-based steps — trial expiry → restricted, warning → restricted after 14 d, restricted → halted at 30 d, halted → archived after the export window. Every change syncs tenant_status to Keycloak.
  • Usage POST /v1/usage/events (one / array / {events}; ≤ 500; user tokens scoped to their tenant), GET /v1/usage/events, GET /v1/subscriptions, GET /v1/internal/erp/status.

ERPNext mock (staging, decision 18)

cmd/erpmock (also shipped in the image as /erpmock): /api/resource/{doctype} list/create/get/update/delete with filters, seeded Items + Subscription Plans for the Phase-1 SKUs, GET /_mock/requests + /_mock/deliveries for test assertions, POST /_mock/fire/{sales-order-submit|invoice-overdue|payment-entry|subscription-cancel} which mutate the docs and deliver the signed webhook to ERP_WEBHOOK_URL. Deployed as erpnext-mock-stage (orca-platform#10).

CLI licences (decision 13)

Ed25519 signer (LICENCE_SIGNING_KEY base64 seed; ephemeral + warning when unset). POST /v1/licences (entitlement-gated; user tokens from the device-flow clients get their own tenant; expiry = min(TTL, entitlement expiry)), GET /.well-known/licence-keys.json (JWKS, OKP), POST /v1/licences/heartbeat (PUBLIC_EXPLICIT — the JWT is the credential; ok | warning | degraded | hard_stop | revoked from the tenant ladder + the token's grace policy 7/14/30 d; renewal past half-life), GET /v1/licences, POST /v1/licences/{id}/revoke.

authn.Principal now carries the tenant-context claims (tenant_id, tenant_slug, tenant_status, email).

Tests

Both stores: webhook ladder end-to-end, usage idempotency + outbox, outbox drained against the real erpmock (customer link, usage synced, lead; fired webhooks round-trip; reconcile picks up an order created only in ERP), ladder escalation with an injected clock, licence issue/heartbeat/renew/revoke. Unit tests for erpmock, erp, licence. CI package set coverage: 73.2 %.

Follow-ups (separate PRs)

Portal billing page + status banner; Firmwerk cloud-server / CLI licence client; Werkpilot licence client; Prüfwerk usage emission; ERP/03 live-ERP configuration (prod).

🤖 Generated with Claude Code

https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB

**M-MIG-7** — the always-on customer ⇄ CRM ⇄ ERP link (ERP/02; decisions 3, 12, 13, 18). Stacked on #23 (base branch = `feat/m-mig-5-products-entitlements`); retarget to `main` once #23 is merged. ## Schema (migration 0004) `subscriptions` (tenant × SKU × Sales Order), `usage_events` (unique `idempotency_key`, `erp_synced_at`), `erp_outbox` (retry/backoff), `erp_webhook_receipts` (idempotent inbound), `licences` (revocation + heartbeat), `tenants.status_changed_at` (trigger) for the time-based ladder. ## ERP link - **Client** `internal/erp`: Frappe REST (`Authorization: token key:secret`) — Customer upsert keyed by the custom field `tenant_id`, Subscription list, Usage Record, Lead. Webhook HMAC signing/verification (`X-Frappe-Webhook-Signature` base64 or `X-Hub-Signature-256`). - **Outbox worker**: tenant create → `customer.upsert` (writes back `erp_customer_id`), usage event → `usage.record`, catalog request → `lead.create`; backoff 1 m / 5 m / 15 m / 1 h; idle when `ERP_BASE_URL` is unset (rows accumulate). - **Webhook receiver** `POST /v1/internal/erp/webhook` (PUBLIC_EXPLICIT, HMAC, idempotent by event id): `sales_order.submit` → subscriptions + `tenant_products` (SKU → product, `-STD/-BUS/-ENT` → plan, add-on SKUs → limits such as `runners`) + `active`; `sales_invoice.overdue` → `warning` / `restricted` (≥ `ERP_RESTRICT_AFTER_DAYS`) / `halted` (≥ `ERP_HALT_AFTER_DAYS`), never moving a tenant back up; `payment_entry.submit` → `active` (only with an active subscription); `subscription.cancel` → `halted`, ERP-sourced entitlements disabled (trials untouched). - **Reconcile** (every `ERP_RECONCILE_INTERVAL`, also `POST /v1/internal/erp/reconcile`): mirrors ERP Subscriptions per customer (safety net for lost webhooks) and applies the time-based steps — trial expiry → `restricted`, `warning` → `restricted` after 14 d, `restricted` → `halted` at 30 d, `halted` → `archived` after the export window. Every change syncs `tenant_status` to Keycloak. - **Usage** `POST /v1/usage/events` (one / array / `{events}`; ≤ 500; user tokens scoped to their tenant), `GET /v1/usage/events`, `GET /v1/subscriptions`, `GET /v1/internal/erp/status`. ## ERPNext mock (staging, decision 18) `cmd/erpmock` (also shipped in the image as `/erpmock`): `/api/resource/{doctype}` list/create/get/update/delete with filters, seeded Items + Subscription Plans for the Phase-1 SKUs, `GET /_mock/requests` + `/_mock/deliveries` for test assertions, `POST /_mock/fire/{sales-order-submit|invoice-overdue|payment-entry|subscription-cancel}` which mutate the docs and deliver the signed webhook to `ERP_WEBHOOK_URL`. Deployed as `erpnext-mock-stage` (orca-platform#10). ## CLI licences (decision 13) Ed25519 signer (`LICENCE_SIGNING_KEY` base64 seed; ephemeral + warning when unset). `POST /v1/licences` (entitlement-gated; user tokens from the device-flow clients get their own tenant; expiry = min(TTL, entitlement expiry)), `GET /.well-known/licence-keys.json` (JWKS, OKP), `POST /v1/licences/heartbeat` (PUBLIC_EXPLICIT — the JWT is the credential; `ok | warning | degraded | hard_stop | revoked` from the tenant ladder + the token's grace policy 7/14/30 d; renewal past half-life), `GET /v1/licences`, `POST /v1/licences/{id}/revoke`. `authn.Principal` now carries the tenant-context claims (`tenant_id`, `tenant_slug`, `tenant_status`, `email`). ## Tests Both stores: webhook ladder end-to-end, usage idempotency + outbox, **outbox drained against the real erpmock** (customer link, usage synced, lead; fired webhooks round-trip; reconcile picks up an order created only in ERP), ladder escalation with an injected clock, licence issue/heartbeat/renew/revoke. Unit tests for `erpmock`, `erp`, `licence`. CI package set coverage: 73.2 %. ## Follow-ups (separate PRs) Portal billing page + status banner; Firmwerk cloud-server / CLI licence client; Werkpilot licence client; Prüfwerk usage emission; ERP/03 live-ERP configuration (prod). 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
CODEOWNERS rules requested review from Benjamin_Boenisch 2026-09-11 21:44:59 +00:00
sharang changed target branch from feat/m-mig-5-products-entitlements to main 2026-09-12 07:38:49 +00:00
sharang added 1 commit 2026-09-12 07:45:11 +00:00
feat(erp): always-on ERP link, usage events, outbox, CLI licences
ci / shared (pull_request) Successful in 12s
ci / test (pull_request) Failing after 14m32s
ci / image (pull_request) Skipped
7b02cd1c21
M-MIG-7 (ERP/02, decisions 3, 12, 13, 18):

- migration 0004: subscriptions, usage_events, erp_outbox,
  erp_webhook_receipts, licences; tenants.status_changed_at (trigger)
- internal/erp: Frappe REST client (Customer upsert by tenant_id,
  Subscription list, Usage Record, Lead) + webhook signing/verification
- internal/erpmock + cmd/erpmock: ERPNext stand-in for staging —
  /api/resource/{doctype} with token auth, request log, /_mock/fire/*
  triggers that mutate docs and deliver signed webhooks
- POST /v1/internal/erp/webhook (HMAC, idempotent by event id):
  sales_order.submit → subscriptions + entitlements (SKU → product/plan,
  add-on SKUs → limits) + active; sales_invoice.overdue → warning /
  restricted / halted by days overdue; payment_entry.submit → active;
  subscription.cancel → halted
- outbox worker (customer.upsert on tenant create, usage.record,
  lead.create on catalog request) with backoff; reconcile job mirrors
  ERP subscriptions and applies the time-based ladder (trial expiry,
  warning→restricted 14 d, restricted→halted 30 d, halted→archived 30 d)
- POST/GET /v1/usage/events (idempotency_key, batch), GET
  /v1/subscriptions, GET /v1/internal/erp/status, POST
  /v1/internal/erp/reconcile
- CLI licences: Ed25519 signer (LICENCE_SIGNING_KEY), POST /v1/licences
  (entitlement-gated, expiry capped by the entitlement), GET
  /.well-known/licence-keys.json, POST /v1/licences/heartbeat (grace
  ladder, renewal past half-life, revoked), revoke/list
- authn Principal carries tenant-context claims (user tokens scope usage
  and licence calls to their own tenant); authz groups usage(.read)
- tests for every path (both stores), erpmock, erp client, licence pkg;
  openapi.yaml + README updated; Dockerfile ships /erpmock

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
sharang force-pushed feat/m-mig-7-erp-link from a7d7e22994 to 7b02cd1c21 2026-09-12 07:45:11 +00:00 Compare
sharang added 1 commit 2026-09-12 08:06:48 +00:00
chore(lint): drop unused outboxSelect
ci / shared (pull_request) Successful in 14s
ci / test (pull_request) Successful in 22m6s
ci / image (pull_request) Skipped
fafec29cc4
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
sharang added 1 commit 2026-09-12 08:10:02 +00:00
feat(server): MIGRATE_ON_START; image CMD instead of ENTRYPOINT
ci / shared (pull_request) Successful in 13s
ci / test (pull_request) Successful in 22m5s
ci / image (pull_request) Skipped
0cb0883846
orca's `cmd` sets the container CMD, so with an ENTRYPOINT the stage
service ran `/tenant-registry /bin/sh -c …` (no migrations) and the
erpmock service ran the registry. The image now has CMD only and the
server applies embedded migrations itself when MIGRATE_ON_START=true.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
sharang merged commit 209524e5ac into main 2026-09-12 08:35:14 +00:00
sharang deleted branch feat/m-mig-7-erp-link 2026-09-12 08:35:14 +00:00
Sign in to join this conversation.