started working on cron job task

This commit is contained in:
2020-05-14 23:40:27 -05:00
parent 0dc58ed6a8
commit d1078b8f30
5 changed files with 212 additions and 28 deletions

View File

@ -24,9 +24,9 @@ class DBModel:
def consume_token(self, token):
self.cursor.execute("SELECT email FROM login_tokens WHERE token = %s and created > (NOW() - INTERVAL '20 min')", (token, ))
rows = self.cursor.fetchall()
if len(rows) > 0:
email = rows[0][0]
row = self.cursor.fetchone()
if row:
email = row[0]
self.cursor.execute("DELETE FROM login_tokens WHERE email = %s", (email, ))
self.connection.commit()
return email
@ -125,14 +125,13 @@ class DBModel:
WHERE vms.email = %s AND vms.id = %s""",
(email, id)
)
rows = self.cursor.fetchall()
if len(rows) == 0:
row = self.cursor.fetchone()
if not row:
return None
x = rows[0]
vm = dict(
id=x[0], ipv4=x[1], ipv6=x[2], os_description=x[3], created=x[4], deleted=x[5],
size=x[6], dollars_per_month=x[7], vcpus=x[8], memory_mb=x[9], bandwidth_gb_per_month=x[10]
id=row[0], ipv4=row[1], ipv6=row[2], os_description=row[3], created=row[4], deleted=row[5],
size=row[6], dollars_per_month=row[7], vcpus=row[8], memory_mb=row[9], bandwidth_gb_per_month=row[10]
)
self.cursor.execute("""
@ -166,23 +165,62 @@ class DBModel:
def consume_payment_session(self, payment_type, id, dollars):
self.cursor.execute("SELECT email, dollars FROM payment_sessions WHERE id = %s AND type = %s", (id, payment_type))
rows = self.cursor.fetchall()
if len(rows) > 0:
if int(rows[0][1]) != int(dollars):
row = self.cursor.fetchone()
if row:
if int(row[1]) != int(dollars):
print(f"""
{payment_type} gave us a completed payment session with a different dollar amount than what we had recorded!!
id: {id}
account: {rows[0][0]}
Recorded amount: {int(rows[0][1])}
account: {row[0]}
Recorded amount: {int(row[1])}
{payment_type} sent: {int(dollars)}
""")
# not sure what to do here. For now just log and do nothing.
self.cursor.execute( "DELETE FROM payment_sessions WHERE id = %s AND type = %s", (id, payment_type) )
self.cursor.execute( "INSERT INTO payments (email, dollars) VALUES (%s, %s)", (rows[0][0], rows[0][1]) )
self.cursor.execute( "INSERT INTO payments (email, dollars) VALUES (%s, %s) RETURNING id", (row[0], row[1]) )
if payment_type == "btcpay":
payment_id = self.cursor.fetchone()[0]
self.cursor.execute(
"INSERT INTO unresolved_btcpay_invoices (id, email, payment_id) VALUES (%s, %s, %s)",
(id, row[0], payment_id)
)
self.connection.commit()
return rows[0][0]
return row[0]
else:
return None
def btcpay_invoice_resolved(self, id, completed):
self.cursor.execute("SELECT email, payment_id FROM unresolved_btcpay_invoices WHERE id = %s ", (id,))
row = self.cursor.fetchone()
if row:
self.cursor.execute( "DELETE FROM unresolved_btcpay_invoices WHERE id = %s", (id,) )
if not completed:
self.cursor.execute("UPDATE payments SET invalidated = True WHERE email = %s id = %s", (row[0], row[1]))
self.connection.commit()
def get_unresolved_btcpay_invoices(self):
self.cursor.execute("SELECT id, payments.created, email FROM unresolved_btcpay_invoices JOIN payments on payment_id = payment.id")
return list(map(lambda row: dict(id=row[0], created=row[1], email=row[2]), self.cursor.fetchall()))
def get_account_balance_warning(self, email):
self.cursor.execute("SELECT account_balance_warning FROM accounts WHERE email = %s", (email,))
return self.cursor.fetchone()[0]
def set_account_balance_warning(self, email, account_balance_warning):
self.cursor.execute("UPDATE accounts SET account_balance_warning = %s WHERE email = %s", (account_balance_warning, email))
self.connection.commit()
def all_accounts(self):
self.cursor.execute("SELECT email, account_balance_warning FROM accounts")
return list(map(lambda row: dict(row=row[0], account_balance_warning=row[1]), self.cursor.fetchall()))