# Tasks: enterprise-api-plugin-procurement **Change ID**: enterprise-api-plugin-procurement **Package**: `@suaveplan/api-plugin-procurement` **Target path**: `api/logistics/api-plugin-procurement` **Prerequisite packages on Verdaccio**: `@suaveplan/api`, `@suaveplan/api-shared`, `@suaveplan/api-plugin-auth`, `@suaveplan/api-plugin-database`, `@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-procurement/`; `package.json` v0.0.0; standard tooling. - [ ] Types: `Supplier`, `Requisition`, `PurchaseOrder`, `PoLineItem`, `Grn`, `GrnLineItem`, `InvoiceMatch`, `ProcurementConfig` (Zod). - [ ] Errors: 6-code union + registration. Test + doc. - [ ] Events: `ProcurementEventMap` (procurement:po:created, :approved, :grn:completed, :invoice:matched). --- ## 2. SurrealQL - [ ] Per-table files. All with tenantId indexes. --- ## 3. Services (all `.test.ts` + `.md`) - [ ] **SupplierService**: CRUD + contact info + performance score. - [ ] **RequisitionService**: `createRequisition`, `approveRequisition` (converts to PO), `rejectRequisition`. Approval role check. - [ ] **PurchaseOrderService**: `createPo`, `approvePo` (state: draft→approved→sent→received), `cancelPo`. Audit approval. Emit `procurement:po:created` / `:approved`. - [ ] **GrnService**: `createGrn` (link to PO), `confirmGrn` — emit `procurement:grn:completed` (consumed by inventory plugin). Audit. - [ ] **MatchingService**: three-way match PO/GRN/invoice quantities+amounts; flag discrepancies; emit `:invoice:matched`. - [ ] **PerformanceService**: `recordDelivery`, `getPerformanceScore(supplierId)` — on-time delivery rate + defect rate. --- ## 4. Routes - [ ] Full CRUD + state transitions for each entity. - [ ] Approval endpoints: `requireRole("PROCUREMENT_APPROVER")` + audit. - [ ] Route `.test.ts` + `.md`. --- ## 5. Plugin factory + integration tests + publish - [ ] `createProcurementPlugin`: `definePlugin`, `dependencies: ["database", "auth"]`, `migrationsPath: "./surql"`. - [ ] Integration: supplier → PR → PO approve → GRN → three-way match (real SurrealDB). Verify `procurement:grn:completed` event emitted. - [ ] `bunx turbo lint typecheck test --filter=@suaveplan/api-plugin-procurement` — green; 100% coverage. - [ ] `bun scripts/publish.ts --package @suaveplan/api-plugin-procurement`. - [ ] Archive → `openspec/archive/2026-04-24-enterprise-api-plugin-procurement/`.