feat(M06): Add Department and UserDepartment models, seed 12 DMH departments, and enable department-level permissions
This commit is contained in:
+102
-2
@@ -9,14 +9,31 @@ sys.path.insert(0, str(backend_dir))
|
||||
from sqlalchemy.orm import Session
|
||||
from app.database import engine, Base, SessionLocal
|
||||
from app.models.user import User, UserRole
|
||||
from app.models.department import Department, UserDepartment
|
||||
from app.models.dataset import Dataset, DatasetColumn
|
||||
from app.models.permission import DatasetPermission, ColumnPermission, RowRule
|
||||
from app.core.security import hash_password
|
||||
from app.services.ingestion_service import register_dataset_from_csv
|
||||
from app.services.permission_service import save_dataset_permission
|
||||
from app.services.department_service import add_user_to_department
|
||||
from app.schemas.permission import DatasetPermissionCreate, RowRuleCreate
|
||||
from app.config import settings
|
||||
|
||||
DMH_DEPARTMENTS = [
|
||||
{"code": "ADMISSION", "name": "Admission & Registration", "desc": "Registration overview, demographics, and patient intake"},
|
||||
{"code": "OPD", "name": "Outpatient Department (OPD)", "desc": "Visit performance, clinic consultations, and doctor schedules"},
|
||||
{"code": "IPD", "name": "Inpatient Department (IPD)", "desc": "Inpatient admissions, discharges, ward occupancy, and length of stay"},
|
||||
{"code": "PHARMACY", "name": "Pharmacy & Dispensary", "desc": "Medication dispensing, inventory stock, and procurement"},
|
||||
{"code": "FINANCE", "name": "Finance & Accounts", "desc": "Hospital billing, revenue streams, collections, and outstanding balances"},
|
||||
{"code": "INSURANCE", "name": "Insurance & TPA", "desc": "TPA cashless processing, claims submission, settlements, and rejections"},
|
||||
{"code": "LABORATORY", "name": "Laboratory & Pathology", "desc": "Clinical pathology, test volumes, turnaround times, and pending samples"},
|
||||
{"code": "RADIOLOGY", "name": "Radiology & Imaging", "desc": "CT, MRI, X-ray, Ultrasound volume, and equipment utilization"},
|
||||
{"code": "HR", "name": "Human Resources (HR)", "desc": "Hospital workforce, staffing rosters, attendance, and shift analytics"},
|
||||
{"code": "BIOMEDICAL", "name": "Biomedical Engineering", "desc": "Medical equipment uptime, maintenance cycles, and breakdown tracking"},
|
||||
{"code": "OT", "name": "Operation Theatre (OT)", "desc": "Surgical procedures, OT suite utilization, and elective case turnover"},
|
||||
{"code": "MANAGEMENT", "name": "Executive Management", "desc": "Consolidated hospital KPIs, executive overview, and strategic metrics"},
|
||||
]
|
||||
|
||||
|
||||
def seed_database():
|
||||
print("Initializing database tables...")
|
||||
@@ -84,7 +101,41 @@ def seed_database():
|
||||
|
||||
db.commit()
|
||||
|
||||
# 5. Seed Admission Dataset
|
||||
# 5. Seed 12 DMH Departments
|
||||
print("Seeding 12 DMH clinical and administrative departments...")
|
||||
dept_map = {}
|
||||
for d_info in DMH_DEPARTMENTS:
|
||||
existing_dept = db.query(Department).filter(Department.code == d_info["code"]).first()
|
||||
if not existing_dept:
|
||||
dept_obj = Department(
|
||||
code=d_info["code"],
|
||||
name=d_info["name"],
|
||||
description=d_info["desc"],
|
||||
is_active=True
|
||||
)
|
||||
db.add(dept_obj)
|
||||
db.flush()
|
||||
dept_map[d_info["code"]] = dept_obj
|
||||
else:
|
||||
dept_map[d_info["code"]] = existing_dept
|
||||
db.commit()
|
||||
|
||||
# Assign Users to Departments
|
||||
print("Assigning users to departments...")
|
||||
if "MANAGEMENT" in dept_map and admin_user:
|
||||
add_user_to_department(db, dept_map["MANAGEMENT"].id, admin_user.id, is_head=True)
|
||||
if "IPD" in dept_map and dept_user:
|
||||
add_user_to_department(db, dept_map["IPD"].id, dept_user.id, is_head=True)
|
||||
if "OPD" in dept_map and dept_user:
|
||||
add_user_to_department(db, dept_map["OPD"].id, dept_user.id, is_head=False)
|
||||
if "FINANCE" in dept_map and fin_user:
|
||||
add_user_to_department(db, dept_map["FINANCE"].id, fin_user.id, is_head=True)
|
||||
if "INSURANCE" in dept_map and fin_user:
|
||||
add_user_to_department(db, dept_map["INSURANCE"].id, fin_user.id, is_head=False)
|
||||
if "ADMISSION" in dept_map and viewer_user:
|
||||
add_user_to_department(db, dept_map["ADMISSION"].id, viewer_user.id, is_head=False)
|
||||
|
||||
# 6. Seed Admission Dataset
|
||||
dataset_name = "Hospital Admissions 2025"
|
||||
ds = db.query(Dataset).filter(Dataset.name == dataset_name).first()
|
||||
if not ds:
|
||||
@@ -158,7 +209,56 @@ def seed_database():
|
||||
]
|
||||
)
|
||||
)
|
||||
print("Role permissions seeded successfully!")
|
||||
# Department-level dataset permissions
|
||||
if "IPD" in dept_map:
|
||||
save_dataset_permission(
|
||||
db,
|
||||
DatasetPermissionCreate(
|
||||
dataset_id=ds.id,
|
||||
department_id=dept_map["IPD"].id,
|
||||
can_view=True,
|
||||
can_export=True,
|
||||
hidden_column_ids=[],
|
||||
row_rules=[
|
||||
RowRuleCreate(
|
||||
column_name="AdmittingDoctorSpecialityName",
|
||||
operator="eq",
|
||||
rule_value="MEDICINE"
|
||||
)
|
||||
]
|
||||
)
|
||||
)
|
||||
if "FINANCE" in dept_map:
|
||||
save_dataset_permission(
|
||||
db,
|
||||
DatasetPermissionCreate(
|
||||
dataset_id=ds.id,
|
||||
department_id=dept_map["FINANCE"].id,
|
||||
can_view=True,
|
||||
can_export=True,
|
||||
hidden_column_ids=[],
|
||||
row_rules=[
|
||||
RowRuleCreate(
|
||||
column_name="PatientCategoryName",
|
||||
operator="eq",
|
||||
rule_value="TPA"
|
||||
)
|
||||
]
|
||||
)
|
||||
)
|
||||
if "ADMISSION" in dept_map:
|
||||
save_dataset_permission(
|
||||
db,
|
||||
DatasetPermissionCreate(
|
||||
dataset_id=ds.id,
|
||||
department_id=dept_map["ADMISSION"].id,
|
||||
can_view=True,
|
||||
can_export=False,
|
||||
hidden_column_ids=[],
|
||||
row_rules=[]
|
||||
)
|
||||
)
|
||||
print("Role and Department permissions seeded successfully!")
|
||||
|
||||
print("Seeding completed successfully!")
|
||||
finally:
|
||||
|
||||
Reference in New Issue
Block a user