async def get_all_doctors(request): """Fetches complete details of all Accounts in state""" client_key = general.get_request_key_header(request) # list_doctors_address = helper.make_doctor_list_address() doctors = await security_messaging.get_doctors(request.app.config.VAL_CONN, client_key) # account_resources2 = MessageToJson(account_resources) # account_resources3 = MessageToDict(account_resources) doctors_json = [] for address, doc in doctors.items(): # dec_cl = base64.b64decode(entity.data) # doc = payload_pb2.CreateDoctor() # doc.ParseFromString(entity.data) # permissions = [] # for perm in doc.permissions: # permissions.append(perm) # doctors.append({'name': doc.name, 'surname': doc.surname, 'permissions': str(permissions)}) doctors_json.append({ 'public_key': doc.public_key, 'name': doc.name, 'surname': doc.surname }) # import json # result = json.dumps(clinics) # clinics_json = MessageToJson(account_resources) return response.json(body={'data': doctors_json}, headers=general.get_response_headers())
async def grant_access(request, doctor_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_access_txn = consent_transaction.grant_access( txn_signer=client_signer, batch_signer=client_signer, doctor_pkey=doctor_pkey) batch, batch_id = transaction.make_batch_and_id([grant_access_txn], client_signer) await security_messaging.grant_access(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_lab_tests(request): """Fetches complete details of all Accounts in state""" client_key = general.get_request_key_header(request) # lab_tests_address = helper.make_lab_test_list_address() # lab_test_resources = await messaging.get_state_by_address(request.app.config.VAL_CONN, lab_tests_address) lab_tests = await security_messaging.get_lab_tests( request.app.config.VAL_CONN, client_key) # account_resources2 = MessageToJson(account_resources) # account_resources3 = MessageToDict(account_resources) lab_tests_json = [] # for entity in lab_test_resources.entries: for address, lt in lab_tests.items(): # dec_cl = base64.b64decode(entity.data) # lt = payload_pb2.AddLabTest() # lt.ParseFromString(entity.data) lab_tests_json.append({ 'height': lt.height, 'weight': lt.weight, 'gender': lt.gender, 'a_g_ratio': lt.a_g_ratio, 'albumin': lt.albumin, 'alkaline_phosphatase': lt.alkaline_phosphatase, 'appearance': lt.appearance, 'bilirubin': lt.bilirubin, 'casts': lt.casts, 'color': lt.color, 'name': lt.name, 'surname': lt.surname }) # import json # result = json.dumps(clinics) # clinics_json = MessageToJson(account_resources) return response.json(body={'data': lab_tests_json}, headers=general.get_response_headers())
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 get_all_pulse_items(request): """Fetches complete details of all Accounts in state""" LOGGER.debug("Call 'pulse' request") client_key = general.get_request_key_header(request) # pulse_list_address = helper.make_pulse_list_address() # pulse_resources = await messaging.get_state_by_address(request.app.config.VAL_CONN, pulse_list_address) pulse_list = await security_messaging.get_pulse_list( request.app.config.VAL_CONN, client_key) # account_resources2 = MessageToJson(account_resources) # account_resources3 = MessageToDict(account_resources) pulse_list_json = [] for address, pl in pulse_list.items(): # dec_cl = base64.b64decode(entity.data) # pl = payload_pb2.AddPulse() # pl.ParseFromString(entity.data) pulse_list_json.append({ 'client_pkey': pl.client_pkey, 'pulse': pl.pulse, 'timestamp': pl.timestamp, 'name': pl.name, 'surname': pl.surname }) return response.json(body={'data': pulse_list_json}, headers=general.get_response_headers())
async def get_all_clinics(request): """Fetches complete details of all Accounts in state""" client_key = general.get_request_key_header(request) clinic_list = await security_messaging.get_clinics(request.app.config.VAL_CONN, client_key) clinic_list_json = [] for address, cl in clinic_list.items(): clinic_list_json.append({ 'public_key': cl.public_key, 'name': cl.name }) return response.json(body={'data': clinic_list_json}, headers=general.get_response_headers())
async def get_all_insurances(request): """Fetches complete details of all Accounts in state""" client_key = general.get_request_key_header(request) insurances = await security_messaging.get_insurances( request.app.config.VAL_CONN, client_key) insurances_json = [] for address, ins in insurances.items(): insurances_json.append({ 'name': ins.name, 'public_key': ins.public_key }) return response.json(body={'data': insurances_json}, 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 get_all_contacts(request): """Fetches complete details of all Accounts in state""" LOGGER.debug("Call 'contract' request") client_key = general.get_request_key_header(request) contract_list = await security_messaging.get_contracts(request.app.config.VAL_CONN, client_key) contract_list_json = [] for address, con in contract_list.items(): contract_list_json.append({ 'client_pkey': con.client_pkey, 'id': con.id, 'name': con.name, 'surname': con.surname }) return response.json(body={'data': contract_list_json}, 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 get_all_payments(request): """Fetches complete details of all Accounts in state""" LOGGER.debug("Call 'payment' request") client_key = general.get_request_key_header(request) payment_list = await security_messaging.get_payments( request.app.config.VAL_CONN, client_key) payment_list_json = [] for address, pay in payment_list.items(): payment_list_json.append({ 'patient_pkey': pay.patient_pkey, 'contract_id': pay.contract_id, 'claim_id': pay.claim_id, 'id': pay.id, 'timestamp': pay.timestamp }) return response.json(body={'data': payment_list_json}, headers=general.get_response_headers())
async def get_all_claims(request): client_key = general.get_request_key_header(request) claim_list = await security_messaging.get_claims( request.app.config.VAL_CONN, client_key) claim_list_json = [] for address, cl in claim_list.items(): claim_list_json.append({ 'client_pkey': cl.client_pkey, 'id': cl.id, 'description': cl.description, 'provided_service': cl.provided_service, 'state': cl.state, 'contract_id': cl.contract_id, 'name': cl.name, 'surname': cl.surname }) return response.json(body={'data': claim_list_json}, 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 get_all_labs(request): """Fetches complete details of all Accounts in state""" client_key = general.get_request_key_header(request) # list_lab_address = helper.make_lab_list_address() lab_resources = await security_messaging.get_labs( request.app.config.VAL_CONN, client_key) # account_resources2 = MessageToJson(account_resources) # account_resources3 = MessageToDict(account_resources) labs = [] for entity in lab_resources.entries: # dec_cl = base64.b64decode(entity.data) lab = payload_pb2.CreateLab() lab.ParseFromString(entity.data) # permissions = [] # for perm in cl.permissions: # permissions.append(perm) labs.append({'name': lab.name}) # import json # result = json.dumps(clinics) # clinics_json = MessageToJson(account_resources) return response.json(body={'data': labs}, 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 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())