def post(self): import api.util as util import data.crud as crud import service.assoc as assoc from models import Patient try: request_body = _get_request_body() except: return {"HTTP 400": decoding_error}, 400 if not request_body["patientId"]: return {"HTTP 400": "Patient Id is empty."}, 400 # Emulate old API functionality with new systems, use of /api/associations is # preferred over this method now patient = crud.read(Patient, patientId=request_body["patientId"]) if patient: user = util.current_user() facility = user.healthFacility if not assoc.has_association(patient, facility, user): assoc.associate(patient, facility, user) else: abort(409, message="Duplicate entry") return { "message": "patient has been added to facility successfully" }, 201 else: abort(404, message="This patient does not exist.")
def post(): json = request.get_json(force=True) error_message = patients.validate(json) if error_message is not None: abort(400, message=error_message) patient = marshal.unmarshal(Patient, json) if crud.read(Patient, patientId=patient.patientId): abort(409, message=f"A patient already exists with id: {patient.patientId}") # Resolve invariants and set the creation timestamp for the patient ensuring # that both the created and lastEdited fields have the exact same value. invariant.resolve_reading_invariants(patient) creation_time = get_current_time() patient.created = creation_time patient.lastEdited = creation_time crud.create(patient, refresh=True) # Associate the patient with the user who created them user = util.current_user() assoc.associate_by_user_role(patient, user) # If the patient has any readings, and those readings have referrals, we # associate the patient with the facilities they were referred to for reading in patient.readings: referral = reading.referral if referral and not assoc.has_association(patient, referral.healthFacility): assoc.associate(patient, facility=referral.healthFacility) # The associate function performs a database commit, since this will # wipe out the patient we want to return we must refresh it. data.db_session.refresh(patient) return marshal.marshal(patient), 201
def post(): json = request.get_json(force=True) error_message = referrals.validate(json) if error_message is not None: abort(400, message=error_message) referral = marshal.unmarshal(Referral, json) crud.create(referral) # Creating a referral also associates the corresponding patient to the health # facility they were referred to. patient = referral.patient facility = referral.healthFacility if not assoc.has_association(patient, facility): assoc.associate(patient, facility=facility) return marshal.marshal(referral), 201
def post(): json: dict = request.get_json(force=True) error_message = associations.validate(json) if error_message is not None: abort(400, message=error_message) patient_id = json.get("patientId") facility_name = json.get("healthFacilityName") user_id = json.get("userId") patient = crud.read(Patient, patientId=patient_id) if not patient: abort(400, message=f"No patient exists with id: {patient_id}") if facility_name: facility = crud.read(HealthFacility, healthFacilityName=facility_name) if not facility: abort(400, message=f"No health facility with name: {facility_name}") else: facility = None if user_id: user = crud.read(User, id=user_id) if not user: abort(400, message=f"No user with id: {user_id}") # if user exists but no health facility then assign the patient to the user's health facility facility = user.healthFacility else: user = None if not facility_name and not user_id: # If neither facility_name or user_id are present in the request, create a # associate patient with the current user's health facility user = util.current_user() assoc.associate(patient, user.healthFacility, user) else: # Otherwise, simply associate the provided models together if not assoc.has_association(patient, facility, user): assoc.associate(patient, facility, user) return {}, 201
def post(): # Get all patients for this user user = util.current_user() timestamp: int = request.args.get("since", None, type=int) if not timestamp: abort(400, message="'since' query parameter is required") patients_to_be_added: [Patient] = [] # ~~~~~~~~~~~~~~~~~~~~~~ new Logic ~~~~~~~~~~~~~~~~~~~~~~~~~~ json = request.get_json(force=True) for p in json: patient_on_server = crud.read(Patient, patientId=p.get("patientId")) if patient_on_server is None: error_message = patients.validate(p) if error_message is not None: abort(400, message=error_message) patient = marshal.unmarshal(Patient, p) # # Resolve invariants and set the creation timestamp for the patient ensuring # # that both the created and lastEdited fields have the exact same value. invariant.resolve_reading_invariants_mobile(patient) creation_time = get_current_time() patient.created = creation_time patient.lastEdited = creation_time patients_to_be_added.append(patient) else: if (int(patient_on_server.lastEdited) < int( p.get("lastEdited")) < timestamp): if p.get("base"): if p.get("base") != p.get("lastEdited"): abort( 409, message= "Unable to merge changes, conflict detected", ) del p["base"] p["lastEdited"] = get_current_time() crud.update(Patient, p, patientId=p["patientId"]) # TODO: revisit association logic if not assoc.has_association(patient_on_server, user=user): assoc.associate(patient_on_server, user.healthFacility, user) # update association if patients_to_be_added: crud.create_all_patients(patients_to_be_added) for new_patient in patients_to_be_added: # TODO: revisit association logic if not assoc.has_association(new_patient, user=user): assoc.associate(new_patient, user.healthFacility, user) # read all the patients from the DB # TODO: optimize to get only patients all_patients = view.patient_view_for_user(user) all_patients_edited_or_new = [ p for p in all_patients if p["lastEdited"] > timestamp ] # ~~~~~~~~~~~~~~~~~ old logic ~~~~~~~~~~~~~~~~~~~~ # New patients are patients who are created after the timestamp # new_patients = [ # p["patientId"] for p in all_patients if p["created"] > timestamp # ] # Edited patients are patients who were created before the timestamp but # edited after it # edited_patients = [ # p["patientId"] # for p in all_patients # if p["created"] < p["lastEdited"] # and p["created"] <= timestamp < p["lastEdited"] # ] # New readings created after the timestamp for patients who where created before # the timestamp # readings = [] # New followups which were created after the timestamp for readings which were # created before the timestamp # followups = [] # # for p in all_patients: # for r in p["readings"]: # r_time = int(r["dateTimeTaken"]) # if p["created"] <= timestamp < r_time: # readings.append(r["readingId"]) # # if r["followup"] and r_time < timestamp < int( # r["followup"]["dateAssessed"] # ): # followups.append(r["followup"]["id"]) return { "total": len(all_patients_edited_or_new), "patients": all_patients_edited_or_new, }