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,80 @@
# OpenSpec Change: enterprise-api-plugin-customs
- **Change ID**: enterprise-api-plugin-customs
- **Package**: `@suaveplan/api-plugin-customs`
- **Category**: api/logistics
- **Target path**: `api/logistics/api-plugin-customs`
- **Status**: PROPOSED
- **Date**: 2026-04-24
- **Reference spec**: `/mnt/s/web/suaveplan/enterprise/api-plugin-customs.md`
---
## Summary
Deliver `@suaveplan/api-plugin-customs` on the genesis branch from scratch at enterprise quality. The main-branch package is a skeleton (79 lines, no repos, no routes, no tests, placeholder event emission on startup). Genesis delivers the full implementation: customs declaration lifecycle (submitted/under-review/cleared/rejected), border-control holds (held/released), HS-code classification lookup, Intrastat reporting, duty payment tracking, and per-declaration document management. Routes under `/customs`. Per-table SurrealQL. Typed events (proper module augmentation — no `as never`/`as Parameters<...>` casts): `customs:declaration:submitted`, `customs:declaration:cleared`, `customs:border:held`, `customs:border:released`, `customs:duty:paid`.
---
## 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-logistics` (`workspace:^`) | Shipment reference |
| `@suaveplan/events` (`workspace:^`) | Event bus |
| `@suaveplan/error` (`workspace:^`) | `CustomsError` hierarchy |
| `@suaveplan/idempotency` (`workspace:^`) | Mutation guard |
| `@suaveplan/audit-logging` (`workspace:^`) | Audit trail |
| `@suaveplan/rate-limiter` (`workspace:^`) | Rate limits |
| `hono` + `@hono/zod-openapi` + `zod` | Routes |
| `@opentelemetry/api` | OTel |
---
## Public API surface
```ts
function createCustomsPlugin(config?: CustomsConfig): PluginDescriptor<CustomsConfig>
// Services: DeclarationService, BorderHoldService, HsCodeService, DutyService, IntrastatService
// Routes: /customs/declarations, /customs/declarations/:id,
// /customs/declarations/:id/clear, /customs/border-holds,
// /customs/hs-codes/lookup, /customs/intrastat
```
### Error codes
```ts
type CustomsErrorCode =
| "CUSTOMS_DECLARATION_NOT_FOUND"
| "CUSTOMS_DECLARATION_ALREADY_CLEARED"
| "CUSTOMS_BORDER_HOLD_NOT_FOUND"
| "CUSTOMS_HS_CODE_NOT_FOUND"
| "CUSTOMS_DUTY_PAYMENT_FAILED"
| "CUSTOMS_INTRASTAT_PERIOD_CLOSED"
```
---
## File layout
```text
api/logistics/api-plugin-customs/
package.json tsconfig.json vitest.config.ts
surql/ (customs_declaration, border_hold, hs_code, duty_payment, intrastat_report tables)
src/
index.ts events.ts types/ errors/
services/declaration/ services/border-hold/ services/hs-code/
services/duty/ services/intrastat/
routes/customs/
tests/integration.test.ts
```
---
## Impact
Prerequisites: `@suaveplan/api`, `@suaveplan/api-plugin-auth`, `@suaveplan/api-plugin-database`, `@suaveplan/api-plugin-logistics`, `@suaveplan/events`, `@suaveplan/idempotency`, `@suaveplan/audit-logging`.

View File

@@ -0,0 +1,56 @@
# Tasks: enterprise-api-plugin-customs
**Change ID**: enterprise-api-plugin-customs
**Package**: `@suaveplan/api-plugin-customs`
**Target path**: `api/logistics/api-plugin-customs`
**Prerequisite packages on Verdaccio**: `@suaveplan/api`, `@suaveplan/api-shared`, `@suaveplan/api-plugin-auth`, `@suaveplan/api-plugin-database`, `@suaveplan/api-plugin-logistics`, `@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/logistics/api-plugin-customs/`; `package.json` v0.0.0, `"sideEffects": ["src/events.ts"]`; standard tooling.
- [ ] Types: `CustomsDeclaration`, `BorderHold`, `HsCode`, `DutyPayment`, `IntrastatReport`, `CustomsConfig` (Zod — remove stub `z.object({})`).
- [ ] Errors: 6-code union + registration. Test + doc.
- [ ] Events: `CustomsEventMap` module augmentation — proper declaration, no `as Parameters<...>[0]` cast. Covers all 5 event types listed in proposal. Remove the spurious startup event emission from `onInit`.
---
## 2. SurrealQL
- [ ] `customs_declaration.surql`, `border_hold.surql`, `hs_code.surql` (reference data), `duty_payment.surql`, `intrastat_report.surql`. tenantId indexes.
---
## 3. Services (all `.test.ts` + `.md`)
- [ ] **DeclarationService**: `submitDeclaration` (link to shipmentId from logistics), `getDeclaration`, `listDeclarations`, `clearDeclaration`, `rejectDeclaration`. State machine enforcement. Emit events; audit-log.
- [ ] **BorderHoldService**: `placeHold`, `releaseHold`, `listHolds`. Audit-logged.
- [ ] **HsCodeService**: `lookup(description)` — fuzzy search in hs_code reference table; `getHsCode(code)`.
- [ ] **DutyService**: `recordDutyPayment`, `listDutyPayments(declarationId)`.
- [ ] **IntrastatService**: `generateReport(period: string)` — aggregate trade data for EU Intrastat submission; `submitReport`.
---
## 4. Routes
- [ ] Full route coverage for all 5 service areas.
- [ ] All write routes: `requireAuth` + idempotency + audit.
- [ ] Route `.test.ts` + `.md`.
---
## 5. Plugin factory + integration tests + publish
- [ ] `createCustomsPlugin`: `definePlugin`, `dependencies: ["database", "auth"]`, `migrationsPath: "./surql"`.
- [ ] `onInit` does NOT emit any placeholder event on startup.
- [ ] Integration: submit declaration → border hold → release → clear (real SurrealDB).
- [ ] `bunx turbo lint typecheck test --filter=@suaveplan/api-plugin-customs` — green; 100% coverage.
- [ ] `bun scripts/publish.ts --package @suaveplan/api-plugin-customs`.
- [ ] Archive → `openspec/archive/2026-04-24-enterprise-api-plugin-customs/`.