-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathroutes.py
103 lines (80 loc) · 3.18 KB
/
routes.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
from bson import ObjectId
from flask import request, jsonify, Blueprint
from server.utils import token_required
from server.models import Patient, HealthOfficial, Record, PatientNotifications
from flask_cors import CORS
notifications = Blueprint("notifications", __name__)
# CORS(notifications)
@notifications.route("/api/notifications", methods=["POST", "GET"])
@token_required
def getNotifications(_id):
if request.method == "POST":
pid = request.json["patientId"]
rid = request.json["recordId"]
try:
notif = PatientNotifications(
healthOfficial=ObjectId(_id), record=ObjectId(rid), rtype="consent"
)
patient = Patient.objects(_id=ObjectId(pid)).first()
patient.notifs.append(notif)
patient.save()
return jsonify({"message": "Notification sent."}), 201
except:
return jsonify({"message": "Failed to add notification"}), 400
elif request.method == "GET":
patient = Patient.objects(_id=ObjectId(_id)).first()
recordlist = patient.records
resp_notifList = list()
for notif in patient.notifs:
notif_obj = dict()
notif_obj["id"] = str(notif._id)
notif_obj["approved"] = notif.approved
notif_obj["rtype"] = notif.rtype
rid = notif.record
record_obj = dict()
record_obj["id"] = str(rid)
for recs in recordlist:
if recs._id == rid:
record_obj["name"] = recs.name
record_obj["category"] = recs.category
break
notif_obj["record"] = record_obj
healthOfficial_obj = dict()
healthOfficial_obj["id"] = str(notif.healthOfficial)
healthOfficial = HealthOfficial.objects(
_id=ObjectId(notif.healthOfficial)
).first()
healthOfficial_name = healthOfficial.name
healthOfficial_obj["name"] = healthOfficial_name
notif_obj["healthOfficial"] = healthOfficial_obj
resp_notifList.append(notif_obj)
return jsonify(resp_notifList), 200
@notifications.route("/api/notifications/<nid>", methods=["POST"])
@token_required
def approveNotifs(_id, nid):
approved = request.json["isApproved"]
patient = Patient.objects(_id=ObjectId(_id)).first()
try:
if approved:
rid, hid = None, None
for notif in patient.notifs:
if notif._id == ObjectId(nid):
rid = notif.record
hid = notif.healthOfficial
break
for record in patient.records:
if record._id == rid:
record.healthOfficials.append(hid)
break
else:
pass
patient.save()
notifs = []
for notif in patient.notifs:
if notif._id != ObjectId(nid):
notifs.append(notif)
patient.notifs = notifs
patient.save()
return jsonify({"message": "Request has been processed."}), 201
except:
return jsonify({"message": "Unable to process the request."}), 400