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 getHealthOfficials(_id): hid = request.args.get("hid", default=None, type=str) name = request.args.get("name", default=None, type=str) regex = re.compile(f".*{name}.*", re.IGNORECASE) try: if hid is None and name is None: healthOfficials = HealthOfficial.objects.scalar("name", "email").to_json() return healthOfficials, 200 elif hid and name is None: healthOfficial = HealthOfficial.objects(_id=ObjectId(hid)).scalar( "name", "email") healthOfficial = healthOfficial.to_json() return healthOfficial, 200 elif name and hid is None: healthOfficial = HealthOfficial.objects(name=regex).scalar( "name", "email") healthOfficial = healthOfficial.to_json() return healthOfficial, 200 else: return jsonify({"message": "Bad Request"}), 400 except: return jsonify({"message": "Unexpected error occurred."}), 500
def addConsultationRequest(_id): if request.method == "POST": try: data = request.json hid = data["hid"] age = data["age"] sex = data["sex"] symptoms = data["symptoms"] description = data["description"] consultationData = ConsultationData(age=age, sex=sex, symptoms=symptoms, description=description) consultationRequest = ConsultationRequest( patient=ObjectId(_id), patientName=Patient.objects.get(_id=ObjectId(_id)).name, healthOfficial=ObjectId(hid), consultationData=consultationData, ) healthOfficial = HealthOfficial.objects(_id=ObjectId(hid)).first() healthOfficial.consultationRequests.append(consultationRequest) healthOfficial.save() return jsonify({"message": "Request sent successfully."}), 200 except: return jsonify({"message": "Unexpected error occurred."}), 500 else: return jsonify({"message": "Bad 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 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 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 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 getRequests(_id): req_id = request.args.get("req_id", default=None, type=str) healthOfficial = HealthOfficial.objects(_id=ObjectId(_id)).first() consultationRequests = healthOfficial.consultationRequests # try: if req_id is None: # response with all requests resp = [] for crequest in consultationRequests: crequest = json.loads(crequest.to_json()) resp.append(crequest) return jsonify(resp), 200 else: # response with given req_id for crequest in consultationRequests: if crequest._id == ObjectId(req_id): resp = json.loads(crequest.to_json()) return jsonify(resp), 200 # except: return jsonify({"message": "Unexpected error occurred."}), 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