def addPatient(_id): if request.method == "POST": email = request.json["email"] try: healthOfficial = HealthOfficial.objects(_id=ObjectId(_id)).first() patient = Patient.objects(email=email).first() healthOfficial.patients.append(patient._id) healthOfficial.save() return jsonify({"message": "Patient has been added."}), 201 except: return jsonify({"message": "Invalid request"}), 400 if request.method == "GET": healthOfficial = HealthOfficial.objects(_id=ObjectId(_id)).first() patientData = list() for oid in healthOfficial.patients: data_dict = dict() data_dict["id"] = str(oid) patient = Patient.objects(_id=oid).first() data_dict["name"] = patient.name data_dict["email"] = patient.email patientData.append(data_dict) return jsonify(patientData), 200
def userProfile(_id): userData = Patient.objects(_id=ObjectId(_id)).first() if not userData: userData = HealthOfficial.objects(_id=ObjectId(_id)).first() if request.method == "GET": return jsonify({ "name": userData["name"], "email": userData["email"] }), 200 elif request.method == "PUT": userData = Patient.objects(_id=ObjectId(_id)) if not userData: userData = HealthOfficial.objects(_id=ObjectId(_id)) data = request.json try: userData.update( name=data["name"], email=data["email"], password=generate_password_hash(data["password"]), ) return ( jsonify( {"message": "Credentials have been updated successfully."}), 200, ) except: return jsonify({"message": "Invalid update request."}), 400
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
def addPatientRecord(_id, pid): data = request.json name = data["name"] category = data["category"] doctor = data["doctor"] description = data["description"] attachment = data["file"] try: record = Record( name=name, category=category, doctor=doctor, description=description, attachments=[attachment], ) patient = Patient.objects(_id=ObjectId(pid)).first() patient.records.append(record) patient.save() # record.save() return jsonify({"message": "Record added successfully."}), 200 except: return jsonify({"message": "Unable to create the record."}), 400
def getPatientRecords(_id, pid): patient = Patient.objects(_id=ObjectId(pid)).first() recordList = list() for record in patient.records: rdict = dict() rdict["id"] = str(record._id) rdict["name"] = record.name rdict["category"] = record.category rdict["doctor"] = record.doctor rdict["description"] = record.description rdict["attachments"] = record.attachments rdict["isApproved"] = False if ObjectId(_id) in record.healthOfficials: rdict["isApproved"] = True recordList.append(rdict) return ( jsonify({ "id": str(patient._id), "name": patient.name, "email": patient.email, "records": recordList, }), 200, )
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
def deleteRequest(_id): data = request.json req_id = data["req_id"] p_id = data["p_id"] approved = data["approved"] healthOfficial = HealthOfficial.objects(_id=ObjectId(_id)).first() crequests = [] for crequest in healthOfficial.consultationRequests: if crequest._id == ObjectId(req_id): pass else: crequests.append(crequest) healthOfficial.consultationRequests = crequests pnotif = PatientNotifications(healthOfficial=ObjectId(_id), rtype="consult") # create new patient notification # type = consultation if approved == "True": healthOfficial.patients.append(ObjectId(p_id)) pnotif.approved = True patient = Patient.objects(_id=ObjectId(p_id)).first() patient.notifs.append(pnotif) patient.save() healthOfficial.save() return jsonify({"message": "Request executed successfully."})
def addRecord(_id): if request.method == "GET": records = list() patient = Patient.objects(_id=ObjectId(_id)).first() for record in patient.records: record_dict = dict() record_dict["id"] = str(record._id) record_dict["name"] = record.name record_dict["category"] = record.category record_dict["doctor"] = record.doctor record_dict["description"] = record.description record_dict["attachments"] = record.attachments records.append(record_dict) return jsonify(records), 200 elif request.method == "POST": data = request.json name = data["name"] category = data["category"] doctor = data["doctor"] description = data["description"] attachment = data["file"] try: record = Record( name=name, category=category, doctor=doctor, description=description, attachments=[attachment], ) patient = Patient.objects(_id=ObjectId(_id)).first() patient.records.append(record) # print('Reached!!!') patient.save() # record.save() return jsonify({"message": "Record added successfully."}), 200 except: return jsonify({"message": "Unable to create the record."}), 500
def getSingleRecord(_id, rid): if request.method == "GET": rdict = dict() patient = Patient.objects(_id=ObjectId(_id)).first() for record in patient.records: if record._id == ObjectId(rid): rdict["id"] = str(record._id) rdict["name"] = record.name rdict["category"] = record.category rdict["doctor"] = record.doctor rdict["description"] = record.description rdict["attachments"] = record.attachments rdict["healthOfficials"] = [] for healthOfficial in record.healthOfficials: healthOfficial_obj = HealthOfficial.objects( _id=ObjectId(healthOfficial)).first() temp = {} temp["id"] = str(healthOfficial_obj._id) temp["name"] = healthOfficial_obj.name temp["email"] = healthOfficial_obj.email rdict["healthOfficials"].append(temp) break return jsonify(rdict), 200 if request.method == "PUT": try: data = request.json patient = Patient.objects(_id=ObjectId(_id)).first() healthOfficialId = data["healthOfficialId"] for record in patient.records: if record._id == ObjectId(rid): record.healthOfficials.remove(ObjectId(healthOfficialId)) break patient.save() return jsonify({"message": "Health Official Removed!"}), 200 except: # print(e) return jsonify({"error": "Some Error occurred!"}), 500
def signup(): data = request.json if (not data or not data["name"] or not data["email"] or not data["password"] or not data["userType"]): return jsonify({"message": "Information entered in not complete"}), 400 checkUser = None if data["userType"] == "patient": checkUser = Patient.objects(email=data["email"]).first() else: checkUser = HealthOfficial.objects(email=data["email"]).first() if checkUser: return jsonify({"message": "User already exists!"}), 400 hashedpassword = generate_password_hash(data["password"]) try: _id = None if data["userType"] == "patient": patient = Patient(name=data["name"], email=data["email"], password=hashedpassword) patient.save() _id = str(patient._id) else: healthOfficial = HealthOfficial(name=data["name"], email=data["email"], password=hashedpassword) healthOfficial.save() _id = str(healthOfficial._id) token = jwt.encode( { "id": _id, "exp": datetime.datetime.utcnow() + datetime.timedelta(days=30), }, Config.SECRET_KEY, ) return jsonify({ "name": data["name"], "email": data["email"], "userType": data["userType"], "id": _id, "token": token.decode("utf-8"), }) except: return jsonify({"message": "Bad request"}), 400
def addAttachment(_id, rid): data = request.json # print(data["patientId"]) patientId = _id if data["patientId"] != 0: patientId = data["patientId"] patient = Patient.objects(_id=ObjectId(patientId)).first() attachment = data["attachment"] for record in patient.records: if record._id == ObjectId(rid): record.attachments.append(attachment) break patient.save() return jsonify({"message": "Attachement Added!"}), 200
def login(): try: data = request.json if (not data or not data["email"] or not data["password"] or not data["userType"]): return jsonify({"message": "Incomplete login information"}), 400 checkUser, token = None, None if data["userType"] == "patient": checkUser = Patient.objects(email=data["email"]).first() else: checkUser = HealthOfficial.objects(email=data["email"]).first() if checkUser: if check_password_hash(checkUser["password"], data["password"]): token = jwt.encode( { "id": str(checkUser._id), "exp": datetime.datetime.utcnow() + datetime.timedelta(days=30), }, Config.SECRET_KEY, ) else: return jsonify({"message": "Invalid email or password"}), 404 else: return ( jsonify({ "message": "User doesn't exist! Please check your email address and password." }), 404, ) return jsonify({ "name": checkUser["name"], "email": checkUser["email"], "id": str(checkUser._id), "userType": data["userType"], "token": token.decode("utf-8"), }) except: return jsonify({"message": "Some Error Occured!"}), 500
def getRecords(_id, pid, rid): patient = Patient.objects(_id=ObjectId(pid)).first() rdict = dict() for record in patient.records: if record._id == ObjectId(rid): rdict["id"] = str(record._id) rdict["name"] = record.name rdict["category"] = record.category rdict["doctor"] = record.doctor rdict["description"] = record.description rdict["attachments"] = record.attachments rdict["isApproved"] = False if ObjectId(_id) in record.healthOfficials: rdict["isApproved"] = True break return jsonify(rdict), 200