Skip to content

Commit 762689d

Browse files
committed
chore: Commit pending changes (SMTP, API utils, Seeding)
1 parent ccb779a commit 762689d

File tree

3 files changed

+129
-8
lines changed

3 files changed

+129
-8
lines changed

backend/email_service.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
def send_booking_confirmation(to_email: str, patient_name: str, doctor_name: str, date_time: str, link: str):
1414
"""
1515
Simulates sending a booking confirmation email.
16-
In production, this would use SMTP or SendGrid.
16+
If SMTP_SERVER is set, it sends a REAL email.
17+
Otherwise, logs it.
1718
"""
1819
try:
1920
# Construct Email Body
@@ -32,13 +33,32 @@ def send_booking_confirmation(to_email: str, patient_name: str, doctor_name: str
3233
AI Healthcare System
3334
"""
3435

35-
# Log instead of Send (Safety First)
36-
logger.info(f"📧 [EMAIL SENT TO {to_email}] Subject: {subject}")
37-
logger.info(f"📧 Body: {body.strip()}")
38-
39-
# If we had SMTP credentials, we would use them here:
40-
# if os.getenv("SMTP_SERVER"):
41-
# send_smtp_email(...)
36+
smtp_server = os.getenv("SMTP_SERVER")
37+
smtp_port = int(os.getenv("SMTP_PORT", 587))
38+
smtp_user = os.getenv("SMTP_EMAIL")
39+
smtp_password = os.getenv("SMTP_PASSWORD")
40+
41+
if smtp_server and smtp_user:
42+
import smtplib
43+
from email.mime.text import MIMEText
44+
from email.mime.multipart import MIMEMultipart
45+
46+
msg = MIMEMultipart()
47+
msg['From'] = smtp_user
48+
msg['To'] = to_email
49+
msg['Subject'] = subject
50+
msg.attach(MIMEText(body, 'plain'))
51+
52+
server = smtplib.SMTP(smtp_server, smtp_port)
53+
server.starttls()
54+
server.login(smtp_user, smtp_password)
55+
text = msg.as_string()
56+
server.sendmail(smtp_user, to_email, text)
57+
server.quit()
58+
logger.info(f"✅ Real Email sent to {to_email}")
59+
else:
60+
# Fallback to simulation
61+
logger.info(f"📧 [SIMULATION] To: {to_email} | Subject: {subject}")
4262

4363
return True
4464
except Exception as e:

frontend/utils/api.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,36 @@ def reschedule_appointment(appointment_id: int, date, time) -> bool:
272272
except Exception as e:
273273
st.error(f"Error: {e}")
274274
return False
275+
276+
def delete_user(user_id: int) -> bool:
277+
if 'token' not in st.session_state: return False
278+
try:
279+
resp = requests.delete(f"{BACKEND_URL}/admin/users/{user_id}", headers=_headers())
280+
if resp.status_code == 200:
281+
return True
282+
st.error(f"User Deletion Failed: {resp.json().get('detail')}")
283+
except Exception as e:
284+
st.error(f"Error: {e}")
285+
return False
286+
287+
def delete_appointment(appointment_id: int) -> bool:
288+
if 'token' not in st.session_state: return False
289+
try:
290+
resp = requests.delete(f"{BACKEND_URL}/appointments/{appointment_id}", headers=_headers())
291+
if resp.status_code == 200:
292+
return True
293+
st.error(f"Appointment Deletion Failed: {resp.json().get('detail')}")
294+
except Exception as e:
295+
st.error(f"Error: {e}")
296+
return False
297+
298+
def update_user_role(user_id: int, role: str) -> bool:
299+
if 'token' not in st.session_state: return False
300+
try:
301+
resp = requests.put(f"{BACKEND_URL}/admin/users/{user_id}/role", params={"role": role}, headers=_headers())
302+
if resp.status_code == 200:
303+
return True
304+
st.error("Update failed")
305+
except Exception as e:
306+
st.error(f"Error: {e}")
307+
return False

seed_data.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
from backend import database, models, auth
2+
import sys
3+
4+
def seed_data():
5+
print("Starting database seeding...")
6+
db = database.SessionLocal()
7+
try:
8+
# --- SEED ADMIN ---
9+
admin = db.query(models.User).filter(models.User.role == "admin").first()
10+
if not admin:
11+
print("Creating Admin user...")
12+
hashed_pw = auth.get_password_hash("admin123")
13+
admin_user = models.User(
14+
username="admin",
15+
hashed_password=hashed_pw,
16+
email="admin@hospital.com",
17+
full_name="System Administrator",
18+
role="admin",
19+
allow_data_collection=0,
20+
dob="1980-01-01" # Ensure DOB is present if schema requires it
21+
)
22+
db.add(admin_user)
23+
print("Admin created: admin / admin123")
24+
else:
25+
print("Admin already exists.")
26+
27+
# --- SEED DOCTOR ---
28+
doctor = db.query(models.User).filter(models.User.role == "doctor").first()
29+
if not doctor:
30+
print("Creating Doctor user...")
31+
hashed_pw = auth.get_password_hash("doctor123")
32+
doctor_user = models.User(
33+
username="dr_smith",
34+
hashed_password=hashed_pw,
35+
email="dr.smith@hospital.com",
36+
full_name="Dr. Adam Smith",
37+
role="doctor",
38+
# consultation_fee=150.0, # Commenting out as column might not exist yet (migration script handles it but maybe not run yet)
39+
# specialization="General Physician",
40+
profile_picture="https://i.pravatar.cc/150?u=dr_smith",
41+
dob="1975-05-20",
42+
allow_data_collection=1
43+
)
44+
# handle potential missing columns by checking getattr or just try/except
45+
# For now, let's assume the basic User model works and add extras if columns exist
46+
# The 'run_migrations' in main.py should have added them, but let's be safe.
47+
try:
48+
doctor_user.consultation_fee = 150.0
49+
doctor_user.specialization = "General Physician"
50+
except:
51+
pass
52+
53+
db.add(doctor_user)
54+
print("Doctor created: dr_smith / doctor123")
55+
else:
56+
print("Doctor already exists.")
57+
58+
db.commit()
59+
print("Seeding complete!")
60+
61+
except Exception as e:
62+
print(f"Error seeding data: {e}")
63+
db.rollback()
64+
finally:
65+
db.close()
66+
67+
if __name__ == "__main__":
68+
seed_data()

0 commit comments

Comments
 (0)