Files
leave-agent/docs/leave-agent-design.md
purvarao 1ecaf8ff95
Some checks failed
Cloudflare Worker API Template / Deploy to ${{ github.ref_name }} environment (pull_request) Failing after 15s
api
2025-11-27 19:14:46 +05:30

6.0 KiB

Leave Agent - Database Design

Database Schema

1. Employees

Stores employee information and their leave balances.

CREATE TABLE employees (
  employee_id INTEGER PRIMARY KEY AUTOINCREMENT,
  name TEXT NOT NULL,
  email TEXT UNIQUE NOT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

2. Leave Types

Defines different types of leaves available in the system.

CREATE TABLE leave_types (
  leave_types_id INTEGER PRIMARY KEY AUTOINCREMENT,
  name TEXT UNIQUE NOT NULL,  -- e.g., "Annual Leave", "Sick Leave"
  description TEXT,
  max_days INTEGER NOT NULL,
  is_active BOOLEAN DEFAULT true,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

3. Leave Balances

Tracks the number of leave days available for each employee by leave type.

CREATE TABLE leave_balances (
  leave_balances_id INTEGER PRIMARY KEY AUTOINCREMENT,
  employee_id INTEGER NOT NULL,
  leave_type_id INTEGER NOT NULL,
  balance_days REAL NOT NULL,
  year INTEGER NOT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (employee_id) REFERENCES employees(employee_id) ON DELETE CASCADE,
  FOREIGN KEY (leave_type_id) REFERENCES leave_types(leave_types_id) ON DELETE CASCADE,
  UNIQUE(employee_id, leave_type_id, year)
);

4. Leave Requests

Tracks all leave requests made by employees.

CREATE TABLE leave_requests (
  leave_requests_id INTEGER PRIMARY KEY AUTOINCREMENT,
  employee_id INTEGER NOT NULL,
  leave_type_id INTEGER NOT NULL,
  start_date DATE NOT NULL,
  end_date DATE NOT NULL,
  reason TEXT,
  status TEXT NOT NULL,  -- 'pending', 'approved', 'rejected', 'cancelled'
  business_days REAL NOT NULL,
  notes TEXT,
  approved_by INTEGER,
  approved_at TIMESTAMP,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (employee_id) REFERENCES employees(employee_id) ON DELETE CASCADE,
  FOREIGN KEY (leave_type_id) REFERENCES leave_types(leave_types_id) ON DELETE CASCADE,
  FOREIGN KEY (approved_by) REFERENCES employees(employee_id) ON DELETE SET NULL
);

5. Holidays

Stores public holidays.

CREATE TABLE holidays (
  holidays_id INTEGER PRIMARY KEY AUTOINCREMENT,
  date DATE NOT NULL,
  name TEXT NOT NULL,
  description TEXT,
  is_recurring BOOLEAN DEFAULT false,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  UNIQUE(date, name)
);

6. Team Events

Stores company events and team activities.

CREATE TABLE team_events (
  team_events_id INTEGER PRIMARY KEY AUTOINCREMENT,
  name TEXT NOT NULL,
  description TEXT,
  start_date DATE NOT NULL,
  end_date DATE,
  is_recurring BOOLEAN DEFAULT false,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Entity Relationship Diagram

erDiagram
    employees ||--o{ leave_balances : has
    employees ||--o{ leave_requests : makes
    leave_types ||--o{ leave_balances : has
    leave_types ||--o{ leave_requests : has
    employees ||--o{ leave_requests : approves

    employees {
        int employee_id PK
        string name
        string email
        timestamp created_at
        timestamp updated_at
    }
    
    leave_types {
        int leave_types_id PK
        string name
        string description
        int max_days
        boolean is_active
        timestamp created_at
    }
    
    leave_balances {
        int leave_balances_id PK
        int employee_id FK
        int leave_type_id FK
        real balance_days
        int year
        timestamp created_at
        timestamp updated_at
    }
    
    leave_requests {
        int leave_requests_id PK
        int employee_id FK
        int leave_type_id FK
        date start_date
        date end_date
        string reason
        string status
        real business_days
        string notes
        int approved_by FK
        timestamp approved_at
        timestamp created_at
        timestamp updated_at
    }
    
    holidays {
        int holidays_id PK
        date date
        string name
        string description
        boolean is_recurring
        timestamp created_at
    }
    
    team_events {
        int team_events_id PK
        string name
        string description
        date start_date
        date end_date
        boolean is_recurring
        timestamp created_at
        timestamp updated_at
    }

Initial Data Setup

Default Leave Types

INSERT INTO leave_types (name, description, max_days, is_active) VALUES
('Annual Leave', 'Paid time off for vacation or personal reasons', 24, true),
('Sick Leave', 'Paid time off for illness or medical appointments', 12, true),
('Maternity Leave', 'Paid leave for new mothers', 182, true),
('Paternity Leave', 'Paid leave for new fathers', 14, true),
('Bereavement Leave', 'Paid time off following the death of a family member', 5, true),
('Marriage Leave', 'Paid time off for employee''s own marriage', 5, true);

Sample Holidays (2025)

INSERT INTO holidays (date, name, description, is_recurring) VALUES
('2025-01-01', 'New Year''s Day', 'First day of the year', true),
('2025-01-26', 'Republic Day', 'Indian Republic Day', true),
('2025-03-17', 'Holi', 'Festival of Colors', true),
('2025-04-14', 'Ambedkar Jayanti', 'Birth anniversary of Dr. B.R. Ambedkar', true),
('2025-08-15', 'Independence Day', 'Indian Independence Day', true),
('2025-10-02', 'Gandhi Jayanti', 'Birth anniversary of Mahatma Gandhi', true),
('2025-12-25', 'Christmas Day', 'Christmas holiday', true);

Indexes

For better query performance, the following indexes are recommended:

-- Indexes for leave_requests
CREATE INDEX idx_leave_requests_employee_id ON leave_requests(employee_id);
CREATE INDEX idx_leave_requests_status ON leave_requests(status);
CREATE INDEX idx_leave_requests_dates ON leave_requests(start_date, end_date);

-- Indexes for leave_balances
CREATE INDEX idx_leave_balances_employee ON leave_balances(employee_id, year);