def complaint_list():
    customer_by_id = {}
    for record in csv_records('all.csv'):
        customer_by_id[record['id']] = {
            'firstname': record['firstname'],
            'lastname': record['lastname'],
            'dob': record['dob'],
            'address': record['address'],
        }
    result = []
    for record in csv_records('hc_complaint.csv'):
        record = dict(record)
        record['customer'] = customer_by_id[record['customer_id']]
        result.append(record)
    return jsonify(result)
def export_data():
    export_id = str(uuid4())
    filename = export_id + '.zip'
    my_dir = os.path.dirname(os.path.realpath(__file__))
    full_path = os.path.join(my_dir, 'static', 'export', filename)

    with ZipFile(full_path, 'w') as myzip:
        auth_dataset = generate_data_set(csv_records('hc_authorization.csv'),
                                         'marketing')
        id_rows = len(auth_dataset)
        auth_csv = csv_from_generated_data(auth_dataset)
        myzip.writestr('authorized_identifiable.csv', auth_csv)

        deid_dataset = generate_data_set(None, 'marketing_deid')
        deid_rows = len(deid_dataset)
        deid_csv = csv_from_generated_data(deid_dataset)
        myzip.writestr('deidentified_data.csv', deid_csv)

    return {
        'export_id': export_id,
        'created_at': datetime_isoformat(datetime.now(UTC)),
        'id_rows': id_rows,
        'deid_rows': deid_rows,
        'url': url_for('static', filename='export/' + filename)
    }
def find_customer_id(firstname, lastname, dob):
    firstname = firstname.upper()
    lastname = lastname.upper()
    for record in csv_records('all.csv'):
        if (record['firstname'] == firstname and record['lastname'] == lastname
                and record['dob'] == dob):
            return record['id']
    return None
def authorization_list():
    customer_by_id = {}
    for record in csv_records('all.csv'):
        customer_by_id[record['id']] = {
            'firstname': record['firstname'],
            'lastname': record['lastname'],
            'dob': record['dob'],
            'age': age_from_dob_str(record['dob']),
            'address': record['address'],
        }
    result = []
    for record in csv_records('hc_authorization.csv'):
        record = dict(record)
        del record['token']
        del record['auth_hash']
        record['customer'] = customer_by_id[record['id']]
        result.append(record)
    return jsonify(result)
def authorization_query():
    authorization = authenticate_customer(request)

    for record in csv_records('all.csv'):
        if record['id'] == authorization['id']:
            authorization['customer'] = {
                'firstname': record['firstname'],
                'lastname': record['lastname'],
            }
    del authorization['auth_hash']
    return jsonify(authorization)
def authorization():
    authorization = authenticate_customer(request, auto_register=True)
    authorization['token'] = str(uuid4())
    authorization['created_at'] = datetime_isoformat(datetime.now(UTC))
    authorization['authorized_fields'] = request.json['authorized_fields']
    csv_append('hc_authorization.csv', authorization)
    del authorization['auth_hash']

    for record in csv_records('all.csv'):
        if record['id'] == authorization['id']:
            authorization['customer'] = {
                'firstname': record['firstname'],
                'lastname': record['lastname'],
            }
    return jsonify(authorization)
def generate_data_set(authorizations, purpose):
    if authorizations is not None:
        authorized_fields_of_customer = {}
        for authorization in authorizations:
            customer_id = authorization['id']
            authorized_fields = authorization['authorized_fields']
            authorized_fields_of_customer[customer_id] = authorized_fields

    diseases_of_customer = {}
    prescription_ids_of_customer = {}
    customer_id_by_insurance_member_id = {}
    result = []
    for record in csv_records('all.csv'):
        customer_id = record['id']
        include_diseases = False
        include_prescriptions = False
        if authorizations is not None:
            authorized_fields = authorized_fields_of_customer.get(
                customer_id, None)
            if authorized_fields is None:
                continue
        if purpose == 'marketing_deid':
            data = {'id': customer_id}
            result.append(data)
            data['zip'] = record['zip']
            data['gender'] = record['gender']
            data['age_group'] = age_group_from_dob_str(record['dob'])
            include_diseases = True
            include_prescriptions = True
        elif purpose == 'individual_req':
            # The individual is requesting all of his/her own information.
            result.append(dict(record))
            include_diseases = True
            include_prescriptions = True
        else:
            data = {'id': customer_id}
            result.append(data)
            for field in authorized_fields.split('+'):
                if field == 'age':
                    data[field] = age_from_dob_str(record['dob'])
                elif field == 'diseases':
                    include_diseases = True
                elif field == 'prescriptions':
                    include_prescriptions = True
                elif field == 'aggregation':
                    pass
                else:
                    data[field] = record[field]

        if include_diseases or include_prescriptions:
            im_id = record['insurance_member_id']
            customer_id_by_insurance_member_id[im_id] = customer_id
        if include_diseases:
            diseases_of_customer[customer_id] = set()
        if include_prescriptions:
            prescription_ids_of_customer[customer_id] = set()

    customers_with_health = set()
    customer_id_by_insurance_id = {}
    for record in csv_records('insurance_company.csv'):
        im_id = record['insurance_member_id']
        customer_id = customer_id_by_insurance_member_id.get(im_id, None)
        if customer_id is not None:
            customer_id_by_insurance_id[record['insurance_id']] = customer_id
            customers_with_health.add(customer_id)

    if authorizations is None:
        result = filter(lambda x: x['id'] in customers_with_health, result)
        result = remove_identifiable_individuals(result)

    disease_name = {}
    for record in csv_records('disease_id.csv'):
        disease_name[record['disease_id']] = record['disease_name']

    prescription_by_id = {}
    for record in csv_records('prescriptions.csv'):
        pid = record['perscription_id']
        prescription_by_id[pid] = record

    for record in csv_records('insurance_health.csv'):
        insurance_id = record['insurance_id']

        customer_id = customer_id_by_insurance_id.get(insurance_id, None)
        if customer_id is not None:
            disease = disease_name[record['disease_id']]
            diseases_of_customer[customer_id].add(disease)
            pid = record['perscription_id']
            prescription_ids_of_customer[customer_id].add(pid)

    for customer in result:
        customer_id = customer['id']
        del customer['id']
        diseases = diseases_of_customer.get(customer_id, None)
        if diseases is not None:
            customer['diseases'] = '|'.join(diseases)
        pids = prescription_ids_of_customer.get(customer_id, None)
        if pids is not None:
            prescriptions = [prescription_by_id[pid] for pid in pids]
            customer['prescription_marketing_names'] = '|'.join(
                (p['marketing_name'] for p in prescriptions))
            if purpose == 'individual_req':
                customer['prescription_chemical_names'] = '|'.join(
                    (p['chemical_name'] for p in prescriptions))
                customer['prescription_probabilities'] = '|'.join(
                    (p['perscription_probability'] for p in prescriptions))
    return result
def find_customer_record(customer_id):
    for record in csv_records('all.csv'):
        if record['id'] == customer_id:
            return record
    return None
def find_authorization_by_token(token):
    for record in csv_records('hc_authorization.csv'):
        if record['token'] == token:
            return record
    return None
def export_list():
    return jsonify(list(csv_records('hc_export.csv')))
def last_authorization_by_customer(customer_id):
    result = None
    for record in csv_records('hc_authorization.csv'):
        if record['id'] == customer_id:
            result = record
    return result