-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken_queue.py
More file actions
61 lines (50 loc) · 1.73 KB
/
Copy pathtoken_queue.py
File metadata and controls
61 lines (50 loc) · 1.73 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
import csv
import datetime
from pymongo import MongoClient
import os
from dotenv import load_dotenv
# Load env variables
load_dotenv()
MONGO_URI = os.getenv("MONGO_URI")
client = MongoClient(MONGO_URI)
db = client["hospital_db"]
token_collection = db["token_queue"]
def import_tokens(csv_path):
with open(csv_path, newline='', encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile)
inserted = 0
for row in reader:
try:
token_doc = {
"token_number": int(row["token_id"]),
"department": row["dept_id"],
"patient_name": row["patient_name"],
"status": row["status"],
"issued_at": datetime.datetime.utcnow()
}
token_collection.insert_one(token_doc)
inserted += 1
except Exception as e:
print(f"Skipping row due to error: {e}\nRow: {row}")
print(f"✅ Inserted {inserted} tokens successfully.")
def get_queue(department=None):
if department:
return list(token_collection.find({"department": department}))
else:
return list(token_collection.find({}))
def generate_token(department):
latest = token_collection.find_one(
{"department": department},
sort=[("issued_at", -1)]
)
next_token = latest["token_number"] + 1 if latest else 1
token_data = {
"department": department,
"token_number": next_token,
"issued_at": datetime.datetime.utcnow(),
"status": "waiting"
}
token_collection.insert_one(token_data)
return token_data
if __name__ == "__main__":
import_tokens("data/token_queue.csv") # adjust path as needed