
- FastAPI application with JWT authentication and role-based access control - Complete employee management with CRUD operations - Department management with manager assignments - Leave management system with approval workflow - Payroll processing with overtime and deductions calculation - Attendance tracking with clock in/out functionality - SQLite database with proper migrations using Alembic - Role-based permissions (Admin, HR Manager, Manager, Employee) - Comprehensive API documentation and health checks - CORS enabled for cross-origin requests Environment Variables Required: - SECRET_KEY: JWT secret key for token signing Features implemented: - User registration and authentication - Employee profile management - Department hierarchy management - Leave request creation and approval - Payroll record processing - Daily attendance tracking - Hours calculation for attendance - Proper error handling and validation
HR Management Backend Service
A comprehensive HR management backend system built with FastAPI, providing APIs for managing employees, departments, leave requests, payroll, and attendance tracking.
Features
- User Authentication: JWT-based authentication with role-based access control
- Employee Management: Complete employee profile management with hierarchical permissions
- Department Management: Create and manage company departments with managers
- Leave Management: Employee leave request system with approval workflow
- Payroll Management: Payroll processing with overtime, bonuses, and deductions
- Attendance Tracking: Clock in/out system with hours calculation
- Role-Based Access: Admin, HR Manager, Manager, and Employee roles with appropriate permissions
Tech Stack
- FastAPI: Modern, fast web framework for building APIs
- SQLAlchemy: SQL toolkit and ORM
- SQLite: Lightweight database for data storage
- Alembic: Database migration tool
- JWT: Token-based authentication
- Pydantic: Data validation using Python type annotations
- Ruff: Fast Python linter and formatter
Project Structure
├── app/
│ ├── core/ # Core functionality (security, dependencies)
│ ├── db/ # Database configuration and session management
│ ├── models/ # SQLAlchemy database models
│ ├── routers/ # API route handlers
│ ├── schemas/ # Pydantic schemas for request/response
│ └── services/ # Business logic services
├── migrations/ # Alembic database migrations
├── main.py # FastAPI application entry point
├── requirements.txt # Python dependencies
└── alembic.ini # Alembic configuration
Installation
- Clone the repository
- Install dependencies:
pip install -r requirements.txt
Running the Application
Start the FastAPI server with uvicorn:
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
The API will be available at:
- Base URL: http://localhost:8000
- API Documentation: http://localhost:8000/docs
- Alternative Docs: http://localhost:8000/redoc
- Health Check: http://localhost:8000/health
Environment Variables
The following environment variables should be set for production:
SECRET_KEY
: JWT secret key for token signing (default: "your-secret-key-change-in-production")
API Endpoints
Authentication
POST /auth/register
- Register a new userPOST /auth/login
- Login and get access tokenGET /auth/me
- Get current user information
Employees
POST /employees
- Create employee profileGET /employees
- List all employeesGET /employees/{id}
- Get employee by IDPUT /employees/{id}
- Update employeeDELETE /employees/{id}
- Delete employee
Departments
POST /departments
- Create departmentGET /departments
- List departmentsGET /departments/{id}
- Get department by IDPUT /departments/{id}
- Update departmentDELETE /departments/{id}
- Delete department
Leave Management
POST /leaves
- Create leave requestGET /leaves
- List leave requestsGET /leaves/{id}
- Get leave request by IDPUT /leaves/{id}/approve
- Approve/reject leave requestPUT /leaves/{id}
- Update leave requestDELETE /leaves/{id}
- Delete leave request
Payroll
POST /payroll
- Create payroll recordGET /payroll
- List payroll recordsGET /payroll/{id}
- Get payroll record by IDPUT /payroll/{id}
- Update payroll recordPOST /payroll/{id}/process
- Process payroll recordDELETE /payroll/{id}
- Delete payroll record
Attendance
POST /attendance
- Create attendance recordGET /attendance
- List attendance recordsGET /attendance/{id}
- Get attendance record by IDPUT /attendance/{id}
- Update attendance recordPOST /attendance/{id}/clock-out
- Clock out from attendance recordDELETE /attendance/{id}
- Delete attendance record
User Roles
Admin
- Full access to all system features
- Can manage all users, employees, departments
- Can view and process all payroll records
- Can delete any records
HR Manager
- Can manage employees and departments
- Can view and approve leave requests
- Can create and process payroll records
- Can view all attendance records
Manager
- Can view employees in their department
- Can approve leave requests for their department
- Can view attendance records for their team
Employee
- Can view and update their own profile
- Can create and manage their own leave requests
- Can view their own payroll records
- Can create and update their own attendance records
Database
The application uses SQLite database with the following main tables:
users
- User accounts and authenticationemployees
- Employee profiles and informationdepartments
- Company departmentsleave_requests
- Leave request recordspayroll_records
- Payroll and salary informationattendance_records
- Daily attendance tracking
Database files are stored in /app/storage/db/
directory.
Development
Running Linter
ruff check . --fix
Database Migrations
The initial migration is included. For new migrations:
# Create a new migration
alembic revision --autogenerate -m "description"
# Apply migrations
alembic upgrade head
Authentication
The API uses JWT (JSON Web Tokens) for authentication. Include the token in the Authorization header:
Authorization: Bearer <your-jwt-token>
Error Handling
The API returns appropriate HTTP status codes:
200
- Success201
- Created400
- Bad Request401
- Unauthorized403
- Forbidden404
- Not Found422
- Validation Error
License
This project is proprietary software for HR management purposes.
Description
Languages
Python
99.1%
Mako
0.9%