Skip to content

Latest commit

 

History

History
95 lines (79 loc) · 3.08 KB

File metadata and controls

95 lines (79 loc) · 3.08 KB

Credential Controller & Routes Implementation

Overview

Implements credential management endpoints for certificates and achievements as specified in issue #8.

Changes Made

New Files

  • src/controllers/credential.controller.ts - Controller with three main endpoints
  • src/routes/v1/credentials.routes.ts - Route definitions with validation
  • tests/unit/credential.controller.test.ts - Comprehensive unit tests (14 tests, all passing)

Modified Files

  • src/routes/index.ts - Added credentials routes to API

Implemented Endpoints

1. GET /api/v1/credentials

  • Auth: Required
  • Purpose: Retrieve all credentials for authenticated user
  • Query Params: moduleId, fromDate, toDate, page, limit
  • Features:
    • Pagination support (default: page=1, limit=10, max=100)
    • Filter by module
    • Filter by date range
    • Returns credential details with module information
    • Includes shareable verification links

2. GET /api/v1/credentials/:id

  • Auth: Required (user must own credential)
  • Purpose: Retrieve single credential details
  • Features:
    • Full credential information
    • Module details (title, description, category, difficulty, reward)
    • Holder information
    • Verification metadata
    • Shareable link

3. GET /api/v1/credentials/verify/:onChainId

  • Auth: Not required (public endpoint)
  • Purpose: Public verification of credentials
  • Features:
    • Verifies by onChainId or regular credential ID
    • Returns validation status
    • Shows credential holder and module information
    • Timestamp of verification

Technical Details

Validation

  • Uses Zod schemas for input validation
  • UUID validation for IDs
  • ISO 8601 datetime validation for date filters
  • Numeric validation for pagination parameters

Error Handling

  • Proper HTTP status codes (400, 401, 404)
  • Descriptive error messages
  • Uses custom error classes (BadRequestError, NotFoundError, UnauthorizedError)
  • Wrapped with asyncHandler for promise rejection handling

Database

  • Uses Prisma ORM
  • Efficient queries with proper includes
  • Pagination with count queries
  • Indexed lookups by ID and onChainId

Testing

  • 14 unit tests covering all endpoints
  • Tests for success cases
  • Tests for error cases (invalid input, unauthorized access, not found)
  • Tests for filtering and pagination
  • All tests passing ✅

Acceptance Criteria Met

  • ✅ Users can view all their earned credentials
  • ✅ Public verification endpoint returns credential validity
  • ✅ Verification works without authentication
  • ✅ Credential details include on-chain reference
  • ✅ Filtering and pagination work correctly
  • ✅ Unit tests written and passing

Code Quality

  • ✅ Linting passed
  • ✅ All existing tests still passing (225 tests total)
  • ✅ Follows existing codebase patterns
  • ✅ Proper TypeScript types
  • ✅ Comprehensive error handling
  • ✅ Clean, readable code with comments

Next Steps

  • Integration testing with actual database
  • E2E testing for complete user flows
  • Performance testing with large datasets
  • Consider adding rate limiting for public verification endpoint