From cd791406a47e857d7a92da9ae27c1fbde251687f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Tue, 20 Aug 2024 18:33:41 +0200 Subject: [PATCH 1/3] fix: update_charge on postgres failed fix --- crud.py | 15 ++++++--------- models.py | 7 +++++-- static/js/display.js | 3 ++- static/js/utils.js | 5 +++-- views.py | 1 - 5 files changed, 16 insertions(+), 15 deletions(-) diff --git a/crud.py b/crud.py index 8c0b7f0..5835189 100644 --- a/crud.py +++ b/crud.py @@ -1,4 +1,5 @@ import json +from datetime import datetime from typing import Optional from lnbits.core.services import create_invoice @@ -95,10 +96,12 @@ async def create_charge( async def update_charge(charge: Charge) -> Charge: - q = ", ".join([f"{field[0]} = ?" for field in charge.dict().items()]) await db.execute( - f"UPDATE satspay.charges SET {q} WHERE id = ?", - (*charge.dict().values(), charge.id), + """ + UPDATE satspay.charges + SET extra = ?, balance = ?, pending = ?, paid = ? WHERE id = ? + """, + (charge.extra, charge.balance, charge.pending, charge.paid, charge.id,), ) return charge @@ -116,12 +119,6 @@ async def get_charge_by_onchain_address(onchain_address: str) -> Optional[Charge async def get_charges(user: str) -> list[Charge]: - await db.execute( - f""" - UPDATE satspay.charges SET last_accessed_at = {db.timestamp_now} WHERE "user" = ? - """, - (user,), - ) rows = await db.fetchall( """SELECT * FROM satspay.charges WHERE "user" = ? ORDER BY "timestamp" DESC """, (user,), diff --git a/models.py b/models.py index 1f42501..8026d63 100644 --- a/models.py +++ b/models.py @@ -1,6 +1,7 @@ from __future__ import annotations import json +from datetime import datetime from typing import Optional from fastapi.param_functions import Query @@ -60,8 +61,8 @@ class Charge(BaseModel): zeroconf: bool balance: int pending: Optional[int] = 0 - timestamp: int - last_accessed_at: Optional[int] = 0 + timestamp: datetime + last_accessed_at: datetime # unused, TODO: remove currency: Optional[str] = None currency_amount: Optional[float] = None paid: bool = False @@ -91,6 +92,8 @@ def public(self): "completelinktext", ] c = {k: v for k, v in self.dict().items() if k in public_keys} + c["timestamp"] = self.timestamp.isoformat() + print(c["timestamp"]) if self.paid: c["completelink"] = self.completelink return c diff --git a/static/js/display.js b/static/js/display.js index 99a9f09..6f3f7c4 100644 --- a/static/js/display.js +++ b/static/js/display.js @@ -35,7 +35,8 @@ new Vue({ hasEnded() { const chargeTimeSeconds = this.charge.time * 60 const now = new Date().getTime() / 1000 - const timeSecondsLeft = chargeTimeSeconds - now + this.charge.timestamp + const then = new Date(this.charge.timestamp).getTime() / 1000 + const timeSecondsLeft = chargeTimeSeconds - now + then return timeSecondsLeft <= 0 || this.charge.paid } }, diff --git a/static/js/utils.js b/static/js/utils.js index 89b2c69..048f24c 100644 --- a/static/js/utils.js +++ b/static/js/utils.js @@ -22,9 +22,10 @@ const mapCharge = (obj, oldObj = {}) => { ? JSON.parse(charge.extra) : charge.extra const now = new Date().getTime() / 1000 + const then = new Date(charge.timestamp).getTime() / 1000 const chargeTimeSeconds = charge.time * 60 - const secondsSinceCreated = chargeTimeSeconds - now + charge.timestamp - charge.timeSecondsLeft = chargeTimeSeconds - now + charge.timestamp + const secondsSinceCreated = chargeTimeSeconds - now + then + charge.timeSecondsLeft = chargeTimeSeconds - now + then charge.timeLeft = charge.timeSecondsLeft <= 0 ? '00:00:00' diff --git a/views.py b/views.py index 74c37be..241a6d7 100644 --- a/views.py +++ b/views.py @@ -43,7 +43,6 @@ async def display_charge(request: Request, charge_id: str): raise HTTPException( status_code=HTTPStatus.NOT_FOUND, detail="Charge link does not exist." ) - return satspay_renderer().TemplateResponse( "satspay/display.html", { From 9fa63df62d78044ef2738361d5229ad74d935902 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Tue, 20 Aug 2024 18:43:15 +0200 Subject: [PATCH 2/3] fixup! --- crud.py | 9 +++++++-- models.py | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/crud.py b/crud.py index 5835189..9179d9a 100644 --- a/crud.py +++ b/crud.py @@ -1,5 +1,4 @@ import json -from datetime import datetime from typing import Optional from lnbits.core.services import create_invoice @@ -101,7 +100,13 @@ async def update_charge(charge: Charge) -> Charge: UPDATE satspay.charges SET extra = ?, balance = ?, pending = ?, paid = ? WHERE id = ? """, - (charge.extra, charge.balance, charge.pending, charge.paid, charge.id,), + ( + charge.extra, + charge.balance, + charge.pending, + charge.paid, + charge.id, + ), ) return charge diff --git a/models.py b/models.py index 8026d63..fc35df6 100644 --- a/models.py +++ b/models.py @@ -62,7 +62,7 @@ class Charge(BaseModel): balance: int pending: Optional[int] = 0 timestamp: datetime - last_accessed_at: datetime # unused, TODO: remove + last_accessed_at: Optional[datetime] = None # unused, TODO: remove currency: Optional[str] = None currency_amount: Optional[float] = None paid: bool = False From b19a6fa9386f9c635200758afe217f46d2cbad58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Tue, 20 Aug 2024 18:45:05 +0200 Subject: [PATCH 3/3] fixup! --- models.py | 1 - 1 file changed, 1 deletion(-) diff --git a/models.py b/models.py index fc35df6..adf7a3e 100644 --- a/models.py +++ b/models.py @@ -93,7 +93,6 @@ def public(self): ] c = {k: v for k, v in self.dict().items() if k in public_keys} c["timestamp"] = self.timestamp.isoformat() - print(c["timestamp"]) if self.paid: c["completelink"] = self.completelink return c