# 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/`.