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’sLeaveRequestForm component was unable to populate the leave type dropdown because:
- Missing Backend Endpoint: No dedicated API endpoint existed to fetch leave types
- 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:
GetLeaveTypes method:
- 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:
RegisterRoutes:
Frontend Integration
The frontend is already correctly configured to use this endpoint: API Client (frontend/apps/platform/src/lib/api.ts):
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:
200 OK
500 Internal Server Error
Testing
1. Verify Database Has Leave Types
2. Test Backend Endpoint
Option A: Using cURL (requires valid Better Auth JWT token)- Start the backend:
cd backend && go run cmd/server/main.go - Start the frontend:
cd frontend && npm run dev - Navigate to the leave request form
- The leave type dropdown should now be populated with the seeded leave types
3. Verify Frontend Integration
- Open the browser developer console
- Navigate to the leave request form page
- Check the Network tab for a request to
/v1/leave/types - Verify the response contains the leave types
- Confirm the dropdown is populated
Data Model
LeaveType Schema
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:- Organization Filtering: The service first attempts to fetch organization-specific leave types using
FindByOrganization(orgID) - Fallback Mechanism: If no organization-specific types exist, it falls back to
FindAllActive()for backward compatibility - Future Enhancement: When multi-tenancy is fully implemented, the
orgIDwill be extracted from the authenticated user’s context instead of using"org_default"
Future Enhancements
- Admin Panel: Build an admin interface to manage leave types (create, update, deactivate)
- Organization Context: Extract
orgIDfrom authenticated user context - Caching: Implement caching for leave types to reduce database queries
- Pagination: Add pagination support if the number of leave types grows significantly
- Leave Type Categories: Group leave types by category (paid, unpaid, statutory, etc.)
Related Files
Backend
internal/modules/leave/application/port/leave_query_ports.go- Service interfaceinternal/modules/leave/application/usecase/leave_query_service.go- Service implementationinternal/modules/leave/module.go- Module initializationinternal/modules/leave/adapter/inbound/http/handler.go- HTTP handlerinternal/modules/leave/adapter/outbound/persistence/postgresql/leave_type_repository.go- Repositoryinternal/modules/leave/domain/leave.go- Domain model
Frontend
frontend/apps/platform/src/lib/api.ts- API clientfrontend/apps/platform/src/lib/types.ts- TypeScript typesfrontend/apps/platform/src/components/qwik/leave/LeaveRequestForm.tsx- Form component
Troubleshooting
Leave types not appearing in the frontend
- Check backend is running:
lsof -ti:8080should return a process ID - Verify database has data: Run the SQL query above
- Check authentication: Ensure you’re logged in with a valid Better Auth JWT token
- Check browser console: Look for any API errors
- Verify API response: Check the Network tab in browser DevTools
Backend errors
- Check logs: Backend logs will show any errors in fetching leave types
- Verify database connection: Ensure PostgreSQL is running and accessible
- Check repository implementation: Verify
FindByOrganizationandFindAllActivemethods 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