Ejemplo n.º 1
0
def get_payments():
    database = DbHelper()
    columns = [c[0] for c in database.get_payments_discounts_columns()]
    return jsonify({
        'payments':
        [dict(zip(columns, t)) for t in database.get_payments_from_database()]
    })
Ejemplo n.º 2
0
def refresh_database(bunq, user):
    discounts = user['discounts']
    database = DbHelper()
    existing_tx = [tranx[0] for tranx in database.get_payments_from_database()]

    #### FIRST TIME DATABASE INITIALIZATION ######
    history = bunq.get_all_payment(count=200)
    # for h in history:
    #     if h.description.split("-")[0] != "CASHBACK":
    #         _, dsc = determine_discount(h.description, user)
    #         database.add_payment_to_database(h.id_, h.description, h.amount.value, dsc*h.amount.value)
    #         existing_tx.append(h.id_)

    while True:
        new_payments = bunq.get_all_payment(5)
        for npy in new_payments:
            if str(npy.id_) not in existing_tx and npy.description.split(
                    "-")[0] != "CASHBACK":
                # Check discount eligibility
                shop, dsc = determine_discount(npy.description, user)
                existing_tx.append(str(npy.id_))
                # Add new payment to database
                database.add_payment_to_database(npy.id_, npy.description,
                                                 npy.amount.value,
                                                 dsc * float(npy.amount.value))

                if shop is not None:
                    # Request cashback from sugar daddy
                    desc = "{}-{}-{}".format("CASHBACK", shop, dsc)
                    bunq.make_request(dsc, desc, "*****@*****.**")

                    # Increase the points the shopper has
                    for i, discount in enumerate(discounts):
                        print(i, discount)
                        if discount['shop'] == shop:
                            level_before = get_level(discounts[i])
                            user['discounts'][i]['current_points'] += int(
                                dsc * -1 * float(npy.amount.value)) * 10
                            level_after = get_level(discounts[i])

                            if level_before != level_after:
                                user['loots']['number'] += 1

                    socketio.emit(
                        'NewPayment', {
                            'shop': {
                                'name':
                                shop,
                                'current_points':
                                user['discounts'][i]['current_points'],
                                'loot_number':
                                user['loots']['number']
                            }
                        })

        time.sleep(3)