
- 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
93 lines
3.2 KiB
Python
93 lines
3.2 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
from typing import List
|
|
from app.db.session import get_db
|
|
from app.schemas.employees import Employee, EmployeeCreate, EmployeeUpdate
|
|
from app.models.employees import Employee as EmployeeModel
|
|
from app.models.users import User, UserRole
|
|
from app.core.deps import get_current_user, require_role
|
|
|
|
router = APIRouter()
|
|
|
|
@router.post("", response_model=Employee)
|
|
def create_employee(
|
|
employee: EmployeeCreate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(require_role([UserRole.ADMIN, UserRole.HR_MANAGER]))
|
|
):
|
|
# Check if employee already exists for this user
|
|
existing_employee = db.query(EmployeeModel).filter(EmployeeModel.user_id == employee.user_id).first()
|
|
if existing_employee:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="Employee profile already exists for this user"
|
|
)
|
|
|
|
db_employee = EmployeeModel(**employee.dict())
|
|
db.add(db_employee)
|
|
db.commit()
|
|
db.refresh(db_employee)
|
|
return db_employee
|
|
|
|
@router.get("", response_model=List[Employee])
|
|
def read_employees(
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(require_role([UserRole.ADMIN, UserRole.HR_MANAGER, UserRole.MANAGER]))
|
|
):
|
|
employees = db.query(EmployeeModel).offset(skip).limit(limit).all()
|
|
return employees
|
|
|
|
@router.get("/{employee_id}", response_model=Employee)
|
|
def read_employee(
|
|
employee_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user)
|
|
):
|
|
employee = db.query(EmployeeModel).filter(EmployeeModel.id == employee_id).first()
|
|
if employee is None:
|
|
raise HTTPException(status_code=404, detail="Employee not found")
|
|
|
|
# Allow access if user is HR/Admin or viewing their own profile
|
|
if (current_user.role not in [UserRole.ADMIN, UserRole.HR_MANAGER] and
|
|
employee.user_id != current_user.id):
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="Not enough permissions"
|
|
)
|
|
|
|
return employee
|
|
|
|
@router.put("/{employee_id}", response_model=Employee)
|
|
def update_employee(
|
|
employee_id: int,
|
|
employee_update: EmployeeUpdate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(require_role([UserRole.ADMIN, UserRole.HR_MANAGER]))
|
|
):
|
|
employee = db.query(EmployeeModel).filter(EmployeeModel.id == employee_id).first()
|
|
if employee is None:
|
|
raise HTTPException(status_code=404, detail="Employee not found")
|
|
|
|
update_data = employee_update.dict(exclude_unset=True)
|
|
for field, value in update_data.items():
|
|
setattr(employee, field, value)
|
|
|
|
db.commit()
|
|
db.refresh(employee)
|
|
return employee
|
|
|
|
@router.delete("/{employee_id}")
|
|
def delete_employee(
|
|
employee_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(require_role([UserRole.ADMIN]))
|
|
):
|
|
employee = db.query(EmployeeModel).filter(EmployeeModel.id == employee_id).first()
|
|
if employee is None:
|
|
raise HTTPException(status_code=404, detail="Employee not found")
|
|
|
|
db.delete(employee)
|
|
db.commit()
|
|
return {"message": "Employee deleted successfully"} |