-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_main_py_before_post_appointments.patch
More file actions
378 lines (364 loc) · 30 KB
/
Copy path_main_py_before_post_appointments.patch
File metadata and controls
378 lines (364 loc) · 30 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
diff --git a/main.py b/main.py
index e9f4bd5..adabcdc 100644
--- a/main.py
+++ b/main.py
@@ -11,13 +11,15 @@ import re
import sqlite3
import traceback
import logging
+import shutil
from datetime import datetime, timedelta, timezone
from typing import List, Optional, Any
-from fastapi import FastAPI, Depends, HTTPException, Query, Request
+from fastapi import FastAPI, Depends, HTTPException, Query, Request, Form
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
-from fastapi.responses import FileResponse, JSONResponse
+from fastapi.responses import FileResponse, JSONResponse, RedirectResponse, HTMLResponse
+from starlette.middleware.sessions import SessionMiddleware
from sqlalchemy.orm import Session
from sqlalchemy import desc, text, func
from pydantic import BaseModel, Field
@@ -39,6 +41,8 @@ from app.api.routes_ai import router as ai_router
from app.api.ai_financial_recordno import router as ai_fin_router
from app.api.routes.engine import router as engine_router
from app.api.financial_operational import router as financial_operational_router
+from app.api.staff.patient_search import router as staff_router
+from app.api.manager.dashboard import router as manager_api_router
from app.routers import frontend_api
# -----------------------------
@@ -69,10 +73,13 @@ app.include_router(ai_router)
app.include_router(ai_fin_router)
app.include_router(engine_router)
app.include_router(financial_operational_router)
+app.include_router(staff_router)
+app.include_router(manager_api_router)
app.include_router(frontend_api.router)
# Patient search must be registered before /patients/{patient_id} to match /patients/search
from app.api.patient_search import search_patients as _search_patients_impl
+from engine.post_import_refresh import PostImportRefreshEngine, RefreshReport
# -----------------------------
# CORS (permissive for local dev)
@@ -85,6 +92,88 @@ app.add_middleware(
allow_headers=["*"],
)
+# -----------------------------
+# Session Middleware & Auth
+# -----------------------------
+app.add_middleware(
+ SessionMiddleware,
+ secret_key="atieh-ai-secret-key",
+)
+
+USERS = {
+ "admin": {"password": "atieh123", "role": "manager"},
+ "operator": {"password": "clinic123", "role": "operator"},
+}
+
+
+def _render_login_form(error: str | None = None, username: str | None = None) -> str:
+ error_html = (
+ f'<div style="color:#ff6b6b; margin-bottom:12px;">{error}</div>'
+ if error
+ else ""
+ )
+ username_value = username or ""
+ return f"""
+ <!DOCTYPE html>
+ <html lang="fa" dir="rtl">
+ <head>
+ <meta charset="UTF-8" />
+ <title>Atieh AI Login</title>
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
+ </head>
+ <body style="margin:0; font-family:system-ui,-apple-system,BlinkMacSystemFont,'Segoe UI',sans-serif; background:#050816; color:#fff; display:flex; align-items:center; justify-content:center; min-height:100vh;">
+ <div style="background:rgba(15,23,42,0.96); border-radius:16px; padding:32px 28px; width:100%; max-width:360px; box-shadow:0 24px 60px rgba(15,23,42,0.9); border:1px solid rgba(148,163,184,0.25);">
+ <h1 style="margin:0 0 8px; font-size:22px;">ورود به داشبورد آتیه</h1>
+ <p style="margin:0 0 20px; font-size:13px; color:#9ca3af;">لطفاً نام کاربری و رمز عبور خود را وارد کنید.</p>
+ {error_html}
+ <form method="post" action="/login">
+ <div style="margin-bottom:12px;">
+ <label for="username" style="display:block; margin-bottom:4px; font-size:13px;">نام کاربری</label>
+ <input id="username" name="username" value="{username_value}" required style="width:100%; padding:10px 12px; border-radius:10px; border:1px solid rgba(148,163,184,0.45); background:rgba(15,23,42,0.85); color:#e5e7eb; font-size:13px; box-sizing:border-box; outline:none;" />
+ </div>
+ <div style="margin-bottom:18px;">
+ <label for="password" style="display:block; margin-bottom:4px; font-size:13px;">رمز عبور</label>
+ <input id="password" type="password" name="password" required style="width:100%; padding:10px 12px; border-radius:10px; border:1px solid rgba(148,163,184,0.45); background:rgba(15,23,42,0.85); color:#e5e7eb; font-size:13px; box-sizing:border-box; outline:none;" />
+ </div>
+ <button type="submit" style="width:100%; padding:10px 12px; border-radius:999px; border:none; background:linear-gradient(135deg,#22c55e,#0ea5e9); color:#0b1120; font-weight:600; font-size:14px; cursor:pointer;">
+ ورود
+ </button>
+ </form>
+ </div>
+ </body>
+ </html>
+ """
+
+
+@app.get("/login", response_class=HTMLResponse)
+async def login_get(request: Request):
+ """نمایش فرم ورود ساده برای داشبورد."""
+ if request.session.get("user"):
+ return RedirectResponse(url="/", status_code=302)
+ return HTMLResponse(content=_render_login_form())
+
+
+@app.post("/login")
+async def login_post(request: Request, username: str = Form(...), password: str = Form(...)):
+ """اعتبارسنجی ورود و ایجاد سشن."""
+ user = USERS.get(username)
+ if not user or user["password"] != password:
+ html = _render_login_form(
+ error="نام کاربری یا رمز عبور نادرست است.",
+ username=username,
+ )
+ return HTMLResponse(content=html, status_code=401)
+
+ request.session["user"] = {"username": username, "role": user["role"]}
+ return RedirectResponse(url="/", status_code=302)
+
+
+@app.get("/logout")
+async def logout(request: Request):
+ """خروج کاربر و پاک کردن سشن."""
+ request.session.clear()
+ return RedirectResponse(url="/login", status_code=302)
+
# -----------------------------
# Lightweight SQLite helper (for simple read-only endpoints)
@@ -138,8 +227,10 @@ if os.path.exists(static_dir):
app.mount("/static", StaticFiles(directory=static_dir), name="static")
@app.get("/")
- async def read_root():
- """صفحه اصلی رابط کاربری"""
+ async def read_root(request: Request):
+ """صفحه اصلی رابط کاربری (محافظت‌شده با سشن)."""
+ if not request.session.get("user"):
+ return RedirectResponse(url="/login", status_code=302)
return FileResponse(
os.path.join(static_dir, "index.html"),
media_type="text/html"
@@ -149,6 +240,26 @@ public_dir = os.path.join(os.path.dirname(__file__), "public")
if os.path.exists(public_dir):
app.mount("/public", StaticFiles(directory=public_dir), name="public")
+# Manager dashboard page (standalone HTML)
+_templates_dir = os.path.join(os.path.dirname(__file__), "templates")
+_manager_dashboard_path = os.path.join(_templates_dir, "manager_dashboard.html")
+
+
+@app.get("/manager")
+async def manager_dashboard_page(request: Request):
+ """Manager dashboard ΓÇô financial insights (login may be required)."""
+ if os.path.exists(_manager_dashboard_path):
+ return FileResponse(_manager_dashboard_path, media_type="text/html")
+ return RedirectResponse(url="/#/manager", status_code=302)
+
+
+@app.get("/staff/search")
+async def staff_search_page(request: Request):
+ """Staff patient search page ΓÇô serves main SPA on patient search route."""
+ if not request.session.get("user"):
+ return RedirectResponse(url="/login", status_code=302)
+ return RedirectResponse(url="/#/patients", status_code=302)
+
# -----------------------------
# CORS
@@ -161,24 +272,12 @@ app.add_middleware(
allow_headers=["*"],
)
# =============================
-# Compat API for tests (v1)
+# Health Check (used by launcher to detect API readiness)
# =============================
@app.get("/health")
def health():
- crm_mode = os.environ.get("CRM_MODE", "mock")
- try:
- crm_client = get_crm_client_dependency()
- crm_healthy = crm_client is not None
- except Exception:
- crm_healthy = False
- return {
- "status": "ok",
- "version": app.version,
- "crm_mode": crm_mode,
- "crm_healthy": crm_healthy,
- "timestamp": datetime.now(timezone.utc).isoformat(),
- }
+ return {"ok": True, "service": "Atieh AI"}
def _clamp01(x: float) -> float:
@@ -478,6 +577,176 @@ def debug_db():
return {"db_path": p, "patients_count": count, "sample": sample}
+@app.get("/backup-db")
+async def backup_db():
+ """
+ دستی گرفتن نسخه پشتیبان از پایگاه داده کاری atieh_clinic_working.db
+ و ذخیره آن در پوشه backups با نام timestamp شده.
+ """
+ project_root = os.path.dirname(os.path.abspath(__file__))
+ db_name = "atieh_clinic_working.db"
+ db_path = os.path.join(project_root, db_name)
+
+ if not os.path.exists(db_path):
+ return {"ok": False, "error": "database file not found"}
+
+ backups_dir = os.path.join(project_root, "backups")
+ os.makedirs(backups_dir, exist_ok=True)
+
+ ts = datetime.now().strftime("%Y%m%d_%H%M%S")
+ backup_filename = f"atieh_clinic_working_{ts}.db"
+ backup_path = os.path.join(backups_dir, backup_filename)
+
+ shutil.copy2(db_path, backup_path)
+
+ return {
+ "ok": True,
+ "backup_file": f"backups/{backup_filename}",
+ }
+
+
+# =============================
+# Import Center v1
+# =============================
+
+IMPORT_INCOMING = r"C:\AtiehAI\incoming"
+IMPORT_PROCESSED = r"C:\AtiehAI\processed"
+IMPORT_FAILED = r"C:\AtiehAI\failed"
+IMPORT_EXTENSIONS = (".csv", ".xlsx", ".xls")
+IMPORT_DB_NAME = "atieh_clinic_working.db"
+
+
+def _get_import_db_conn():
+ """Return SQLite connection to atieh_clinic_working.db for import_runs_v1."""
+ project_root = os.path.dirname(os.path.abspath(__file__))
+ db_path = os.path.join(project_root, IMPORT_DB_NAME)
+ conn = sqlite3.connect(db_path)
+ conn.row_factory = sqlite3.Row
+ return conn
+
+
+def _scan_incoming_files():
+ """Scan C:\\AtiehAI\\incoming for supported files (.csv, .xlsx, .xls)."""
+ if not os.path.exists(IMPORT_INCOMING):
+ return []
+ files = []
+ for name in os.listdir(IMPORT_INCOMING):
+ path = os.path.join(IMPORT_INCOMING, name)
+ if os.path.isfile(path) and any(name.lower().endswith(ext) for ext in IMPORT_EXTENSIONS):
+ files.append((name, path))
+ return files
+
+
+@app.post("/imports/run")
+async def imports_run():
+ """Import Center v1: scan incoming, create import_runs_v1 records, move files to processed or failed."""
+ files = _scan_incoming_files()
+ if not files:
+ return {"ok": True, "message": "no files found", "processed_count": 0, "refresh": None}
+
+ project_root = os.path.dirname(os.path.abspath(__file__))
+ db_path = os.path.join(project_root, IMPORT_DB_NAME)
+ if not os.path.exists(db_path):
+ return {"ok": False, "error": "database file not found", "processed_count": 0, "refresh": None}
+
+ os.makedirs(IMPORT_PROCESSED, exist_ok=True)
+ os.makedirs(IMPORT_FAILED, exist_ok=True)
+
+ processed_count = 0
+ conn = _get_import_db_conn()
+ cur = conn.cursor()
+
+ for file_name, file_path in files:
+ file_size = os.path.getsize(file_path) if os.path.exists(file_path) else 0
+ started_at = datetime.now().isoformat()
+ run_id = None
+
+ try:
+ cur.execute(
+ """
+ INSERT INTO import_runs_v1 (file_name, file_path, file_size, status, rows_loaded, started_at, finished_at, notes)
+ VALUES (?, ?, ?, 'PENDING', 0, ?, NULL, 'v1 intake started')
+ """,
+ (file_name, file_path, file_size, started_at),
+ )
+ run_id = cur.lastrowid
+ conn.commit()
+
+ shutil.move(file_path, os.path.join(IMPORT_PROCESSED, file_name))
+ finished_at = datetime.now().isoformat()
+ cur.execute(
+ """
+ UPDATE import_runs_v1 SET status = 'SUCCESS', finished_at = ?, rows_loaded = 0, notes = 'v1 intake completed'
+ WHERE id = ?
+ """,
+ (finished_at, run_id),
+ )
+ conn.commit()
+ processed_count += 1
+ except Exception as e:
+ finished_at = datetime.now().isoformat()
+ notes = str(e)
+ if run_id is not None:
+ cur.execute(
+ "UPDATE import_runs_v1 SET status = 'FAILED', finished_at = ?, notes = ? WHERE id = ?",
+ (finished_at, notes, run_id),
+ )
+ conn.commit()
+ try:
+ if os.path.exists(file_path):
+ shutil.move(file_path, os.path.join(IMPORT_FAILED, file_name))
+ except Exception:
+ pass
+
+ conn.close()
+
+ refresh_report = None
+ if processed_count > 0:
+ try:
+ refresh_engine = PostImportRefreshEngine(db_path=db_path)
+ refresh_report = refresh_engine.run()
+ except Exception as e:
+ logger.exception("Post-import refresh failed: %s", e)
+ refresh_report = RefreshReport(
+ ok=False,
+ total_duration_ms=0,
+ steps=[],
+ message=str(e),
+ )
+
+ return {
+ "ok": True,
+ "processed_count": processed_count,
+ "refresh": {
+ "ok": refresh_report.ok,
+ "total_duration_ms": refresh_report.total_duration_ms,
+ "steps": refresh_report.steps,
+ "message": refresh_report.message,
+ } if refresh_report else None,
+ }
+
+
+@app.get("/imports/history")
+def imports_history():
+ """Return latest 50 rows from import_runs_v1, ordered by id desc."""
+ project_root = os.path.dirname(os.path.abspath(__file__))
+ db_path = os.path.join(project_root, IMPORT_DB_NAME)
+ if not os.path.exists(db_path):
+ return {"ok": False, "error": "database file not found", "data": []}
+
+ conn = _get_import_db_conn()
+ try:
+ cur = conn.cursor()
+ cur.execute(
+ "SELECT id, file_name, file_path, file_size, status, rows_loaded, started_at, finished_at, notes FROM import_runs_v1 ORDER BY id DESC LIMIT 50"
+ )
+ rows = cur.fetchall()
+ data = [dict(r) for r in rows]
+ return {"ok": True, "data": data}
+ finally:
+ conn.close()
+
+
# =============================
# Appointments routes (order matters for tests)
# Static first, then dynamic