async def get_patients(conn, client_key):
    client = await get_client(conn, client_key)
    patient_list = {}
    if Permission(type=Permission.READ_PATIENT) in client.permissions:
        list_patient_address = helper.make_patient_list_address()
        LOGGER.debug('has READ_PATIENT permission: ' + str(client_key))
        # Get Consent
        consent = await get_consent(conn, client_key)
        consent_list = {}
        for address, pt in consent.items():
            LOGGER.debug('consent: ' + str(pt))
            patient = await get_patient(conn, pt.patient_pkey)
            consent_list[pt.patient_pkey] = patient
        #
        patient_list_resources = await messaging.get_state_by_address(
            conn, list_patient_address)
        for entity in patient_list_resources.entries:
            pat = CreatePatient()
            pat.ParseFromString(entity.data)

            patient_list[entity.address] = pat
            LOGGER.debug('patient: ' + str(pat))
        # Apply Consent
        for patient_address, pt in patient_list.items():
            LOGGER.debug('patient: ' + str(pt))
            if Permission(
                    type=Permission.READ_OWN_PATIENT
            ) in client.permissions and pt.public_key == client_key:
                pass
            elif pt.public_key not in consent_list:
                pat2 = CreatePatient()
                patient_list[patient_address] = pat2
        return patient_list
    raise ApiForbidden("Insufficient permission")
async def get_inform_consent_request_list(conn, client_key):
    client = await get_client(conn, client_key)
    if Permission(type=Permission.READ_INFORM_CONSENT_REQUEST) in client.permissions and \
            Permission(type=Permission.READ_SIGNED_INFORM_CONSENT) in client.permissions:
        LOGGER.debug('has READ_INFORM_CONSENT_REQUEST and READ_SIGNED_INFORM_CONSENT permission: ' + str(client_key))
        inform_consent_request_list = await get_inform_consent_request(conn, client_key)
        return inform_consent_request_list
    raise ApiForbidden("Insufficient permission")
async def get_ehrs(ehr_conn, consent_conn, client_key):
    client = await get_client(consent_conn, client_key)
    ehr_list = {}
    if Permission(type=Permission.READ_PATIENT_DATA) in client.permissions:
        ehr_list_address = ehr_helper.make_ehr_list_address()
        LOGGER.debug('has READ_PATIENT_DATA permission: ' + str(client_key))
        # Get Consent
        access = await get_data_processing_access(ehr_conn, client_key)
        patient_list = {}
        for address, pt in access.items():
            LOGGER.debug('patient access: ' + str(pt))
            patient = await get_patient(ehr_conn, pt.src_pkey)
            patient_list[pt.src_pkey] = patient
        #
        ehr_list_resources = await messaging.get_state_by_address(
            ehr_conn, ehr_list_address)
        for entity in ehr_list_resources.entries:
            cl = EHRWithUser()
            cl.ParseFromString(entity.data)

            ehr_list[entity.address] = cl
            LOGGER.debug('ehr: ' + str(cl))
        # Apply Consent
        for patient_address, pt in patient_list.items():
            LOGGER.debug('patient: ' + str(pt))
            for claim_address, e in ehr_list.items():
                LOGGER.debug('ehr: ' + str(e))
                if patient_address == e.client_pkey:
                    LOGGER.debug('match!')
                    pt_local = patient_list[patient_address]
                    e.name = pt_local.name
                    e.surname = pt_local.surname
                    ehr_list[claim_address] = e
        return ehr_list
    elif Permission(
            type=Permission.READ_OWN_PATIENT_DATA) in client.permissions:
        ehr_list_ids_address = ehr_helper.make_ehr_list_by_patient_address(
            client_key)
        LOGGER.debug('has READ_OWN_PATIENT_DATA permission: ' +
                     str(ehr_list_ids_address))
        ehr_list_ids = await messaging.get_state_by_address(
            ehr_conn, ehr_list_ids_address)
        for entity in ehr_list_ids.entries:
            ehr_id = entity.data.decode()
            ehr_address = ehr_helper.make_ehr_address(ehr_id)
            LOGGER.debug('get ehr: ' + str(ehr_address))
            ehr_resources = await messaging.get_state_by_address(
                ehr_conn, ehr_address)
            for entity2 in ehr_resources.entries:
                LOGGER.debug('get ehr entity2: ' + str(entity2.address))
                e = EHRWithUser()
                e.ParseFromString(entity2.data)
                ehr_list[entity2.address] = e
        return ehr_list
    else:
        LOGGER.debug(
            'neither READ_PATIENT_DATA nor READ_OWN_PATIENT_DATA permissions')
    raise ApiForbidden("Insufficient permission")
async def get_contracts(conn, client_key):
    client = await get_client(conn, client_key)
    contract_list = {}
    if Permission(type=Permission.READ_CONTRACT) in client.permissions:
        contract_list_address = insurance_helper.make_contract_list_address()
        LOGGER.debug('has READ_CONTRACT permission: ' + str(client_key))
        contract_list_ids = await messaging.get_state_by_address(
            conn, contract_list_address)
        for entity in contract_list_ids.entries:
            con = ContractWithUser()
            con.ParseFromString(entity.data)
            contract_list[entity.address] = con
            LOGGER.debug('contract: ' + str(con))
        return contract_list
    elif Permission(type=Permission.READ_OWN_CONTRACT) in client.permissions:
        # As Insurance
        contract_list_ids_address = insurance_helper.make_contract_list_by_insurance_address(
            client_key)
        LOGGER.debug('has READ_OWN_CONTRACT permission: ' +
                     str(contract_list_ids_address))
        contract_list_ids = await messaging.get_state_by_address(
            conn, contract_list_ids_address)
        for entity in contract_list_ids.entries:
            contract_id = entity.data.decode()
            contract_address = insurance_helper.make_contract_address(
                contract_id)
            LOGGER.debug('get contract: ' + str(contract_address))
            contract_resources = await messaging.get_state_by_address(
                conn, contract_address)
            for entity2 in contract_resources.entries:
                LOGGER.debug('get contract entity2: ' + str(entity2.address))
                con = ContractWithUser()
                con.ParseFromString(entity2.data)
                contract_list[entity2.address] = con
        # As Patient
        contract_list_ids2_address = insurance_helper.make_contract_list_by_patient_address(
            client_key)
        LOGGER.debug('has READ_OWN_CONTRACT permission (as patient): ' +
                     str(contract_list_ids2_address))
        contract_list_ids2 = await messaging.get_state_by_address(
            conn, contract_list_ids2_address)
        for entity in contract_list_ids2.entries:
            contract_id = entity.data.decode()
            contract_address = insurance_helper.make_contract_address(
                contract_id)
            LOGGER.debug('get contract: ' + str(contract_address))
            contract_resources = await messaging.get_state_by_address(
                conn, contract_address)
            for entity2 in contract_resources.entries:
                LOGGER.debug('get contract entity2: ' + str(entity2.address))
                con = ContractWithUser()
                con.ParseFromString(entity2.data)
                contract_list[entity2.address] = con
        return contract_list
    else:
        LOGGER.debug('neither READ_CONTRACT or READ_OWN_CONTRACT permissions')
    raise ApiForbidden("Insufficient permission")
async def get_claims(conn, client_key):
    client = await get_client(conn, client_key)
    claim_list = {}
    if Permission(type=Permission.READ_CLAIM) in client.permissions:
        claim_list_address = helper.make_claim_list_address()
        LOGGER.debug('has READ_CLAIM permission: ' + str(client_key))
        # Get Consent
        consent = await get_consent(conn, client_key)
        patient_list = {}
        for address, pt in consent.items():
            LOGGER.debug('patient consent: ' + str(pt))
            patient = await get_patient(conn, pt.patient_pkey)
            patient_list[pt.patient_pkey] = patient
        #
        claim_list_resources = await messaging.get_state_by_address(
            conn, claim_list_address)
        for entity in claim_list_resources.entries:
            cl = ClaimWithUser()
            cl.ParseFromString(entity.data)

            claim_list[entity.address] = cl
            LOGGER.debug('claim: ' + str(cl))
        # Apply Consent
        for patient_address, pt in patient_list.items():
            LOGGER.debug('patient: ' + str(pt))
            for claim_address, cl in claim_list.items():
                LOGGER.debug('claim: ' + str(cl))
                if patient_address == cl.client_pkey:
                    LOGGER.debug('match!')
                    pt_local = patient_list[patient_address]
                    cl.name = pt_local.name
                    cl.surname = pt_local.surname
                    claim_list[claim_address] = cl
        return claim_list
    elif Permission(type=Permission.READ_OWN_CLAIM) in client.permissions:
        claim_list_ids_address = helper.make_claim_list_by_patient_address(
            client_key)
        LOGGER.debug('has READ_OWN_CLAIM permission: ' +
                     str(claim_list_ids_address))
        claim_list_ids = await messaging.get_state_by_address(
            conn, claim_list_ids_address)
        for entity in claim_list_ids.entries:
            claim_id = entity.data.decode()
            claim_address = helper.make_claim_address(claim_id)
            LOGGER.debug('get claim: ' + str(claim_address))
            claim_resources = await messaging.get_state_by_address(
                conn, claim_address)
            for entity2 in claim_resources.entries:
                LOGGER.debug('get claim entity2: ' + str(entity2.address))
                cl = ClaimWithUser()
                cl.ParseFromString(entity2.data)
                claim_list[entity2.address] = cl
        return claim_list
    else:
        LOGGER.debug('neither READ_OWN_CLAIM nor READ_CLAIM permissions')
    raise ApiForbidden("Insufficient permission")
async def revoke_data_processing(conn, timeout, batches, client_key):
    client = await get_client(conn, client_key)
    if Permission(type=Permission.REVOKE_READ_DATA_ACCESS) in client.permissions and \
            Permission(type=Permission.REVOKE_WRITE_DATA_ACCESS) in client.permissions:
        LOGGER.debug('has permission: True')
        await _send(conn, timeout, batches)
        return
    else:
        LOGGER.debug('has permission: False')
    raise ApiForbidden("Insufficient permission")
async def get_lab_tests(conn, client_key):
    client = await get_client(conn, client_key)
    lab_tests = {}
    if Permission(type=Permission.READ_LAB_TEST) in client.permissions:
        lab_tests_address = helper.make_lab_test_list_address()
        LOGGER.debug('has READ_LAB_TEST permission: ' + str(client_key))
        #
        consent = await get_consent(conn, client_key)
        patient_list = {}
        for address, pt in consent.items():
            LOGGER.debug('patient consent: ' + str(pt))
            patient = await get_patient(conn, pt.patient_pkey)
            patient_list[pt.patient_pkey] = patient
        #
        lab_test_resources = await messaging.get_state_by_address(
            conn, lab_tests_address)
        for entity in lab_test_resources.entries:
            lt = AddLabTestWithUser()
            lt.ParseFromString(entity.data)
            lab_tests[entity.address] = lt
            LOGGER.debug('lab_test: ' + str(lt))
        for patient_address, pt in patient_list.items():
            LOGGER.debug('patient: ' + str(pt))
            for pulse_address, lt in lab_tests.items():
                LOGGER.debug('lab_test: ' + str(lt))
                if patient_address == lt.client_pkey:
                    LOGGER.debug('match!')
                    pt_local = patient_list[patient_address]
                    lt.name = pt_local.name
                    lt.surname = pt_local.surname
                    lab_tests[pulse_address] = lt
        return lab_tests
    elif Permission(type=Permission.READ_OWN_LAB_TEST) in client.permissions:
        lab_test_ids_address = helper.make_lab_test_list_by_patient_address(
            client_key)
        LOGGER.debug('has READ_OWN_LAB_TEST permission: ' +
                     str(lab_test_ids_address))
        lab_test_ids = await messaging.get_state_by_address(
            conn, lab_test_ids_address)
        for entity in lab_test_ids.entries:
            lab_test_id = entity.data.decode()
            lab_test_address = helper.make_lab_test_address(lab_test_id)
            LOGGER.debug('get lab test: ' + str(lab_test_address))
            lab_test_resources = await messaging.get_state_by_address(
                conn, lab_test_address)
            for entity2 in lab_test_resources.entries:
                LOGGER.debug('get lab test entity2: ' + str(entity2.address))
                lt = AddLabTestWithUser()
                lt.ParseFromString(entity2.data)
                lab_tests[entity2.address] = lt
        return lab_tests
    else:
        LOGGER.debug('neither READ_OWN_LAB_TEST nor READ_LAB_TEST permissions')
    raise ApiForbidden("Insufficient permission")
async def get_patients(ehr_conn, consent_conn, client_key):
    client = await get_client(consent_conn, client_key)
    patient_list = {}
    if Permission(type=Permission.READ_PATIENT) in client.permissions:
        LOGGER.debug('has READ_PATIENT permission: ' + str(client_key))
        list_patient_address = ehr_helper.make_patient_list_address()
        # Get Data Processing Access
        data_processing_access = await get_data_processing_access(
            consent_conn, client_key)
        data_processing_access_list = {}
        for address, pt in data_processing_access.items():
            LOGGER.debug('data_processing_access: ' + str(pt))
            patient = await get_patient(ehr_conn, pt.src_pkey)
            data_processing_access_list[pt.src_pkey] = patient

        # consent = await get_read_ehr_consent(conn, client_key)
        # consent_list = {}
        # for address, pt in consent.items():
        #     LOGGER.debug('consent: ' + str(pt))
        #     patient = await get_patient(conn, pt.src_pkey)
        #     consent_list[pt.src_pkey] = patient
        #
        patient_list_resources = await messaging.get_state_by_address(
            ehr_conn, list_patient_address)
        for entity in patient_list_resources.entries:
            pat = Patient()
            pat.ParseFromString(entity.data)
            patient_list[entity.address] = pat
            LOGGER.debug('patient: ' + str(pat))
        # Apply Access
        for patient_address, pt in patient_list.items():
            LOGGER.debug('patient: ' + str(pt))
            if Permission(
                    type=Permission.READ_OWN_PATIENT
            ) in client.permissions and pt.public_key == client_key:
                pass
            elif pt.public_key not in data_processing_access_list:
                pat2 = Patient()
                patient_list[patient_address] = pat2
        return patient_list
    elif Permission(type=Permission.READ_OWN_PATIENT) in client.permissions:
        LOGGER.debug('has READ_OWN_PATIENT: ' + str(client_key))
        # Get Data Processing Access
        data_processing_access = await get_data_processing_access(
            consent_conn, client_key)
        data_processing_access_list = {}
        for address, pt in data_processing_access.items():
            LOGGER.debug('data_processing_access: ' + str(pt))
            patient = await get_patient(ehr_conn, pt.src_pkey)
            data_processing_access_list[pt.src_pkey] = patient
        return data_processing_access_list
    raise ApiForbidden("Insufficient permission")
async def get_pulse_list(conn, client_key):
    client = await get_client(conn, client_key)
    pulse_list = {}
    if Permission(type=Permission.READ_PULSE) in client.permissions:
        pulse_list_address = helper.make_pulse_list_address()
        LOGGER.debug('has READ_PULSE permission: ' + str(client_key))
        consent = await get_consent(conn, client_key)
        patient_list = {}
        for address, pt in consent.items():
            LOGGER.debug('patient consent: ' + str(pt))
            patient = await get_patient(conn, pt.patient_pkey)
            patient_list[pt.patient_pkey] = patient
        pulse_list_resources = await messaging.get_state_by_address(
            conn, pulse_list_address)
        for entity in pulse_list_resources.entries:
            pl = AddPulseWithUser()
            pl.ParseFromString(entity.data)
            pulse_list[entity.address] = pl
            LOGGER.debug('pulse: ' + str(pl))
        for patient_address, pt in patient_list.items():
            LOGGER.debug('patient: ' + str(pt))
            for pulse_address, pl in pulse_list.items():
                LOGGER.debug('pulse: ' + str(pl))
                if patient_address == pl.client_pkey:
                    LOGGER.debug('match!')
                    pt_local = patient_list[patient_address]
                    pl.name = pt_local.name
                    pl.surname = pt_local.surname
                    pulse_list[pulse_address] = pl
        return pulse_list
    elif Permission(type=Permission.READ_OWN_PULSE) in client.permissions:
        pulse_list_ids_address = helper.make_pulse_list_by_patient_address(
            client_key)
        LOGGER.debug('has READ_OWN_PULSE permission: ' +
                     str(pulse_list_ids_address))
        pulse_list_ids = await messaging.get_state_by_address(
            conn, pulse_list_ids_address)
        for entity in pulse_list_ids.entries:
            pulse_id = entity.data.decode()
            pulse_address = helper.make_pulse_address(pulse_id)
            LOGGER.debug('get pulse: ' + str(pulse_address))
            pulse_resources = await messaging.get_state_by_address(
                conn, pulse_address)
            for entity2 in pulse_resources.entries:
                LOGGER.debug('get pulse entity2: ' + str(entity2.address))
                pl = AddPulseWithUser()
                pl.ParseFromString(entity2.data)
                pulse_list[entity2.address] = pl
        return pulse_list
    else:
        LOGGER.debug('neither READ_OWN_PULSE nor READ_PULSE permissions')
    raise ApiForbidden("Insufficient permission")
async def update_claim(conn, timeout, batches, dest_pkey, src_pkey):
    client = await get_client(conn, dest_pkey)
    if Permission(type=Permission.READ_CLAIM) in client.permissions \
            and Permission(type=Permission.UPDATE_CLAIM) in client.permissions:
        LOGGER.debug('has READ_CLAIM and UPDATE_CLAIM permission: True')
        # Has consent from patient
        consent = await has_consent(conn, dest_pkey, src_pkey)
        if not consent:
            LOGGER.debug('no consent from patient')
            raise ApiForbidden("Insufficient permission")
        #
        await _send(conn, timeout, batches)
        return
    else:
        LOGGER.debug('has permission: False')
    raise ApiForbidden("Insufficient permission")
async def grant_access(conn, timeout, batches, client_key):
    client = await get_client(conn, client_key)
    if Permission(type=Permission.GRANT_ACCESS) in client.permissions:
        LOGGER.debug('has permission: True')
        await _send(conn, timeout, batches)
        return
    else:
        LOGGER.debug('has permission: False')
    raise ApiForbidden("Insufficient permission")
async def add_pulse(conn, timeout, batches, client_key):
    client = await get_client(conn, client_key)
    if Permission(type=Permission.WRITE_PULSE) in client.permissions:
        LOGGER.debug('has permission: True')
        await _send(conn, timeout, batches)
        return
    else:
        LOGGER.debug('has permission: False')
    raise ApiForbidden("Insufficient permission")
async def revoke_investigator_access(conn, timeout, batches, client_key):
    client = await get_client(conn, client_key)
    if Permission(type=Permission.REVOKE_INVESTIGATOR_ACCESS) in client.permissions:
        LOGGER.debug('has permission: True')
        await _send(conn, timeout, batches)
        return
    else:
        LOGGER.debug('has permission: False')
    raise ApiForbidden("Insufficient permission")
async def sign_inform_document_consent(conn, timeout, batches, client_key):
    client = await get_client(conn, client_key)
    if Permission(type=Permission.SIGN_INFORM_CONSENT) in client.permissions:
        LOGGER.debug('has permission: True')
        await _send(conn, timeout, batches)
        return
    else:
        LOGGER.debug('has permission: False')
    raise ApiForbidden("Insufficient permission")
async def update_investigator(conn, timeout, batches, client_key):
    client = await get_client(conn, client_key)
    if Permission(type=Permission.UPDATE_TRIAL_DATA) in client.permissions:
        LOGGER.debug('has UPDATE_TRIAL_DATA permission: True')
        await _send(conn, timeout, batches)
        return
    else:
        LOGGER.debug('has permission: False')
    raise ApiForbidden("Insufficient permission")
async def import_screening_data(conn, timeout, batches, client_key):
    client = await get_client(conn, client_key)
    if Permission(type=Permission.IMPORT_TRIAL_DATA) in client.permissions:
        LOGGER.debug('has IMPORT_TRIAL_DATA permission: True')
        await _send(conn, timeout, batches)
        return
    else:
        LOGGER.debug('has permission: False')
    raise ApiForbidden("Insufficient permission")
async def get_payments(conn, client_key):
    client = await get_client(conn, client_key)
    payment_list = {}
    if Permission(type=Permission.READ_PAYMENT) in client.permissions:
        payment_list_address = payment_helper.make_payment_list_address()
        LOGGER.debug('has READ_PAYMENT permission: ' + str(client_key))
        payment_resources_ids = await messaging.get_state_by_address(
            conn, payment_list_address)
        for entity in payment_resources_ids.entries:
            LOGGER.debug('get payment entity: ' + str(entity.address))
            pay = Payment()
            pay.ParseFromString(entity.data)
            payment_list[entity.address] = pay
            LOGGER.debug('payment: ' + str(pay))
        return payment_list
    elif Permission(type=Permission.READ_OWN_PAYMENT) in client.permissions:
        # As Patient
        payment_list_ids_address = payment_helper.make_payment_list_by_patient_address(
            client_key)
        LOGGER.debug('has READ_OWN_PAYMENT permission: ' +
                     str(payment_list_ids_address))
        payment_list_ids = await messaging.get_state_by_address(
            conn, payment_list_ids_address)
        for entity in payment_list_ids.entries:
            payment_id = entity.data.decode()
            payment_address = payment_helper.make_payment_address(payment_id)
            LOGGER.debug('get payment entity: ' + str(payment_address))
            payment_resources = await messaging.get_state_by_address(
                conn, payment_address)
            for entity2 in payment_resources.entries:
                LOGGER.debug('get payment entity2: ' + str(entity2.address))
                pay = Payment()
                pay.ParseFromString(entity2.data)
                if pay.contract_id is not None and pay.contract_id != '':
                    timestamp = pay.timestamp
                    pay = Payment()
                    pay.timestamp = timestamp
                payment_list[entity2.address] = pay

        return payment_list
    else:
        LOGGER.debug('neither READ_PAYMENT or READ_OWN_PAYMENT permissions')
    raise ApiForbidden("Insufficient permission")
async def add_contract(conn, timeout, batches, client_key):
    # LOGGER.debug('add_contract')
    # await _send(conn, timeout, batches)
    client = await get_client(conn, client_key)
    if Permission(type=Permission.WRITE_CONTRACT) in client.permissions:
        LOGGER.debug('has permission: True')
        await _send(conn, timeout, batches)
        return
    else:
        LOGGER.debug('has permission: False')
    raise ApiForbidden("Insufficient permission")
async def get_pre_screening_data(conn, investigator_pkey, inc_excl_criteria):
    client = await get_client(conn, investigator_pkey)
    if Permission(type=Permission.READ_PATIENT_DATA) in client.permissions:
        ehr_list = await get_shared_ehrs(conn, investigator_pkey)
        ehr_screening_list = {}
        for address, ehr in ehr_list.items():
            if _match_incl_excl_criteria(ehr, inc_excl_criteria):
                ehr_screening_list[address] = ehr
        return ehr_screening_list
    else:
        LOGGER.debug('has permission: False')
    raise ApiForbidden("Insufficient permission")
async def get_hospitals(conn, client_key):
    client = await get_client(conn, client_key)
    hospital_list = {}
    if Permission(type=Permission.READ_HOSPITAL) in client.permissions:
        list_hospital_address = ehr_helper.make_hospital_list_address()
        list_hospital_resources = await messaging.get_state_by_address(conn, list_hospital_address)
        for entity in list_hospital_resources.entries:
            hp = Hospital()
            hp.ParseFromString(entity.data)
            LOGGER.debug('hospital: ' + str(hp))
            hospital_list[entity.address] = hp
        return hospital_list
    raise ApiForbidden("Insufficient permission")
async def get_investigators(conn, client_key):
    client = await get_client(conn, client_key)
    investigator_list = {}
    if Permission(type=Permission.READ_INVESTIGATOR) in client.permissions:
        list_investigator_address = ehr_helper.make_investigator_list_address()
        list_investigator_resources = await messaging.get_state_by_address(conn, list_investigator_address)
        for entity in list_investigator_resources.entries:
            dp = Investigator()
            dp.ParseFromString(entity.data)
            LOGGER.debug('investigator: ' + str(dp))
            investigator_list[entity.address] = dp
        return investigator_list
    elif Permission(type=Permission.READ_OWN_INVESTIGATOR) in client.permissions:
        list_investigator_address = ehr_helper.make_investigator_address(client_key)
        list_investigator_resources = await messaging.get_state_by_address(conn, list_investigator_address)
        for entity in list_investigator_resources.entries:
            dp = Investigator()
            dp.ParseFromString(entity.data)
            LOGGER.debug('investigator: ' + str(dp))
            investigator_list[entity.address] = dp
        return investigator_list
    raise ApiForbidden("Insufficient permission")
async def get_doctors(conn, client_key):
    client = await get_client(conn, client_key)
    doctors = {}
    if Permission(type=Permission.READ_DOCTOR) in client.permissions:
        list_doctors_address = helper.make_doctor_list_address()
        doctor_resources = await messaging.get_state_by_address(
            conn, list_doctors_address)
        for entity in doctor_resources.entries:
            doc = CreateDoctor()
            doc.ParseFromString(entity.data)
            doctors[entity.address] = doc
        return doctors
    raise ApiForbidden("Insufficient permission")
async def get_clinics(conn, client_key):
    client = await get_client(conn, client_key)
    clinic_list = {}
    if Permission(type=Permission.READ_CLINIC) in client.permissions:
        list_clinic_address = helper.make_clinic_list_address()
        list_clinic_resources = await messaging.get_state_by_address(
            conn, list_clinic_address)
        for entity in list_clinic_resources.entries:
            cl = CreateClinic()
            cl.ParseFromString(entity.data)
            LOGGER.debug('clinic: ' + str(cl))
            clinic_list[entity.address] = cl
        return clinic_list
    raise ApiForbidden("Insufficient permission")
async def get_insurances(conn, client_key):
    client = await get_client(conn, client_key)
    insurances = {}
    if Permission(
            type=Permission.READ_INSURANCE_COMPANY) in client.permissions:
        list_insurance_address = insurance_helper.make_insurance_list_address()
        insurance_resources = await messaging.get_state_by_address(
            conn, list_insurance_address)
        for entity in insurance_resources.entries:
            ins = Insurance()
            ins.ParseFromString(entity.data)
            insurances[entity.address] = ins
        return insurances
    raise ApiForbidden("Insufficient permission")
async def get_data_from_investigators(conn, client_key):
    client = await get_client(conn, client_key)
    data_list = {}
    if Permission(type=Permission.READ_TRIAL_DATA) in client.permissions:
        data_list_address = ehr_helper.make_investigator_data_list_address()
        LOGGER.debug('has READ_DATA permission: ' + str(client_key))
        data_list_resources = await messaging.get_state_by_address(conn, data_list_address)
        for entity in data_list_resources.entries:
            data = Data()
            data.ParseFromString(entity.data)
            data_list[entity.address] = data
            LOGGER.debug('data: ' + str(data))
        return data_list
    else:
        LOGGER.debug('no READ_DATA permissions')
    raise ApiForbidden("Insufficient permission")
async def add_ehr(conn, timeout, batches, dest_pkey, src_pkey):
    client = await get_client(conn, dest_pkey)
    if Permission(type=Permission.WRITE_PATIENT_DATA) in client.permissions:
        LOGGER.debug('has WRITE_PATIENT_DATA permission: True')
        # Has consent from patient
        access = await has_data_processing_access(conn, dest_pkey, src_pkey)
        if not access:
            LOGGER.debug('no data processing access')
            raise ApiForbidden("Insufficient permission")
        #
        await _send(conn, timeout, batches)
        return
        # LOGGER.debug('has permission: True')
        # await _send(conn, timeout, batches)
        # return
    else:
        LOGGER.debug('has permission: False')
    raise ApiForbidden("Insufficient permission")
async def get_labs(conn, client_key):
    # client_address = consent_helper.make_client_address(client_key)
    # LOGGER.debug('client_address: ' + str(client_address))
    # client_resources = await messaging.get_state_by_address(conn, client_address)
    # LOGGER.debug('client_resources: ' + str(client_resources))
    # for entity in client_resources.entries:
    #     cl = Client()
    #     cl.ParseFromString(entity.data)
    #     LOGGER.debug('client: ' + str(cl))
    #     if Permission(type=Permission.READ_LAB) in cl.permissions:
    #         return await messaging.get_state_by_address(conn, address_suffix)
    client = await get_client(conn, client_key)
    if Permission(type=Permission.READ_LAB) in client.permissions:
        LOGGER.debug('has permission: True')
        list_lab_address = helper.make_lab_list_address()
        return await messaging.get_state_by_address(conn, list_lab_address)
    else:
        LOGGER.debug('has permission: False')
    raise ApiForbidden("Insufficient permission")