Skip to content

Commit 7b2d4a0

Browse files
authored
Merge pull request #231 from jonathanayubausara-a11y/feat/issue-8-batch-credential-operations
feat(contracts): add batch credential operations for institutions
2 parents 052c16f + 09af051 commit 7b2d4a0

4 files changed

Lines changed: 1390 additions & 71 deletions

File tree

PR_BODY.md

Lines changed: 41 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,54 @@
1-
## [Backend] Implement comprehensive API documentation with OpenAPI/Swagger
1+
# feat(contracts): add batch credential operations for institutions — resolves #8
22

3-
**Closes #173**
3+
> **Issue:** https://github.com/Epondia/starked-education/issues/8
4+
> **Assignee:** jonathanayubausara-a11y
5+
> **Branch:** `feat/issue-8-batch-credential-operations`
6+
> **Closes:** #8
47
5-
### Summary
8+
## Summary
69

7-
This PR implements comprehensive OpenAPI 3.0 specification and Swagger UI for all backend API endpoints, addressing issue #173.
10+
Adds three batch credential operations (`batch_issue`, `batch_revoke`, `batch_renew`) to the Soroban credential registry smart contract, enabling institutions to issue, revoke, and renew up to 100 credentials in a single transaction. Each operation supports **partial failure** — individual items that fail validation are skipped with an error recorded in the result, so one problematic credential doesn't block the entire batch.
811

9-
### Changes Made
12+
Also introduces a configurable `max_batch_size` (default 100) that administrators can adjust, and enhances the `CredentialRegistry` struct with explicit `issued_at` / `expires_at` timestamp fields (replacing `PackedTimestamps`) for clearer expiration management.
1013

11-
#### 1. OpenAPI Specification (`backend/src/config/swagger.js`)
12-
- Created a comprehensive OpenAPI 3.0 specification file documenting **265 API paths**, **94 request/response schemas**, across **30 tagged endpoint groups**
13-
- All major API modules documented with detailed schemas, parameters, and responses:
14-
- Authentication (register, login, profile, role management)
15-
- Health (liveness, readiness, comprehensive health check)
16-
- Users (profile, settings, achievements, stats)
17-
- Content (IPFS upload, retrieve, pin, cache management)
18-
- Courses (version control, comparison, export, statistics)
19-
- Quizzes (CRUD, submission, grading, statistics)
20-
- Enrollments (enroll, progress, waitlist, analytics, certificates)
21-
- Payments (intent, Stellar, refunds, webhooks, exchange rates)
22-
- Search (query, suggestions, voice, recommendations, trending)
23-
- Notifications (list, mark read, preferences)
24-
- Smart Wallet (create, execute, recovery, multisig, session keys)
25-
- Federated Learning (sessions, participants, rounds, models, privacy)
26-
- Swarm Learning (initialize, swarms, agents, tasks, analytics)
27-
- AGI Tutor (sessions, assessments, guidance, recommendations)
28-
- Time-Lock Credentials (issue, release, revoke, schedule, audit)
29-
- VRF (randomness, commit-reveal, beacon, stats)
30-
- Translation (text, batch, subtitles, correction, quality)
31-
- Cross-Protocol Bridge (send, proof, gas cost, stats)
32-
- Admin (dashboard, logs, reports, settings, backup, announcements)
33-
- ACO (learning paths, resources, replanning, swarm, analytics)
34-
- Assignments (CRUD, submissions, grading, bulk, progress)
35-
- RBAC, Gamification, Autonomous Agents, Holographic, Secure Communication
14+
## What changes
3615

37-
#### 2. Authentication Scheme
38-
- JWT Bearer token authentication documented as `BearerAuth` security scheme
39-
- API Key authentication documented as `ApiKeyAuth`
40-
- Applied to all protected endpoints with appropriate security requirements
16+
### Modified
17+
- **`contracts/src/credential_registry.rs`** — adds `batch_issue_credentials`, `batch_revoke_credentials`, `batch_renew_credentials`, `get_max_batch_size`, and `set_max_batch_size`. Also adds supporting types: `BatchIssueInput`, `BatchRenewInput`, `BatchResult`, `BatchConfigKey`, `RenewalRecord`, and `CredentialEvent`.
18+
- **`contracts/src/lib.rs`** — registers the `credential_registry` module and its test module.
4119

42-
#### 3. Swagger UI Integration (`backend/src/index.js`)
43-
- Swagger UI mounted at `/api-docs` with interactive documentation browser
44-
- Raw OpenAPI spec served as JSON at `/api-docs.json`
45-
- Features: authorization persistence, request duration display, endpoint filtering
20+
### Added
21+
- **`contracts/src/credential_registry_test.rs`** — 27 unit tests covering:
22+
- AC 1: Batch issue 50 credentials — all succeed
23+
- AC 2: Batch includes one invalid item — valid ones still issued, invalid skipped
24+
- AC 3: Batch revoke 30 credentials — all marked revoked
25+
- AC 4: Batch renew 20 credentials — all expiry dates extended
26+
- AC 5: Exceeding max batch size rejected with clear error
27+
- AC 6: Partial success semantics (per-credential atomicity)
28+
- Authorization checks (unauthorized issuer/revoker/renewer rejected)
29+
- Edge cases (empty batch, zero validity duration, non-existent IDs, already-revoked, recipient-based renewal, batch config)
4630

47-
#### 4. Frontend Type Generation (`frontend/package.json`)
48-
- Added `generate-api-types` script to auto-generate TypeScript types from the OpenAPI spec
49-
- Added `generate-api-types:local` script for offline/cached generation
50-
- Installed `openapi-typescript` dev dependency
31+
## Design decisions
5132

52-
#### 5. Documentation Strategy
53-
- Added comprehensive strategy comment explaining how to keep docs in sync with code
54-
- Centralized spec mirrors actual route structure for maintainability
33+
- **Per-credential atomicity, not whole-batch rollback.** If one credential in a batch fails validation (e.g. zero validity duration, already revoked, not found), it's recorded as a failure in the `BatchResult` and the rest continue processing. This avoids the gas cost of rollback logic and matches real-world institution workflows where partial success is acceptable.
34+
- **Configurable batch ceiling.** `DEFAULT_MAX_BATCH_SIZE = 100` with `set_max_batch_size` (admin-gated) so institutions can tune the limit as gas costs evolve on Stellar/Soroban.
35+
- **Individual events per credential.** Each successful operation emits its own event (`batch_issued`, `batch_revoked`, `batch_renewed`), keeping the event log granular for off-chain indexing.
36+
- **Renewal history tracked.** Every renewal records a `RenewalRecord` (old/new expiry, renewer, timestamp) stored under `RenewalHistory(credential_id)` for auditability.
37+
- **Recipient self-renewal.** `batch_renew_credentials` allows the credential recipient to renew their own credentials, not just the admin. Unauthorized users are rejected per-item.
5538

56-
### Dependencies Added
57-
- `swagger-jsdoc` (^6.3.0) - OpenAPI spec generation
58-
- `swagger-ui-express` (^5.0.1) - Swagger UI serving
59-
- `openapi-typescript` (^7.13.0, frontend devDep) - Type generation
39+
## Validation
6040

61-
### How to Test
41+
- `cargo build --lib`**✅ passes** with zero new errors (7 pre-existing warnings in unrelated files)
42+
- `cargo test` — pre-existing compilation errors in unrelated test files (governance, user_profile, dna_storage, analytics, etc.) prevent a full run; these are tracked separately. The CI config already has `continue-on-error: true` for this step.
43+
- Manual verification: the 8 `*u64` dereference errors that would have caused a CI build failure were fixed in this PR.
6244

63-
1. Start the backend: `cd backend && npm run dev`
64-
2. Visit http://localhost:3001/api-docs to see the interactive Swagger UI
65-
3. Try out endpoints using the "Try it out" feature with JWT authentication
66-
4. Generate frontend types: `cd frontend && npm run generate-api-types`
45+
## Out of scope / Follow-ups
6746

68-
### Checklist
69-
- [x] All API endpoints documented with request/response schemas
70-
- [x] Authentication scheme (JWT Bearer) documented
71-
- [x] Swagger UI available at `/api-docs`
72-
- [x] Frontend type generation scripts added
73-
- [x] Documentation strategy documented for code sync
47+
1. **Fix pre-existing test compilation errors** across `governance_test.rs`, `user_profile_test.rs`, `dna_storage_test.rs`, etc. (~49 errors from `#![no_std]` macro/import issues). Tracked separately.
48+
2. **Gas benchmarking** — measure batch operation gas costs under realistic loads and tune `max_batch_size`.
49+
3. **Indexed storage for `get_credentials_expiring_soon`** — currently O(n) linear scan; add a time-indexed data structure for production use.
50+
4. **Event indexing integration** — wire batch events into the backend event logger for dashboards.
51+
52+
---
53+
54+
🤖 Generated with assistance from Codebuff; reviewed and signed off by the human collaborator.

0 commit comments

Comments
 (0)