-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
187 lines (160 loc) · 5.59 KB
/
db.py
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
# IMPORTS
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
from dotenv import load_dotenv
import os
from datetime import datetime, timedelta
# SETUP
load_dotenv()
client = MongoClient(os.getenv("ATLAS_URI"), server_api=ServerApi('1'))
db = client[os.getenv("DB_NAME")]
patients_collection = db["patients"]
appointment_queue_collection = db["appointment_queue"]
# FUNCTIONS
# Registrations
def patient_exists(telegram_id: int) -> bool:
'''
Returns True if the patient exists in the database, False otherwise
'''
return patients_collection.count_documents({"telegram_id": telegram_id}) != 0
def create_patient(telegram_id: int) -> None:
'''
Creates a new empty patient in the database with empty fields
'''
new_patient = {
"telegram_id": telegram_id,
"has_registered": False,
"is_sick": False,
"name": None,
"age": None,
"sex": None,
"reg_no": None,
"block": None,
"room_no": None,
"phone_no": None,
"consultations": []
}
patients_collection.insert_one(new_patient)
def get_patient(telegram_id: int) -> dict:
'''
Returns the patient with the given telegram_id
'''
return patients_collection.find_one({"telegram_id": telegram_id})
def patient_has_registered(telegram_id: int) -> bool:
'''
Returns True if the patient has registered (If the empty fields have been filled), False otherwise
'''
patient = get_patient(telegram_id)
return patient["has_registered"]
def register_patient(telegram_id: int, name: str, age: int, sex: str, reg_no: str, block: str, room_no: int, phone_no: int) -> None:
'''
Fills the empty fields of the patient with the given details
'''
patients_collection.update_one(
{"telegram_id": telegram_id},
{"$set": {
"has_registered": True,
"name": name,
"age": age,
"sex": sex,
"reg_no": reg_no,
"block": block,
"room_no": room_no,
"phone_no": phone_no
}}
)
def make_patient_sick(telegram_id: int) -> None:
'''
Makes the patient with the given telegram_id sick
'''
patients_collection.update_one(
{"telegram_id": telegram_id},
{"$set": {"is_sick": True}}
)
def get_sick_patients() -> list:
'''
Returns a list of all the sick patients
'''
return patients_collection.find({"is_sick": True})
def is_patient_sick(telegram_id: int) -> bool:
'''
Returns True if the patient with the given telegram_id is sick, False otherwise
'''
patient = get_patient(telegram_id)
return patient["is_sick"]
def get_patient_by_reg_no(reg_no: str) -> dict:
'''
Returns the patient with the given registration number
'''
return patients_collection.find_one({"reg_no": reg_no})
# Appointments
def create_appointment(telegram_id: int) -> None:
'''
Creates a new appointment for the patient with the given telegram_id
'''
make_patient_sick(telegram_id)
patient = get_patient(telegram_id)
new_appointment = {
"telegram_id": patient["telegram_id"],
"name": patient["name"],
"age": patient["age"],
"sex": patient["sex"],
"phone_no": patient["phone_no"],
"time": datetime.now(),
"is_active": True,
}
appointment_queue_collection.insert_one(new_appointment)
def appointment_exists(telegram_id: int) -> bool:
'''
Returns True if the patient with the given telegram_id has an active appointment, False otherwise
'''
return appointment_queue_collection.count_documents({"telegram_id": telegram_id, "is_active": True}) != 0
def close_appointment(telegram_id: int) -> None:
'''
Closes the active appointment for the patient with the given telegram_id
'''
appointment_queue_collection.update_one(
{"telegram_id": telegram_id, "is_active": True},
{"$set": {"is_active": False}}
)
def get_appointment_queue_size() -> int:
'''
Returns the number of active appointments
'''
return appointment_queue_collection.count_documents({})
def get_all_appointments() -> list:
'''
Returns a list of all the appointments
'''
return appointment_queue_collection.find({})
def get_active_appointments() -> list:
'''
Returns a list of all the active appointments for the patient with the given telegram_id
'''
return appointment_queue_collection.find({"is_active": True})
def get_first_appointment() -> list:
'''
Returns a list of the first three active appointments
'''
return appointment_queue_collection.find({"is_active": True}).limit(1)
# Consultations
def create_consultation(telegram_id: int, symptoms: list, prescription: dict) -> None:
'''
Creates a new consultation for the patient with the given telegram_id
'''
new_consultation = {
"symptoms": symptoms,
"prescription": prescription,
"time": datetime.now()
}
patients_collection.update_one(
{"telegram_id": telegram_id},
{"$push": {"consultations": new_consultation}}
)
def get_consultations_from_last_30_days() -> list:
'''
Returns a list of all the consultations from the last 30 days
'''
thirty_days_ago = datetime.now() - timedelta(days=30)
query = {"consultations.time": {"$gte": thirty_days_ago, "$lte": datetime.now()}}
return patients_collection.find(query)