async def decline_inform_consent(request, investigator_pkey): """Updates auth information for the authorized account""" client_key = general.get_request_key_header(request) client_signer = general.get_signer(request, client_key) revoke_data_processing_txn = consent_transaction.decline_inform_consent( txn_signer=client_signer, batch_signer=client_signer, investigator_pkey=investigator_pkey) batch, batch_id = consent_transaction.make_batch_and_id( [revoke_data_processing_txn], client_signer) await security_messaging.decline_inform_consent( request.app.config.CONSENT_VAL_CONN, request.app.config.TIMEOUT, [batch], client_key) try: await security_messaging.check_batch_status( request.app.config.CONSENT_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_academic(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') # private_key = common.get_signer_from_file(keyfile) # signer = CryptoFactory(request.app.config.CONTEXT).new_signer(private_key) academic_signer = request.app.config.SIGNER_ACADEMIC # .get_public_key().as_hex() client_txn = consent_transaction.create_academic_client( txn_signer=academic_signer, batch_signer=academic_signer) academic_txn = data_transaction.create_academic( txn_signer=academic_signer, batch_signer=academic_signer, name=name) batch, batch_id = consent_transaction.make_batch_and_id( [client_txn, academic_txn], academic_signer) await security_messaging.add_academic(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 request_inform_consent(request, patient_pkey): """Updates auth information for the authorized account""" client_key = general.get_request_key_header(request) client_signer = general.get_signer(request, client_key) grant_read_ehr_permission_txn = consent_transaction.request_inform_document_consent( txn_signer=client_signer, batch_signer=client_signer, patient_pkey=patient_pkey) batch, batch_id = ehr_transaction.make_batch_and_id( [grant_read_ehr_permission_txn], client_signer) await security_messaging.request_inform_document_consent( 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 get_screening_data(request): """Updates auth information for the authorized account""" investigator_pkey = general.get_request_key_header(request) ehr_list = await security_messaging.get_pre_screening_data( request.app.config.EHR_VAL_CONN, request.app.config.CONSENT_VAL_CONN, investigator_pkey, request.raw_args) ehr_list_json = [] for address, data in ehr_list.items(): ehr_list_json.append({ 'id': data.id, 'client_pkey': data.client_pkey, 'height': data.height, 'weight': data.weight, 'A1C': data.A1C, 'FPG': data.FPG, 'OGTT': data.OGTT, 'RPGT': data.RPGT, 'event_time': data.event_time, 'name': data.name, 'surname': data.surname }) return response.json(body={'data': ehr_list_json}, headers=general.get_response_headers())
async def set_eligible(request): client_key = general.get_request_key_header(request) required_fields = ['id', 'eligible'] general.validate_fields(required_fields, request.json) uid = request.json.get('id') eligible = bool(request.json.get('eligible')) client_signer = request.app.config.SIGNER_INVESTIGATOR # .get_public_key().as_hex() client_txn = ehr_transaction.set_eligible(txn_signer=client_signer, batch_signer=client_signer, uid=uid, eligible=eligible) batch, batch_id = ehr_transaction.make_batch_and_id([client_txn], client_signer) await security_messaging.set_eligible(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_investigator(request): """Updates auth information for the authorized account""" required_fields = ['name'] general.validate_fields(required_fields, request.json) name = request.json.get('name') clinic_signer = request.app.config.SIGNER_INVESTIGATOR # .get_public_key().as_hex() client_txn = consent_transaction.create_investigator_client( txn_signer=clinic_signer, batch_signer=clinic_signer) clinic_txn = ehr_transaction.create_investigator( txn_signer=clinic_signer, batch_signer=clinic_signer, name=name) batch, batch_id = ehr_transaction.make_batch_and_id( [client_txn, clinic_txn], clinic_signer) await security_messaging.add_investigator(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 get_all_clients(request): """Fetches complete details of all Accounts in state""" consumer_pkey = request.app.config.SIGNER_CONSUMER.get_public_key().as_hex( ) academic_pkey = request.app.config.SIGNER_ACADEMIC.get_public_key().as_hex( ) clients = {'consumer': consumer_pkey, 'academic': academic_pkey} return response.json(body={'data': clients}, headers=general.get_response_headers())
async def get_all_clients(request): """Fetches complete details of all Accounts in state""" hospital_pkey = request.app.config.SIGNER_HOSPITAL.get_public_key().as_hex() # doctor_pkey = request.app.config.SIGNER_DOCTOR.get_public_key().as_hex() patient_pkey = request.app.config.SIGNER_PATIENT.get_public_key().as_hex() # lab_pkey = request.app.config.SIGNER_LAB.get_public_key().as_hex() # insurance_pkey = request.app.config.SIGNER_INSURANCE.get_public_key().as_hex() investigator_pkey = request.app.config.SIGNER_INVESTIGATOR.get_public_key().as_hex() clients = {'hospital': hospital_pkey, 'patient': patient_pkey, 'investigator': investigator_pkey} return response.json(body={'data': clients}, headers=general.get_response_headers())
async def get_all_data_providers(request): """Fetches complete details of all Accounts in state""" client_key = general.get_request_key_header(request) data_provider_list = await security_messaging.get_data_providers(request.app.config.VAL_CONN, client_key) data_provider_list_json = [] for address, dp in data_provider_list.items(): data_provider_list_json.append({ 'public_key': dp.public_key, 'name': dp.name }) return response.json(body={'data': data_provider_list_json}, headers=general.get_response_headers())
async def import_screening_data(request, patient_pkey, ehr_id): """Updates auth information for the authorized account""" investigator_pkey = general.get_request_key_header(request) client_signer = general.get_signer(request, investigator_pkey) # LOGGER.debug('request.json: ' + str(request.json)) # data_list = request.json # data_txns = [] # for data in data_list: has_signed_inform_consent = \ await security_messaging.has_signed_inform_consent( request.app.config.VAL_CONN, patient_pkey, investigator_pkey) if not has_signed_inform_consent: raise ApiBadRequest("No signed inform consent between patient '" + patient_pkey + "' and investigator '" + investigator_pkey + "'") ehr = await security_messaging.get_ehr_by_id(request.app.config.VAL_CONN, patient_pkey, ehr_id) data_txn = ehr_transaction.add_data(txn_signer=client_signer, batch_signer=client_signer, uid=ehr.id, height=ehr.height, weight=ehr.weight, a1c=ehr.A1C, fpg=ehr.FPG, ogtt=ehr.OGTT, rpgt=ehr.RPGT, event_time=ehr.event_time) batch, batch_id = ehr_transaction.make_batch_and_id([data_txn], client_signer) await security_messaging.import_screening_data(request.app.config.VAL_CONN, request.app.config.TIMEOUT, [batch], investigator_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 inform_consent_request_list(request): """Fetches complete details of all Accounts in state""" client_key = general.get_request_key_header(request) inform_consent_list = \ await security_messaging.get_inform_consent_request_list(request.app.config.CONSENT_VAL_CONN, client_key) inform_consent_list_json = [] for address, inf_con in inform_consent_list.items(): inform_consent_list_json.append({ 'src_pkey': inf_con.src_pkey, 'dest_pkey': inf_con.dest_pkey }) return response.json(body={'data': inform_consent_list_json}, headers=general.get_response_headers())
async def get_all_hospitals(request): """Fetches complete details of all Accounts in state""" client_key = general.get_request_key_header(request) hospital_list = await security_messaging.get_hospitals( request.app.config.EHR_VAL_CONN, request.app.config.CONSENT_VAL_CONN, client_key) hospital_list_json = [] for address, hp in hospital_list.items(): hospital_list_json.append({ 'public_key': hp.public_key, 'name': hp.name }) return response.json(body={'data': hospital_list_json}, headers=general.get_response_headers())
async def add_ehr(request): """Updates auth information for the authorized account""" hospital_pkey = general.get_request_key_header(request) required_fields = [ 'patient_pkey', 'id', 'height', 'weight', 'A1C', 'FPG', 'OGTT', 'RPGT' ] general.validate_fields(required_fields, request.json) patient_pkey = request.json.get('patient_pkey') ehr_id = request.json.get('id') height = request.json.get('height') weight = request.json.get('weight') a1c = request.json.get('A1C') fpg = request.json.get('FPG') ogtt = request.json.get('OGTT') rpgt = request.json.get('RPGT') client_signer = general.get_signer(request, hospital_pkey) ehr_txn = ehr_transaction.add_ehr(txn_signer=client_signer, batch_signer=client_signer, uid=ehr_id, client_pkey=patient_pkey, height=height, weight=weight, a1c=a1c, fpg=fpg, ogtt=ogtt, rpgt=rpgt) batch, batch_id = ehr_transaction.make_batch_and_id([ehr_txn], client_signer) await security_messaging.add_ehr(request.app.config.EHR_VAL_CONN, request.app.config.CONSENT_VAL_CONN, request.app.config.TIMEOUT, [batch], hospital_pkey, patient_pkey) try: await security_messaging.check_batch_status( request.app.config.EHR_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 consent_request_list(request): """Fetches complete details of all Accounts in state""" client_key = general.get_request_key_header(request) consent_list = \ await security_messaging.get_consent_request_list(request.app.config.VAL_CONN, client_key) consent_list_json = [] for address, con in consent_list.items(): consent_list_json.append({ 'src_pkey': con.src_pkey, 'dest_pkey': con.dest_pkey, 'action_type': ActionOnAccess.ActionType.Name(con.action_type) }) return response.json(body={'data': consent_list_json}, headers=general.get_response_headers())
async def get_all_data(request): client_key = general.get_request_key_header(request) data_list = await security_messaging.get_data(request.app.config.VAL_CONN, client_key) data_list_json = [] for address, data in data_list.items(): data_list_json.append({ 'id': data.id, 'client_pkey': data.client_pkey, 'field1': data.field1, 'field2': data.field2, 'field3': data.field3, 'event_time': data.event_time, 'name': data.name }) return response.json(body={'data': data_list_json}, headers=general.get_response_headers())
async def update_data(request): client_key = general.get_request_key_header(request) required_fields = ['id', 'height', 'weight', 'A1C', 'FPG', 'OGTT', 'RPGT'] general.validate_fields(required_fields, request.json) uid = request.json.get('id') height = request.json.get('height') weight = request.json.get('weight') A1C = request.json.get('A1C') FPG = request.json.get('FPG') OGTT = request.json.get('OGTT') RPGT = request.json.get('RPGT') client_signer = request.app.config.SIGNER_INVESTIGATOR # .get_public_key().as_hex() client_txn = ehr_transaction.update_data(txn_signer=client_signer, batch_signer=client_signer, uid=uid, height=height, weight=weight, a1c=A1C, fpg=FPG, ogtt=OGTT, rpgt=RPGT) batch, batch_id = ehr_transaction.make_batch_and_id([client_txn], client_signer) await security_messaging.update_investigator(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 get_all_data_from_investigators(request): """Fetches complete details of all Accounts in state""" client_key = general.get_request_key_header(request) data_list = await security_messaging.get_data_from_investigators( request.app.config.VAL_CONN, client_key) data_list_json = [] for address, data in data_list.items(): data_list_json.append({ 'id': data.id, 'height': data.height, 'weight': data.weight, 'A1C': data.A1C, 'FPG': data.FPG, 'OGTT': data.OGTT, 'RPGT': data.RPGT, 'event_time': data.event_time, 'eligible': data.eligible }) return response.json(body={'data': data_list_json}, headers=general.get_response_headers())
async def get_all_ehrs(request): client_key = general.get_request_key_header(request) ehr_list = await security_messaging.get_ehrs(request.app.config.VAL_CONN, client_key) ehr_list_json = [] for address, ehr in ehr_list.items(): ehr_list_json.append({ 'id': ehr.id, 'client_pkey': ehr.client_pkey, 'height': ehr.height, 'weight': ehr.weight, 'A1C': ehr.A1C, 'FPG': ehr.FPG, 'OGTT': ehr.OGTT, 'RPGT': ehr.RPGT, 'event_time': ehr.event_time, 'name': ehr.name, 'surname': ehr.surname }) return response.json(body={'data': ehr_list_json}, headers=general.get_response_headers())
async def add_data(request): """Updates auth information for the authorized account""" consumer_pkey = general.get_request_key_header(request) required_fields = ['id', 'field1', 'field2', 'field3'] general.validate_fields(required_fields, request.json) data_id = request.json.get('id') field1 = request.json.get('field1') field2 = request.json.get('field2') field3 = request.json.get('field3') client_signer = general.get_signer(request, consumer_pkey) data_txn = data_transaction.add_data(txn_signer=client_signer, batch_signer=client_signer, uid=data_id, field1=field1, field2=field2, field3=field3) batch, batch_id = data_transaction.make_batch_and_id([data_txn], client_signer) await security_messaging.add_data(request.app.config.VAL_CONN, request.app.config.TIMEOUT, [batch], consumer_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 get_ehr_by_id(request, patient_pkey, ehr_id): """Updates auth information for the authorized account""" investigator_pkey = general.get_request_key_header(request) # client_signer = general.get_signer(request, investigator_pkey) # LOGGER.debug('request.json: ' + str(request.json)) # data_list = request.json # data_txns = [] # for data in data_list: has_signed_inform_consent = \ await security_messaging.has_signed_inform_consent( request.app.config.CONSENT_VAL_CONN, patient_pkey, investigator_pkey) if not has_signed_inform_consent: raise ApiBadRequest("No signed inform consent between patient '" + patient_pkey + "' and investigator '" + investigator_pkey + "'") ehr = await security_messaging.get_ehr_by_id( request.app.config.EHR_VAL_CONN, request.app.config.CONSENT_VAL_CONN, patient_pkey, ehr_id) # data_txn = ehr_transaction.add_data( # txn_signer=client_signer, # batch_signer=client_signer, # uid=ehr.id, # height=ehr.height, # weight=ehr.weight, # a1c=ehr.A1C, # fpg=ehr.FPG, # ogtt=ehr.OGTT, # rpgt=ehr.RPGT, # event_time=ehr.event_time) # # batch, batch_id = ehr_transaction.make_batch_and_id([data_txn], client_signer) # # await security_messaging.import_screening_data( # request.app.config.VAL_CONN, # request.app.config.TIMEOUT, # [batch], investigator_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()) ehr_json = { 'id': ehr.id, 'client_pkey': ehr.client_pkey, 'height': ehr.height, 'weight': ehr.weight, 'A1C': ehr.A1C, 'FPG': ehr.FPG, 'OGTT': ehr.OGTT, 'RPGT': ehr.RPGT, 'event_time': ehr.event_time, 'name': ehr.name, 'surname': ehr.surname } return response.json(body={'data': ehr_json}, headers=general.get_response_headers())