Skip to content

feat(v0.1.25.29): budget bulk-action endpoint (spec v0.1.25.26) - #112

Merged
amavashev merged 1 commit into
mainfrom
feat/admin-budget-bulk-action-v0.1.25.29
Apr 18, 2026
Merged

amavashev merged 1 commit into
mainfrom
feat/admin-budget-bulk-action-v0.1.25.29

Conversation

@amavashev

Copy link
Copy Markdown
Collaborator

Summary

Implements POST /v1/admin/budgets/bulk-action to match cycles-protocol admin spec v0.1.25.26, resolving issue #99 (Bulk Budget Reset at Tenant or Parent-Scope Level). Operators rolling over a billing period can now issue one filtered bulk request instead of iterating listBudgets + per-row fundBudget.

Shape mirrors the tenant/webhook bulk-action pattern already shipped in v0.1.25.26.

Endpoint

Method Path Auth Purpose
POST /v1/admin/budgets/bulk-action AdminKeyAuth only Apply a FundingOperation (CREDIT / DEBIT / RESET / REPAY_DEBT / RESET_SPENT) to every budget matching a filter, bounded to 500 rows.

Safety & semantics

  • 500-row hard cap → 400 LIMIT_EXCEEDED
  • expected_count gate → 409 COUNT_MISMATCH
  • 15-min idempotency replay via IdempotencyStore (returns cached envelope verbatim)
  • Per-row derived idempotency key (bulkKey:scope:unit) → Lua fund short-circuit prevents double-apply on retry-the-failed-set
  • Row-level known codes: BUDGET_EXCEEDED, INVALID_TRANSITION, NOT_FOUND, PERMISSION_DENIED, INTERNAL_ERROR
  • Skipped bucket: REPAY_DEBT on debt==0 → reason=ALREADY_IN_TARGET_STATE
  • Cross-tenant safety: filter.tenant_id is required; missing → 400
  • Zero-drift matcher: BudgetListFilters.fromBulkFilter(...) ensures listBudgets preview and bulk-action always agree on what matches
  • AuthInterceptor plugged: explicit AdminKeyAuth branch — tenant keys rejected 401
  • AuditLogEntry: one per bulk-op with per-bucket counts in metadata
  • Per-row id: ledger_id (per spec BudgetBulkActionResponse.succeeded.description)

Files changed

  • New DTOs (model): BudgetBulkFilter, BudgetBulkActionRequest, BudgetBulkActionResponse
  • BudgetRepository: new matchForBulk(tenantId, filters, cap) + fromBulkFilter static factory on BudgetListFilters
  • BudgetController: new bulkActionBudgets handler — validate → idempotency lookup → match → limit check → count gate → per-row apply → store → audit
  • AuthInterceptor: explicit /v1/admin/budgets/bulk-action admin-key branch
  • Version: 0.1.25.28 → 0.1.25.29 in pom.xml, docker-compose.prod.yml, docker-compose.full-stack.prod.yml
  • AUDIT.md: new dated section with full design rationale

Test coverage

  • BudgetControllerBulkActionTest — 27 tests (happy paths × 5 actions, classification branches, validation 400s, idempotency replay, audit metadata, auth)
  • BudgetRepositoryBulkMatchTest — 14 tests (each filter dimension, cap+1 sentinel, tenant isolation, stale-index cleanup, parse-failure tolerance)
  • 698/698 tests green; JaCoCo ≥95% per module
  • OpenApiContractDiffTest + SpecCoverageReportTest clean against spec v0.1.25.26 on cycles-protocol/main

Closes #99.

Test plan

  • mvn-proxy -B verify --file cycles-admin-service/pom.xml -Dtest='!*IntegrationTest' -Dsurefire.failIfNoSpecifiedTests=false — 698/698 green
  • BudgetControllerBulkActionTest — 27/27 green
  • BudgetRepositoryBulkMatchTest — 14/14 green
  • OpenApiContractDiffTest + SpecCoverageReportTest — 46/46 spec operations covered
  • JaCoCo ≥95% per-module
  • Manual curl smoke (admin server on 7979): preview via listBudgets, bulk-action with expected_count, 409 on count drift, 400 on missing amount
  • CI green on this PR

Implements POST /v1/admin/budgets/bulk-action, resolving cycles-server-admin
issue #99 ("Bulk Budget Reset at Tenant or Parent-Scope Level"). Operators
rolling over a billing period can now issue one filtered bulk request
instead of iterating listBudgets + per-row fundBudget.

Shape mirrors the tenant/webhook bulk-action pattern already shipped in
v0.1.25.26:

- BudgetBulkFilter reuses the listBudgets matcher (zero-drift via
  BudgetListFilters.fromBulkFilter) and requires tenant_id for
  cross-tenant safety.
- action reuses FundingOperation (CREDIT/DEBIT/RESET/REPAY_DEBT/RESET_SPENT).
- Envelope returns 200 with succeeded/failed/skipped; per-row id is the
  ledger_id per spec BudgetBulkActionResponse.succeeded.description.
- Safety gates: 500-row hard cap -> LIMIT_EXCEEDED (400); expected_count
  -> COUNT_MISMATCH (409); 15-min idempotency replay via IdempotencyStore.
- Per-row derived idempotency key (bulkKey:scope:unit) feeds Lua fund
  short-circuit so replays and retry-the-failed-set never double-apply.
- Row-level known codes: BUDGET_EXCEEDED, INVALID_TRANSITION, NOT_FOUND,
  PERMISSION_DENIED, INTERNAL_ERROR; skipped carries
  reason=ALREADY_IN_TARGET_STATE for REPAY_DEBT on zero debt.
- AuthInterceptor plugged: bulk-action path now explicitly requires
  AdminKeyAuth (tenant keys rejected 401).
- AuditLogEntry emitted once per bulk-op with per-bucket counts.

Verification: 698/698 tests green, JaCoCo >=95% per module,
OpenApiContractDiffTest + SpecCoverageReportTest clean against spec
v0.1.25.26 on cycles-protocol/main.
@amavashev
amavashev merged commit 2982b26 into main Apr 18, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] Bulk Budget Reset at Tenant or Parent-Scope Level

1 participant