# Tasks: enterprise-api-plugin-port-tracking **Change ID**: enterprise-api-plugin-port-tracking **Package**: `@suaveplan/api-plugin-port-tracking` **Target path**: `api/logistics/api-plugin-port-tracking` **Prerequisite packages on Verdaccio**: `@suaveplan/api`, `@suaveplan/api-shared`, `@suaveplan/api-plugin-auth`, `@suaveplan/api-plugin-database`, `@suaveplan/api-plugin-logistics`, `@suaveplan/datetime`, `@suaveplan/events`, `@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-port-tracking/`; `package.json` v0.0.0; standard tooling. - [ ] Types: `Vessel`, `VesselPosition`, `Port`, `Berth`, `Arrival`, `CargoManifest`, `AisAdapter`, `PortTrackingConfig` (Zod). - [ ] Errors: 4-code union + registration. Test + doc. - [ ] Events: `PortTrackingEventMap` (port:vessel:arrived, port:vessel:departed, port:ais:update). --- ## 2. SurrealQL - [ ] `vessel.surql`, `port.surql`, `berth.surql`, `arrival.surql`, `cargo_manifest.surql`. tenantId + mmsi indexes. --- ## 3. AIS adapter + services (all `.test.ts` + `.md`) - [ ] `src/adapters/ais.port.ts` — `AisAdapter` interface + stub implementation for tests. - [ ] **VesselService**: `getVessel(mmsi)`, `updatePosition(mmsi, pos)`, `listVessels`. Subscribe to AIS updates in `onInit` via adapter. - [ ] **PortService**: `getPort(code)`, `listPorts`, `getArrivals(portCode, dateRange)`. - [ ] **BerthService**: `allocateBerth`, `releaseBerth`. Conflict detection. - [ ] **ManifestService**: `createManifest`, `updateManifest`, `getManifest`. Link to logistics shipment. - [ ] **EtaService**: `calculateEta(mmsi, destinationPort)` using current position + speed via `@suaveplan/datetime`. --- ## 4. Routes - [ ] `GET /port-tracking/vessels/:mmsi` — `requireAuth`. - [ ] `GET /port-tracking/ports/:portCode`, `GET /port-tracking/ports/:portCode/arrivals` — `requireAuth`. - [ ] `GET/POST /port-tracking/manifests`, `GET /port-tracking/manifests/:id` — `requireAuth`. - [ ] `GET /port-tracking/vessels/:mmsi/eta?destination=` — `requireAuth`. - [ ] Route `.test.ts` + `.md`. --- ## 5. Plugin factory + integration tests + publish - [ ] `createPortTrackingPlugin(aisAdapter?, config?)`: `definePlugin`, `dependencies: ["database", "auth"]`, `migrationsPath: "./surql"`. - [ ] `onInit`: subscribe to AIS adapter updates; capture disposers for `onClose`. - [ ] Integration: vessel position update → arrival recorded → manifest linked to shipment (real SurrealDB + stub AIS adapter). - [ ] `bunx turbo lint typecheck test --filter=@suaveplan/api-plugin-port-tracking` — green; 100% coverage. - [ ] `bun scripts/publish.ts --package @suaveplan/api-plugin-port-tracking`. - [ ] Archive → `openspec/archive/2026-04-24-enterprise-api-plugin-port-tracking/`.