Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

10 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Medicare - Smart Hospital Bed & Patient Allocation System

A full-stack hospital management system demonstrating advanced DBMS concepts including transactions, triggers, stored procedures, views, indexes, and concurrency control.

πŸ—οΈ Tech Stack

  • Backend: Node.js + Express.js
  • Frontend: React + React Router DOM + Tailwind CSS + PostCSS
  • Database: PostgreSQL (pure SQL, no ORM)
  • API Client: Axios
  • Database Driver: node-postgres (pg)

πŸ“‹ Features

  • Patient Management: Register and track patient information
  • Smart Bed Allocation: Real-time bed availability with concurrency-safe assignment
  • Admission System: Transaction-based admission process with automatic bed allocation
  • Waiting List: Priority-based queue for bed assignment
  • Doctor Assignment: Manage doctor-patient relationships
  • Billing System: Automated billing with tax calculation
  • Audit Logs: Track all database changes with triggers
  • Role-Based Access: Admin, Doctor, Staff, and Billing roles

🎯 DBMS Concepts Demonstrated

  1. Transactions: Multi-step admission process with ACID properties
  2. Triggers: Automatic audit logging on INSERT/UPDATE/DELETE
  3. Stored Procedures/Functions: Complex business logic in database
  4. Views: Materialized queries for reporting
  5. Indexes: Performance optimization on frequently queried columns
  6. Constraints: Data integrity with CHECK, FOREIGN KEY, UNIQUE constraints
  7. Row-Level Locking: SELECT FOR UPDATE to prevent race conditions
  8. Parameterized Queries: SQL injection prevention

πŸš€ Setup Instructions

Prerequisites

  • Node.js (v18 or higher)
  • PostgreSQL (v14 or higher)
  • npm or yarn

Step 1: Database Setup

  1. Install PostgreSQL and create a new database:
psql -U postgres
CREATE DATABASE medicare;
\q
  1. Copy environment files:
# Root level
cp .env.example .env

# Backend level
cp backend/.env.example backend/.env
  1. Edit .env files with your PostgreSQL credentials:
DB_HOST=localhost
DB_PORT=5432
DB_NAME=medicare
DB_USER=postgres
DB_PASSWORD=your_password
JWT_SECRET=your_secret_key

Step 2: Run Database Migrations

On Windows (PowerShell):

cd db
# Run each SQL file in order
Get-Content schema.sql | psql -U postgres -d medicare
Get-Content functions.sql | psql -U postgres -d medicare
Get-Content triggers.sql | psql -U postgres -d medicare
Get-Content indexes_and_views.sql | psql -U postgres -d medicare
Get-Content seed.sql | psql -U postgres -d medicare

On Linux/Mac:

cd db
chmod +x run_migrations.sh
./run_migrations.sh

Or manually:

psql -U postgres -d medicare -f db/schema.sql
psql -U postgres -d medicare -f db/functions.sql
psql -U postgres -d medicare -f db/triggers.sql
psql -U postgres -d medicare -f db/indexes_and_views.sql
psql -U postgres -d medicare -f db/seed.sql

Step 3: Install Dependencies

# Install root dependencies
npm install

# Install all dependencies (backend + frontend)
npm run install-all

Step 4: Run the Application

Development Mode (Both servers concurrently):

npm run dev

This will start:

Or run individually:

# Terminal 1 - Backend
cd backend
npm run dev

# Terminal 2 - Frontend
cd frontend
npm run dev

Step 5: Test with Postman

Import the postman_collection_medicare.json file into Postman to test all API endpoints.

πŸ“ Project Structure

medicare/
β”œβ”€β”€ README.md
β”œβ”€β”€ package.json
β”œβ”€β”€ .env.example
β”œβ”€β”€ db/
β”‚   β”œβ”€β”€ schema.sql              # Database schema with constraints
β”‚   β”œβ”€β”€ functions.sql            # Stored procedures and functions
β”‚   β”œβ”€β”€ triggers.sql             # Audit triggers
β”‚   β”œβ”€β”€ indexes_and_views.sql    # Performance optimizations
β”‚   β”œβ”€β”€ seed.sql                 # Sample data
β”‚   └── run_migrations.sh        # Migration script
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ package.json
β”‚   β”œβ”€β”€ .env.example
β”‚   β”œβ”€β”€ server.js
β”‚   β”œβ”€β”€ config/
β”‚   β”‚   └── db.js               # PostgreSQL connection pool
β”‚   β”œβ”€β”€ controllers/            # Request handlers
β”‚   β”œβ”€β”€ routes/                 # API routes
β”‚   β”œβ”€β”€ services/               # Business logic with transactions
β”‚   └── utils/                  # Helper functions
└── frontend/
    β”œβ”€β”€ package.json
    β”œβ”€β”€ tailwind.config.js
    β”œβ”€β”€ postcss.config.js
    β”œβ”€β”€ src/
    β”‚   β”œβ”€β”€ main.jsx
    β”‚   β”œβ”€β”€ App.jsx
    β”‚   β”œβ”€β”€ api/                # Axios configuration
    β”‚   β”œβ”€β”€ pages/              # React page components
    β”‚   └── components/         # Reusable UI components
    └── public/

πŸ”Œ API Endpoints

Authentication

  • POST /api/auth/login - User login
  • POST /api/auth/register - User registration

Patients

  • GET /api/patients - List all patients
  • GET /api/patients/:id - Get patient details
  • POST /api/patients - Register new patient
  • PUT /api/patients/:id - Update patient
  • DELETE /api/patients/:id - Delete patient

Admissions

  • POST /api/admissions - Create admission (with transaction)
  • GET /api/admissions - List admissions
  • GET /api/admissions/:id - Get admission details
  • PUT /api/admissions/:id/discharge - Discharge patient

Beds

  • GET /api/beds - List all beds with availability
  • GET /api/beds/available - Get available beds
  • PUT /api/beds/:id/status - Update bed status

Doctors

  • GET /api/doctors - List all doctors
  • GET /api/doctors/:id/patients - Get doctor's patients

Billing

  • POST /api/billing - Generate bill
  • GET /api/billing/:admissionId - Get bill for admission
  • PUT /api/billing/:id/pay - Mark bill as paid

Reports

  • GET /api/reports/occupancy - Bed occupancy report
  • GET /api/reports/revenue - Revenue report
  • GET /api/reports/waiting-list - Waiting list report

πŸ‘₯ Default Users (from seed data)

  • Admin: username: admin, password: admin123
  • Doctor: username: doctor1, password: doctor123
  • Staff: username: staff1, password: staff123
  • Billing: username: billing1, password: billing123

πŸ§ͺ Testing

  1. Start the application
  2. Import Postman collection
  3. Use the login endpoint to get a JWT token
  4. Test various endpoints with the token

πŸ“ Sample Workflows

Admitting a Patient

  1. Create/Select a patient
  2. Check available beds: GET /api/beds/available
  3. Create admission: POST /api/admissions (uses transaction)
  4. System automatically:
    • Assigns bed
    • Updates bed status
    • Creates audit log entry
    • Triggers any relevant stored procedures

Discharging a Patient

  1. Discharge: PUT /api/admissions/:id/discharge
  2. Generate bill: POST /api/billing
  3. Mark payment: PUT /api/billing/:id/pay

πŸ”’ Security Features

  • Parameterized queries (prevents SQL injection)
  • Password hashing with bcrypt
  • JWT authentication
  • Input validation
  • Transaction rollback on errors

πŸ“Š Database Features

  • Normalization: 3NF normalized schema
  • Referential Integrity: Foreign key constraints
  • Data Validation: CHECK constraints
  • Audit Trail: Automatic logging via triggers
  • Concurrency Control: Row-level locking for bed allocation
  • Performance: Strategic indexes on foreign keys and query columns

🀝 Contributing

This is an educational project demonstrating DBMS concepts. Feel free to extend it with additional features.

πŸ“„ License

MIT License

About

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages