async def add_new_contract(request): """Updates auth information for the authorized account""" client_key = general.get_request_key_header(request) required_fields = ['id', 'client_pkey'] general.validate_fields(required_fields, request.json) uid = request.json.get('id') contractor_pkey = request.json.get('client_pkey') client_signer = general.get_signer(request, client_key) contract_txn = transaction.add_contract( txn_signer=client_signer, batch_signer=client_signer, uid=uid, client_pkey=contractor_pkey ) batch, batch_id = transaction.make_batch_and_id([contract_txn], client_signer) await security_messaging.add_contract( request.app.config.VAL_CONN, request.app.config.TIMEOUT, [batch], client_key) try: await security_messaging.check_batch_status( request.app.config.VAL_CONN, [batch_id]) except (ApiBadRequest, ApiInternalError) as err: # await auth_query.remove_auth_entry( # request.app.config.DB_CONN, request.json.get('email')) raise err return response.json(body={'status': general.DONE}, headers=general.get_response_headers())
async def next_visit(request): required_fields = ['claim_id', 'doctor_pkey'] general.validate_fields(required_fields, request.json) claim_id = request.json.get('claim_id') doctor_pkey = request.json.get('doctor_pkey') current_times_str = str(time.time()) # private_key = common.get_signer_from_file(keyfile) # signer = CryptoFactory(request.app.config.CONTEXT).new_signer(private_key) clinic_signer = request.app.config.SIGNER # .get_public_key().as_hex() batch, batch_id = transaction.next_visit( txn_signer=clinic_signer, batch_signer=clinic_signer, claim_id=claim_id, description="Doctor: {}, completed next visit for claim: {}".format( doctor_pkey, claim_id), event_time=current_times_str) await messaging.send(request.app.config.VAL_CONN, request.app.config.TIMEOUT, [batch]) try: await messaging.check_batch_status(request.app.config.VAL_CONN, [batch_id]) except (ApiBadRequest, ApiInternalError) as err: # await auth_query.remove_auth_entry( # request.app.config.DB_CONN, request.json.get('email')) raise err return response.json(body={'status': general.DONE}, headers=general.get_response_headers())
async def update_claim(request): """Updates auth information for the authorized account""" # keyfile = common.get_keyfile(request.json.get['signer']) clinic_pkey = general.get_request_key_header(request) required_fields = [ 'claim_id', 'provided_service', 'client_pkey', 'provided_service' ] general.validate_fields(required_fields, request.json) claim_id = request.json.get('claim_id') provided_service = request.json.get('provided_service') patient_pkey = request.json.get('client_pkey') contract_id = request.json.get('contract_id') client_signer = general.get_signer(request, clinic_pkey) close_claim_txn = transaction.update_claim( txn_signer=client_signer, batch_signer=client_signer, uid=claim_id, patient_pkey=patient_pkey, provided_service=provided_service) # create_payment_txn = payment_transaction.create_payment( # txn_signer=client_signer, # batch_signer=client_signer, # payment_id=str(helper.get_current_timestamp()), # patient_pkey=patient_pkey, # contract_id=contract_id, # claim_id=claim_id # ) batch, batch_id = transaction.make_batch_and_id([close_claim_txn], client_signer) await security_messaging.update_claim(request.app.config.VAL_CONN, request.app.config.TIMEOUT, [batch], clinic_pkey, patient_pkey) try: await security_messaging.check_batch_status( request.app.config.VAL_CONN, [batch_id]) except (ApiBadRequest, ApiInternalError) as err: # await auth_query.remove_auth_entry( # request.app.config.DB_CONN, request.json.get('email')) raise err return response.json(body={'status': general.DONE}, headers=general.get_response_headers())
async def register_new_claim(request): """Updates auth information for the authorized account""" # keyfile = common.get_keyfile(request.json.get['signer']) clinic_pkey = general.get_request_key_header(request) required_fields = ['patient_pkey', 'claim_id', 'description'] general.validate_fields(required_fields, request.json) patient_pkey = request.json.get('patient_pkey') claim_id = request.json.get('claim_id') description = request.json.get('description') contract_id = request.json.get('contract_id') if contract_id is not None and contract_id != '': is_valid = await security_messaging.valid_contracts( request.app.config.VAL_CONN, patient_pkey, contract_id) if not is_valid: return response.text(body="Contract having '" + contract_id + "' id is not valid", status=ApiBadRequest.status_code, headers=general.get_response_headers()) client_signer = general.get_signer(request, clinic_pkey) claim_txn = transaction.add_claim(txn_signer=client_signer, batch_signer=client_signer, uid=claim_id, description=description, client_pkey=patient_pkey, contract_id=contract_id) batch, batch_id = transaction.make_batch_and_id([claim_txn], client_signer) await security_messaging.add_claim(request.app.config.VAL_CONN, request.app.config.TIMEOUT, [batch], clinic_pkey, patient_pkey) try: await security_messaging.check_batch_status( request.app.config.VAL_CONN, [batch_id]) except (ApiBadRequest, ApiInternalError) as err: # await auth_query.remove_auth_entry( # request.app.config.DB_CONN, request.json.get('email')) raise err return response.json(body={'status': general.DONE}, headers=general.get_response_headers())
async def register_new_clinic(request): """Updates auth information for the authorized account""" # keyfile = common.get_keyfile(request.json.get['signer']) # client_key = general.get_request_key_header(request) required_fields = ['name'] general.validate_fields(required_fields, request.json) name = request.json.get('name') # private_key = common.get_signer_from_file(keyfile) # signer = CryptoFactory(request.app.config.CONTEXT).new_signer(private_key) clinic_signer = request.app.config.SIGNER_CLINIC # .get_public_key().as_hex() client_txn = consent_transaction.create_clinic_client( txn_signer=clinic_signer, batch_signer=clinic_signer ) clinic_txn = transaction.create_clinic( txn_signer=clinic_signer, batch_signer=clinic_signer, name=name ) batch, batch_id = transaction.make_batch_and_id([client_txn, clinic_txn], clinic_signer) # batch, batch_id = transaction.create_clinic( # txn_signer=clinic_signer, # batch_signer=clinic_signer, # name=name) await security_messaging.add_clinic( request.app.config.VAL_CONN, request.app.config.TIMEOUT, [batch]) try: await security_messaging.check_batch_status( request.app.config.VAL_CONN, [batch_id]) except (ApiBadRequest, ApiInternalError) as err: # await auth_query.remove_auth_entry( # request.app.config.DB_CONN, request.json.get('email')) raise err return response.json(body={'status': general.DONE}, headers=general.get_response_headers())
async def add_new_pulse(request): """Updates auth information for the authorized account""" # keyfile = common.get_keyfile(request.json.get['signer']) client_key = general.get_request_key_header(request) required_fields = ['pulse', 'timestamp'] general.validate_fields(required_fields, request.json) pulse = request.json.get('pulse') timestamp = request.json.get('timestamp') # private_key = common.get_signer_from_file(keyfile) # signer = CryptoFactory(request.app.config.CONTEXT).new_signer(private_key) # patient_signer = request.app.config.SIGNER # .get_public_key().as_hex() client_signer = general.get_signer(request, client_key) current_times_str = str(helper.get_current_timestamp()) pulse_txn = transaction.add_pulse(txn_signer=client_signer, batch_signer=client_signer, pulse=pulse, uid=current_times_str, timestamp=timestamp, client_pkey=client_key) batch, batch_id = transaction.make_batch_and_id([pulse_txn], client_signer) await security_messaging.add_pulse(request.app.config.VAL_CONN, request.app.config.TIMEOUT, [batch], client_key) try: await security_messaging.check_batch_status( request.app.config.VAL_CONN, [batch_id]) except (ApiBadRequest, ApiInternalError) as err: # await auth_query.remove_auth_entry( # request.app.config.DB_CONN, request.json.get('email')) raise err return response.json(body={'status': general.DONE}, headers=general.get_response_headers())
async def add_new_payment(request): """Updates auth information for the authorized account""" # keyfile = common.get_keyfile(request.json.get['signer']) client_key = general.get_request_key_header(request) required_fields = ['patient_pkey', 'contract_id', 'claim_id'] general.validate_fields(required_fields, request.json) # uid = request.json.get('id') patient_pkey = request.json.get('patient_pkey') contract_id = request.json.get('contract_id') claim_id = request.json.get('claim_id') client_signer = general.get_signer(request, client_key) payment_id = str(helper.get_current_timestamp()) batch, batch_id = transaction.create_payment(txn_signer=client_signer, batch_signer=client_signer, payment_id=payment_id, patient_pkey=patient_pkey, contract_id=contract_id, claim_id=claim_id) await security_messaging.create_payment(request.app.config.VAL_CONN, request.app.config.TIMEOUT, [batch], client_key) try: await security_messaging.check_batch_status( request.app.config.VAL_CONN, [batch_id]) except (ApiBadRequest, ApiInternalError) as err: # await auth_query.remove_auth_entry( # request.app.config.DB_CONN, request.json.get('email')) raise err return response.json(body={'status': general.DONE}, headers=general.get_response_headers())
async def register_new_insurance(request): """Updates auth information for the authorized account""" # keyfile = common.get_keyfile(request.json.get['signer']) required_fields = ['name'] general.validate_fields(required_fields, request.json) name = request.json.get('name') insurance_signer = request.app.config.SIGNER_INSURANCE client_txn = consent_transaction.create_insurance_client( txn_signer=insurance_signer, batch_signer=insurance_signer) LOGGER.debug('client_txn: ' + str(client_txn)) insurance_txn = insurance_transaction.create_insurance( txn_signer=insurance_signer, batch_signer=insurance_signer, name=name) LOGGER.debug('insurance_txn: ' + str(insurance_txn)) batch, batch_id = insurance_transaction.make_batch_and_id( [client_txn, insurance_txn], insurance_signer) # batch, batch_id = consent_transaction.make_batch_and_id([insurance_txn], insurance_signer) await security_messaging.add_insurance(request.app.config.VAL_CONN, request.app.config.TIMEOUT, [batch]) try: await security_messaging.check_batch_status( request.app.config.VAL_CONN, [batch_id]) except (ApiBadRequest, ApiInternalError) as err: # await auth_query.remove_auth_entry( # request.app.config.DB_CONN, request.json.get('email')) raise err return response.json(body={'status': general.DONE}, headers=general.get_response_headers())
async def register_new_patient(request): """Updates auth information for the authorized account""" # keyfile = common.get_keyfile(request.json.get['signer']) required_fields = ['first_name', 'last_name', 'idcard_type', 'idcard'] general.validate_fields(required_fields, request.json) name = request.json.get('first_name') surname = request.json.get('last_name') idcard = request.json.get('idcard') idcard_type = int(request.json.get('idcard_type')) party_info = { "name": name, "lastname": surname, "idcard": idcard, "telephone": request.json.get('telephone') if 'telephone' in request.json else " ", "birthdate": request.json.get('birthdate') if 'birthdate' in request.json else " ", "idcard_type": idcard_type } headers = {'Content-Type': 'application/json'} dataParty = { "username": party_info['idcard'], "_client_type_": _client_type_ } response_load = requests.post('http://validator:8863/getPrivateKey', data=GeneralTool().parse2JSON(dataParty), headers=headers) batch_lst = [] if response_load.status_code != 200: raise Exception( "There was a problem communicating with the validator.") elif response_load.status_code == 200 and 'private_key' in GeneralTool( ).parseFromJSON(response_load.content.decode()): keys = GeneralTool().parseFromJSON(response_load.content.decode()) privatekey = keys['private_key'] public_key = keys['public_key'] elif response_load.status_code == 200 and 'private_key' not in GeneralTool( ).parseFromJSON(response_load.content.decode()): party_txn, privatekey, public_key = general.addParty( party_info, _client_type_) batch_lst.append(party_txn) else: raise Exception( "There was a problem communicating with the validator.") patient_signer = GeneralTool().addSigner( GeneralTool().ParsePrivateKey(private_key_str=privatekey)) patientKey = '{}|{}'.format(public_key, '0' + str(_client_type_)) patient_info = { 'party_key': public_key, 'record_id': patientKey, 'biological_sex': request.json.get('biological_sex'), "blood_type": request.json.get('blood_type') if 'blood_type' in request.json else " ", "critical_info": request.json.get('critical_info') if 'critical_info' in request.json else " ", "current_insurance": request.json.get('current_insurance') if 'current_insurance' in request.json else " ", "disability_kind": request.json.get('disability_kind') if 'disability_kind' in request.json else " ", "disabled_person": request.json.get('disability_kind') if 'disability_kind' in request.json else '0', "familiar_antecedents": request.json.get('familiar_antecedents') if 'familiar_antecedents' in request.json else " ", "general_info": request.json.get('general_info') if 'general_info' in request.json else " ", "history_information": request.json.get('general_info') if 'general_info' in request.json else {}, "alcohol": request.json.get('alcohol') if 'alcohol' in request.json else '0', "anticonceptive": request.json.get('anticonceptive') if 'anticonceptive' in request.json else '0', "car_child_safety": request.json.get('car_child_safety') if 'car_child_safety' in request.json else '0', "car_revision": request.json.get('car_revision') if 'car_revision' in request.json else '0', "car_seat_belt": request.json.get('car_seat_belt') if 'car_seat_belt' in request.json else '0', "coffee": request.json.get('coffee') if 'coffee' in request.json else '0', "diet": request.json.get('diet') if 'diet' in request.json else '0', "drug_iv": request.json.get('drug_iv') if 'drug_iv' in request.json else '0', "drug_usage": request.json.get('drug_usage') if 'drug_usage' in request.json else '0', "eats_alone": request.json.get('eats_alone') if 'eats_alone' in request.json else '0', "ex_alcoholic": request.json.get('ex_alcoholic') if 'ex_alcoholic' in request.json else '0', "ex_drug_addict": request.json.get('ex_drug_addict') if 'ex_drug_addict' in request.json else '0', "ex_smoker": request.json.get('ex_smoker') if 'ex_smoker' in request.json else '0', "exercise": request.json.get('exercise') if 'exercise' in request.json else '0', "helmet": request.json.get('helmet') if 'helmet' in request.json else '0', "home_safety": request.json.get('home_safety') if 'home_safety' in request.json else '0', "motorcycle_rider": request.json.get('motorcycle_rider') if 'motorcycle_rider' in request.json else '0', "prostitute": request.json.get('prostitute') if 'prostitute' in request.json else '0', "salt": request.json.get('salt') if 'salt' in request.json else '0', "second_hand_smoker": request.json.get('second_hand_smoker') if 'second_hand_smoker' in request.json else '0', "smoking": request.json.get('smoking') if 'smoking' in request.json else '0', "soft_drinks": request.json.get('soft_drinks') if 'soft_drinks' in request.json else '0', "traffic_laws": request.json.get('traffic_laws') if 'traffic_laws' in request.json else '0', "photo": request.json.get('photo') if 'photo' in request.json else '/static/pictures/man.svg' if request.json.get('biological_sex') == 'm' else '/static/pictures/woman.svg' } patient_txn = transaction.create_patient(txn_signer=patient_signer, batch_signer=patient_signer, patient_info=patient_info) batch_lst.append(patient_txn) evaluation_record_id = '{}|{}'.format(patient_info['party_key'], party_info['idcard']) record_patient_txn = transaction.add_record_patient( txn_signer=patient_signer, batch_signer=patient_signer, record_id=evaluation_record_id, record_owner=public_key) batch_lst.append(record_patient_txn) batch, batch_id = transaction.make_batch_and_id(batch_lst, patient_signer) await security_messaging.add_patient(request.app.config.VAL_CONN, request.app.config.TIMEOUT, [batch]) # try: # await security_messaging.check_batch_status( # request.app.config.VAL_CONN, [batch_id]) # except (ApiBadRequest, ApiInternalError) as err: # # await auth_query.remove_auth_entry( # # request.app.config.DB_CONN, request.json.get('email')) # raise err return response.json(body={'status': general.DONE}, headers=general.get_response_headers())
async def register_new_doctor(request): """Updates auth information for the authorized account""" # keyfile = common.get_keyfile(request.json.get['signer']) """Updates auth information for the authorized account""" # keyfile = common.get_keyfile(request.json.get['signer']) required_fields = ['first_name', 'last_name', 'idcard_type', 'idcard'] general.validate_fields(required_fields, request.json) name = request.json.get('first_name') surname = request.json.get('last_name') idcard = request.json.get('idcard') idcard_type = int(request.json.get('idcard_type')) # private_key = common.get_signer_from_file(keyfile) # signer = CryptoFactory(request.app.config.CONTEXT).new_signer(private_key) # patient_signer = request.app.config.SIGNER_PATIENT # .get_public_key().as_hex() party_info = { "name": name, "lastname": surname, "idcard": idcard, "telephone": request.json.get('telephone') if 'telephone' in request.json else " ", "birthdate": request.json.get('birthdate') if 'birthdate' in request.json else " ", "idcard_type": idcard_type } headers = {'Content-Type': 'application/json'} dataParty = { "username": party_info['idcard'], "_client_type_": _client_type_ } response_load = requests.post('http://validator:8863/getPrivateKey', data=GeneralTool().parse2JSON(dataParty), headers=headers) batch_lst = [] if response_load.status_code != 200: raise Exception( "There was a problem communicating with the validator.") elif response_load.status_code == 200 and 'private_key' in GeneralTool( ).parseFromJSON(response_load.content.decode()): keys = GeneralTool().parseFromJSON(response_load.content.decode()) privatekey = keys['private_key'] public_key = keys['public_key'] elif response_load.status_code == 200 and 'private_key' not in GeneralTool( ).parseFromJSON(response_load.content.decode()): party_txn, privatekey, public_key = general.addParty( party_info, _client_type_) batch_lst.append(party_txn) else: raise Exception( "There was a problem communicating with the validator.") doctor_signer = GeneralTool().addSigner( GeneralTool().ParsePrivateKey(private_key_str=privatekey)) doctorKey = '{}|{}'.format(public_key, '0' + str(_client_type_)) doctor_info = { 'party_key': public_key, 'doctor_key': doctorKey, "main_speciality": request.json.get('main_speciality') if 'main_speciality' in request.json else " ", "photo": request.json.get('photo') if 'photo' in request.json else '/static/pictures/man.svg' if request.json.get('biological_sex') == 'm' else '/static/pictures/woman.svg', "bio": request.json.get('bio') if 'bio' in request.json else " ", 'biological_sex': request.json.get('biological_sex') } doctor_txn = transaction.create_doctor(txn_signer=doctor_signer, batch_signer=doctor_signer, doctor_info=doctor_info) batch_lst.append(doctor_txn) batch, batch_id = transaction.make_batch_and_id(batch_lst, doctor_signer) await security_messaging.add_doctor(request.app.config.VAL_CONN, request.app.config.TIMEOUT, [batch]) try: await security_messaging.check_batch_status( request.app.config.VAL_CONN, [batch_id]) except (ApiBadRequest, ApiInternalError) as err: raise err return response.json(body={'status': general.DONE}, headers=general.get_response_headers())
async def update_doctor(request): """Updates auth information for the authorized account""" # keyfile = common.get_keyfile(request.json.get['signer']) required_fields = ['idcard'] general.validate_fields(required_fields, request.json) idcard = request.json.get('idcard') headers = {'Content-Type': 'application/json'} dataParty = {"username": idcard} response_load = requests.post('http://validator:8863/getPrivateKey', data=GeneralTool().parse2JSON(dataParty), headers=headers) if response_load.status_code != 200: raise Exception( "There was a problem communicating with the validator.") elif response_load.status_code == 200: keys = GeneralTool().parseFromJSON(response_load.content.decode()) public_key = keys['public_key'] party = await security_messaging.get_party(request.app.config.VAL_CONN, public_key) doctorKey = '{}|{}'.format(public_key, '0' + str(_client_type_)) doctor = await security_messaging.get_doctor( request.app.config.VAL_CONN, doctorKey) party_info = { 'name': request.json.get('telephone') if 'telephone' in request.json else party.name, 'lastname': party.lastname, 'telephone': party.telephone, 'birthdate': party.birthdate, 'idcard': party.idcard, 'sex': party.sex, 'photo': doctor.photo, 'speciality': doctor.main_speciality } batch_lst = [] party_txn, privatekey, public_key = general.update_party(party_info) batch_lst.append(party_txn) doctor_signer = GeneralTool().addSigner( GeneralTool().ParsePrivateKey(private_key_str=privatekey)) doctorKey = '{}|{}'.format(public_key, '0' + str(_client_type_)) doctor_info = { 'party_key': public_key, 'doctor_key': doctorKey, "main_speciality": request.json.get('main_speciality') if 'main_speciality' in request.json else " ", "photo": request.json.get('photo') if 'photo' in request.json else '/static/pictures/man.svg' if request.json.get('biological_sex') == 'm' else '/static/pictures/woman.svg', "bio": request.json.get('bio') if 'bio' in request.json else " ", 'biological_sex': request.json.get('biological_sex') } doctor_txn = transaction.create_doctor(txn_signer=doctor_signer, batch_signer=doctor_signer, doctor_info=doctor_info) batch_lst.append(doctor_txn) batch, batch_id = transaction.make_batch_and_id( batch_lst, doctor_signer) await security_messaging.add_doctor(request.app.config.VAL_CONN, request.app.config.TIMEOUT, [batch]) try: await security_messaging.check_batch_status( request.app.config.VAL_CONN, [batch_id]) except (ApiBadRequest, ApiInternalError) as err: raise err return response.json(body={'status': general.DONE}, headers=general.get_response_headers()) return response.json(body={'status': general.ERROR}, headers=general.get_response_headers())
async def add_new_lab_test(request): """Updates auth information for the authorized account""" # keyfile = common.get_keyfile(request.json.get['signer']) client_key = general.get_request_key_header(request) required_fields = [ 'height', 'weight', 'gender', 'a_g_ratio', 'albumin', 'alkaline_phosphatase', 'appearance', 'bilirubin', 'casts', 'color' ] general.validate_fields(required_fields, request.json) height = request.json.get('height') weight = request.json.get('weight') gender = request.json.get('gender') a_g_ratio = request.json.get('a_g_ratio') albumin = request.json.get('albumin') alkaline_phosphatase = request.json.get('alkaline_phosphatase') appearance = request.json.get('appearance') bilirubin = request.json.get('bilirubin') casts = request.json.get('casts') color = request.json.get('color') # private_key = common.get_signer_from_file(keyfile) # signer = CryptoFactory(request.app.config.CONTEXT).new_signer(private_key) # client_signer = request.app.config.SIGNER # .get_public_key().as_hex() # if request.app.config.SIGNER_CLINIC.get_public_key().as_hex() == client_key: # client_signer = request.app.config.SIGNER_CLINIC # elif request.app.config.SIGNER_PATIENT.get_public_key().as_hex() == client_key: # client_signer = request.app.config.SIGNER_PATIENT # elif request.app.config.SIGNER_DOCTOR.get_public_key().as_hex() == client_key: # client_signer = request.app.config.SIGNER_DOCTOR # elif request.app.config.SIGNER_LAB.get_public_key().as_hex() == client_key: # client_signer = request.app.config.SIGNER_LAB # else: # client_signer = request.app.config.SIGNER_PATIENT client_signer = general.get_signer(request, client_key) current_times_str = str(helper.get_current_timestamp()) lab_test_txn = transaction.add_lab_test( txn_signer=client_signer, batch_signer=client_signer, height=height, weight=weight, gender=gender, a_g_ratio=a_g_ratio, albumin=albumin, alkaline_phosphatase=alkaline_phosphatase, appearance=appearance, bilirubin=bilirubin, casts=casts, color=color, uid=current_times_str, client_pkey=client_key) batch, batch_id = transaction.make_batch_and_id([lab_test_txn], client_signer) await security_messaging.add_lab_test(request.app.config.VAL_CONN, request.app.config.TIMEOUT, [batch], client_key) try: await security_messaging.check_batch_status( request.app.config.VAL_CONN, [batch_id]) except (ApiBadRequest, ApiInternalError) as err: # await auth_query.remove_auth_entry( # request.app.config.DB_CONN, request.json.get('email')) raise err return response.json(body={'status': general.DONE}, headers=general.get_response_headers())