Automated Action 1b9ddb4750 Implement comprehensive HR Management Backend System
- 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
2025-06-23 10:06:23 +00:00

152 lines
8.2 KiB
Python

"""Initial migration
Revision ID: 001
Revises:
Create Date: 2023-12-01 00:00:00.000000
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '001'
down_revision = None
branch_labels = None
depends_on = None
def upgrade() -> None:
# Create users table
op.create_table('users',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('email', sa.String(), nullable=False),
sa.Column('hashed_password', sa.String(), nullable=False),
sa.Column('full_name', sa.String(), nullable=False),
sa.Column('role', sa.Enum('ADMIN', 'HR_MANAGER', 'MANAGER', 'EMPLOYEE', name='userrole'), nullable=True),
sa.Column('is_active', sa.Boolean(), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_users_email'), 'users', ['email'], unique=True)
op.create_index(op.f('ix_users_id'), 'users', ['id'], unique=False)
# Create departments table
op.create_table('departments',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(), nullable=False),
sa.Column('description', sa.String(), nullable=True),
sa.Column('manager_id', sa.Integer(), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_departments_id'), 'departments', ['id'], unique=False)
op.create_index(op.f('ix_departments_name'), 'departments', ['name'], unique=True)
# Create employees table
op.create_table('employees',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('employee_id', sa.String(), nullable=False),
sa.Column('department_id', sa.Integer(), nullable=True),
sa.Column('position', sa.String(), nullable=False),
sa.Column('salary', sa.Numeric(precision=10, scale=2), nullable=True),
sa.Column('hire_date', sa.Date(), nullable=False),
sa.Column('status', sa.Enum('ACTIVE', 'INACTIVE', 'TERMINATED', name='employmentstatus'), nullable=True),
sa.Column('phone', sa.String(), nullable=True),
sa.Column('address', sa.String(), nullable=True),
sa.Column('emergency_contact', sa.String(), nullable=True),
sa.Column('emergency_phone', sa.String(), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
sa.ForeignKeyConstraint(['department_id'], ['departments.id'], ),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_employees_employee_id'), 'employees', ['employee_id'], unique=True)
op.create_index(op.f('ix_employees_id'), 'employees', ['id'], unique=False)
# Add foreign key for department manager
op.create_foreign_key(None, 'departments', 'employees', ['manager_id'], ['id'])
# Create leave_requests table
op.create_table('leave_requests',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('employee_id', sa.Integer(), nullable=False),
sa.Column('leave_type', sa.Enum('VACATION', 'SICK', 'PERSONAL', 'MATERNITY', 'PATERNITY', 'EMERGENCY', name='leavetype'), nullable=False),
sa.Column('start_date', sa.Date(), nullable=False),
sa.Column('end_date', sa.Date(), nullable=False),
sa.Column('days_requested', sa.Integer(), nullable=False),
sa.Column('reason', sa.Text(), nullable=True),
sa.Column('status', sa.Enum('PENDING', 'APPROVED', 'REJECTED', 'CANCELLED', name='leavestatus'), nullable=True),
sa.Column('approved_by', sa.Integer(), nullable=True),
sa.Column('approved_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('comments', sa.Text(), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
sa.ForeignKeyConstraint(['approved_by'], ['users.id'], ),
sa.ForeignKeyConstraint(['employee_id'], ['employees.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_leave_requests_id'), 'leave_requests', ['id'], unique=False)
# Create payroll_records table
op.create_table('payroll_records',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('employee_id', sa.Integer(), nullable=False),
sa.Column('pay_period_start', sa.Date(), nullable=False),
sa.Column('pay_period_end', sa.Date(), nullable=False),
sa.Column('base_salary', sa.Numeric(precision=10, scale=2), nullable=False),
sa.Column('overtime_hours', sa.Numeric(precision=5, scale=2), nullable=True),
sa.Column('overtime_rate', sa.Numeric(precision=5, scale=2), nullable=True),
sa.Column('bonus', sa.Numeric(precision=10, scale=2), nullable=True),
sa.Column('deductions', sa.Numeric(precision=10, scale=2), nullable=True),
sa.Column('gross_pay', sa.Numeric(precision=10, scale=2), nullable=False),
sa.Column('tax_deductions', sa.Numeric(precision=10, scale=2), nullable=True),
sa.Column('net_pay', sa.Numeric(precision=10, scale=2), nullable=False),
sa.Column('status', sa.Enum('DRAFT', 'PROCESSED', 'PAID', name='payrollstatus'), nullable=True),
sa.Column('processed_by', sa.Integer(), nullable=True),
sa.Column('processed_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
sa.ForeignKeyConstraint(['employee_id'], ['employees.id'], ),
sa.ForeignKeyConstraint(['processed_by'], ['users.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_payroll_records_id'), 'payroll_records', ['id'], unique=False)
# Create attendance_records table
op.create_table('attendance_records',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('employee_id', sa.Integer(), nullable=False),
sa.Column('date', sa.Date(), nullable=False),
sa.Column('clock_in', sa.Time(), nullable=True),
sa.Column('clock_out', sa.Time(), nullable=True),
sa.Column('hours_worked', sa.String(), nullable=True),
sa.Column('status', sa.Enum('PRESENT', 'ABSENT', 'LATE', 'HALF_DAY', name='attendancestatus'), nullable=True),
sa.Column('notes', sa.String(), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
sa.ForeignKeyConstraint(['employee_id'], ['employees.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_attendance_records_id'), 'attendance_records', ['id'], unique=False)
def downgrade() -> None:
op.drop_index(op.f('ix_attendance_records_id'), table_name='attendance_records')
op.drop_table('attendance_records')
op.drop_index(op.f('ix_payroll_records_id'), table_name='payroll_records')
op.drop_table('payroll_records')
op.drop_index(op.f('ix_leave_requests_id'), table_name='leave_requests')
op.drop_table('leave_requests')
op.drop_index(op.f('ix_employees_id'), table_name='employees')
op.drop_index(op.f('ix_employees_employee_id'), table_name='employees')
op.drop_table('employees')
op.drop_index(op.f('ix_departments_name'), table_name='departments')
op.drop_index(op.f('ix_departments_id'), table_name='departments')
op.drop_table('departments')
op.drop_index(op.f('ix_users_id'), table_name='users')
op.drop_index(op.f('ix_users_email'), table_name='users')
op.drop_table('users')