Overview

This document describes the implementation of the Leave Types API endpoint that allows the frontend to fetch available leave types for display in the leave request form.

Problem Statement

Previously, the leave types were seeded in the database but were not accessible via the API. The frontend’s LeaveRequestForm component was unable to populate the leave type dropdown because:
  1. Missing Backend Endpoint: No dedicated API endpoint existed to fetch leave types
  2. Frontend Calling Wrong Endpoint: The form was attempting to use the leave balances endpoint

Solution

Backend Changes

1. Updated Query Service Interface (internal/modules/leave/application/port/leave_query_ports.go)

Added a new method to the LeaveQueryService interface:

2. Implemented Service Method (internal/modules/leave/application/usecase/leave_query_service.go)

Added leaveTypeRepo field to the service:
Implemented the GetLeaveTypes method:
Key Features:
  • Attempts to fetch organization-specific leave types first
  • Falls back to all active leave types if none are found for the organization
  • Useful for testing with the default organization (org_default)
  • Proper error handling and logging

3. Updated Module Initialization (internal/modules/leave/module.go)

Modified the NewLeaveQueryService call to include the leaveTypeRepo:

4. Updated HTTP Handler (internal/modules/leave/adapter/inbound/http/handler.go)

Modified the GetLeaveTypes handler to use the query service instead of directly accessing the repository:
The route is already registered in RegisterRoutes:

Frontend Integration

The frontend is already correctly configured to use this endpoint: API Client (frontend/apps/platform/src/lib/api.ts):
Leave Request Form (frontend/apps/platform/src/components/qwik/leave/LeaveRequestForm.tsx):

API Endpoint

GET /v1/leave/types

Retrieves all active leave types available for the organization. Authentication: Required (Bearer token) Request:
Response: 200 OK
Error Response: 500 Internal Server Error

Testing

1. Verify Database Has Leave Types

Expected: Should show several leave types (Annual, Sick, Emergency, Maternity, Paternity, Unpaid Leave)

2. Test Backend Endpoint

Option A: Using cURL (requires valid Better Auth JWT token)
Option B: Using the Frontend
  1. Start the backend: cd backend && go run cmd/server/main.go
  2. Start the frontend: cd frontend && npm run dev
  3. Navigate to the leave request form
  4. The leave type dropdown should now be populated with the seeded leave types

3. Verify Frontend Integration

  1. Open the browser developer console
  2. Navigate to the leave request form page
  3. Check the Network tab for a request to /v1/leave/types
  4. Verify the response contains the leave types
  5. Confirm the dropdown is populated

Data Model

LeaveType Schema

Note: The domain entity in internal/modules/leave/domain/entity/leave.go defines the core logic, while the GORM model in persistence/postgresql/models.go handles the database mapping.

Frontend TypeScript Interface

Multi-Tenancy Support

The implementation is designed with multi-tenancy in mind:
  1. Organization Filtering: The service first attempts to fetch organization-specific leave types using FindByOrganization(orgID)
  2. Fallback Mechanism: If no organization-specific types exist, it falls back to FindAllActive() for backward compatibility
  3. Future Enhancement: When multi-tenancy is fully implemented, the orgID will be extracted from the authenticated user’s context instead of using "org_default"

Future Enhancements

  1. Admin Panel: Build an admin interface to manage leave types (create, update, deactivate)
  2. Organization Context: Extract orgID from authenticated user context
  3. Caching: Implement caching for leave types to reduce database queries
  4. Pagination: Add pagination support if the number of leave types grows significantly
  5. Leave Type Categories: Group leave types by category (paid, unpaid, statutory, etc.)

Backend

  • internal/modules/leave/application/port/leave_query_ports.go - Service interface
  • internal/modules/leave/application/usecase/leave_query_service.go - Service implementation
  • internal/modules/leave/module.go - Module initialization
  • internal/modules/leave/adapter/inbound/http/handler.go - HTTP handler
  • internal/modules/leave/adapter/outbound/persistence/postgresql/leave_type_repository.go - Repository
  • internal/modules/leave/domain/leave.go - Domain model

Frontend

  • frontend/apps/platform/src/lib/api.ts - API client
  • frontend/apps/platform/src/lib/types.ts - TypeScript types
  • frontend/apps/platform/src/components/qwik/leave/LeaveRequestForm.tsx - Form component

Troubleshooting

Leave types not appearing in the frontend

  1. Check backend is running: lsof -ti:8080 should return a process ID
  2. Verify database has data: Run the SQL query above
  3. Check authentication: Ensure you’re logged in with a valid Better Auth JWT token
  4. Check browser console: Look for any API errors
  5. Verify API response: Check the Network tab in browser DevTools

Backend errors

  1. Check logs: Backend logs will show any errors in fetching leave types
  2. Verify database connection: Ensure PostgreSQL is running and accessible
  3. Check repository implementation: Verify FindByOrganization and FindAllActive methods work correctly

Summary

The Leave Types API is now fully functional and integrated with the frontend. Users can:
  • View all active leave types in the leave request form dropdown
  • See leave type details (name, max days)
  • Submit leave requests with the selected leave type
The implementation follows CQRS principles, uses the query service layer for consistency, and is designed to support future multi-tenancy requirements.