Initial Commit

This commit is contained in:
2026-05-27 23:29:53 +01:00
commit fbc70f96e6
10000 changed files with 1102152 additions and 0 deletions

View File

@@ -0,0 +1,82 @@
# OpenSpec Change: enterprise-api-plugin-subscription
- **Change ID**: enterprise-api-plugin-subscription
- **Package**: `@suaveplan/api-plugin-subscription`
- **Category**: api/commerce
- **Target path**: `api/commerce/api-plugin-subscription`
- **Status**: PROPOSED
- **Date**: 2026-04-24
- **Reference spec**: `/mnt/s/web/suaveplan/enterprise/api-plugin-subscription.md`
---
## Summary
Deliver `@suaveplan/api-plugin-subscription` on the genesis branch from scratch at enterprise quality. Recurring billing plugin: plan management (monthly/annual/custom intervals), subscription lifecycle (create/upgrade/downgrade/cancel/reactivate), proration calculations, invoice generation, trial periods, dunning (failed payment retry with configurable schedule), and usage-based metering hooks. Bridges to `@suaveplan/api-plugin-payment` for charge execution. Routes under `/subscriptions`. Per-table SurrealQL. Typed events: `subscription:created`, `subscription:renewed`, `subscription:cancelled`, `subscription:payment:failed`.
---
## Available workspace deps on genesis
| Package | Role |
|---|---|
| `@suaveplan/api` (`workspace:^`) | `definePlugin` |
| `@suaveplan/api-shared` (`workspace:^`) | Helpers |
| `@suaveplan/api-plugin-auth` (`workspace:^`) | `requireAuth` |
| `@suaveplan/api-plugin-database` (`workspace:^`) | SurrealDB |
| `@suaveplan/api-plugin-payment` (`workspace:^`) | Charge execution |
| `@suaveplan/events` (`workspace:^`) | Event bus |
| `@suaveplan/retry` (`workspace:^`) | Dunning retry strategy |
| `@suaveplan/error` (`workspace:^`) | `SubscriptionError` hierarchy |
| `@suaveplan/idempotency` (`workspace:^`) | Mutation guard |
| `@suaveplan/audit-logging` (`workspace:^`) | Billing audit |
| `@suaveplan/rate-limiter` (`workspace:^`) | Rate limits |
| `hono` + `@hono/zod-openapi` + `zod` | Routes |
| `@opentelemetry/api` | OTel |
---
## Public API surface
```ts
function createSubscriptionPlugin(config?: SubscriptionPluginConfig): PluginDescriptor
// Services: PlanService, SubscriptionService, InvoiceService, DunningService, MeteringService
// Routes: /subscriptions/plans, /subscriptions, /subscriptions/:id,
// /subscriptions/:id/cancel, /subscriptions/:id/reactivate,
// /subscriptions/:id/usage, /subscriptions/:id/invoices
```
### Error codes
```ts
type SubscriptionErrorCode =
| "SUBSCRIPTION_PLAN_NOT_FOUND"
| "SUBSCRIPTION_NOT_FOUND"
| "SUBSCRIPTION_ALREADY_CANCELLED"
| "SUBSCRIPTION_IN_TRIAL"
| "SUBSCRIPTION_PAYMENT_FAILED"
| "SUBSCRIPTION_DOWNGRADE_RESTRICTED"
```
---
## File layout
```text
api/commerce/api-plugin-subscription/
package.json tsconfig.json vitest.config.ts
surql/ (plan, subscription, invoice, invoice_line, metering_record, dunning_attempt tables)
src/
index.ts events.ts types/ errors/ schemas/
services/plan/ services/subscription/ services/invoice/
services/dunning/ services/metering/
routes/plans/ routes/subscriptions/
tests/integration.test.ts
```
---
## Impact
Prerequisites: `@suaveplan/api`, `@suaveplan/api-plugin-auth`, `@suaveplan/api-plugin-database`, `@suaveplan/api-plugin-payment`, `@suaveplan/retry`.

View File

@@ -0,0 +1,61 @@
# Tasks: enterprise-api-plugin-subscription
**Change ID**: enterprise-api-plugin-subscription
**Package**: `@suaveplan/api-plugin-subscription`
**Target path**: `api/commerce/api-plugin-subscription`
**Prerequisite packages on Verdaccio**: `@suaveplan/api`, `@suaveplan/api-shared`, `@suaveplan/api-plugin-auth`, `@suaveplan/api-plugin-database`, `@suaveplan/api-plugin-payment`, `@suaveplan/retry`, `@suaveplan/events`, `@suaveplan/idempotency`, `@suaveplan/audit-logging`, `@suaveplan/rate-limiter`, `@suaveplan/error`
---
## 0. Pre-flight (non-negotiable)
- [ ] 0.1 Confirm clean working tree (`git status` reports nothing) and no in-flight worktrees with uncommitted work (`git worktree list` then `git status` in each)
- [ ] 0.2 If any worktrees exist under `.claude/worktrees/`, account for each one: still-active agent, work-pending-merge, or stale-and-prune. Stale worktrees MUST be removed (`git worktree remove`) before opening a new one — code that has been lost twice on main is the failure mode this guards against.
- [ ] 0.3 Confirm the package set in `MANIFEST.md` matches `find packages -maxdepth 3 -name package.json` count. Drift here means a prior change archived without regenerating the manifest.
## 1. Scaffold + types + errors + events
- [ ] Create `api/commerce/api-plugin-subscription/`; `package.json` v0.0.0; standard tooling.
- [ ] Types: `Plan`, `Subscription`, `Invoice`, `InvoiceLine`, `MeteringRecord`, `DunningAttempt`, `SubscriptionPluginConfig` (Zod).
- [ ] Errors: 6-code union + metadata + registration. Test + doc.
- [ ] Events: `SubscriptionEventMap` (subscription:created, subscription:renewed, subscription:cancelled, subscription:payment:failed, subscription:trial:ended).
---
## 2. SurrealQL
- [ ] Per-table files: `plan.surql`, `subscription.surql`, `invoice.surql`, `invoice_line.surql`, `metering_record.surql`, `dunning_attempt.surql`. All with tenantId indexes.
---
## 3. Services (each `.test.ts` + `.md`)
- [ ] **PlanService**: `createPlan`, `updatePlan`, `archivePlan`, `listPlans`. Plans define interval (monthly/annual/custom days), price, trial days, feature flags.
- [ ] **SubscriptionService**: `createSubscription`, `upgrade`, `downgrade` (proration calc), `cancel` (immediate or end-of-period), `reactivate`. Emit typed events; audit-log every state change.
- [ ] **InvoiceService**: `generateInvoice`, `markPaid`, `listInvoices`. Prorations computed as line items.
- [ ] **DunningService**: retry failed payments via `@suaveplan/retry` with configurable backoff schedule; emit `subscription:payment:failed` after all attempts exhausted.
- [ ] **MeteringService**: `recordUsage`, `getUsageSummary` — hook point for usage-based billing.
---
## 4. Routes
- [ ] `GET/POST /subscriptions/plans`, `PATCH/DELETE /subscriptions/plans/:id` — admin-gated.
- [ ] `POST /subscriptions` — `requireAuth` + idempotency + rate-limit + audit.
- [ ] `GET /subscriptions`, `GET /subscriptions/:id` — `requireAuth`.
- [ ] `POST /subscriptions/:id/cancel`, `reactivate`, `upgrade`, `downgrade` — `requireAuth` + audit.
- [ ] `POST /subscriptions/:id/usage` — `requireAuth` + idempotency.
- [ ] `GET /subscriptions/:id/invoices` — `requireAuth`.
- [ ] Route `.test.ts` + `.md`.
---
## 5. Plugin factory + integration tests + publish
- [ ] `createSubscriptionPlugin`: `definePlugin`, `dependencies: ["database", "auth"]`, `migrationsPath: "./surql"`.
- [ ] `onInit`: register all services; start dunning scheduler via `setInterval` captured for `onClose` teardown.
- [ ] `onClose`: clear dunning interval; flush in-flight.
- [ ] Integration: create plan → subscribe → trial ends → invoice generated → payment attempt → dunning retry (real SurrealDB).
- [ ] `bunx turbo lint typecheck test --filter=@suaveplan/api-plugin-subscription` — green; 100% coverage.
- [ ] `bun scripts/publish.ts --package @suaveplan/api-plugin-subscription`.
- [ ] Archive → `openspec/archive/2026-04-24-enterprise-api-plugin-subscription/`.