diff --git a/crud.py b/crud.py index 8c0b7f0..9179d9a 100644 --- a/crud.py +++ b/crud.py @@ -95,10 +95,18 @@ 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 +124,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..adf7a3e 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: Optional[datetime] = None # unused, TODO: remove currency: Optional[str] = None currency_amount: Optional[float] = None paid: bool = False @@ -91,6 +92,7 @@ def public(self): "completelinktext", ] c = {k: v for k, v in self.dict().items() if k in public_keys} + c["timestamp"] = self.timestamp.isoformat() 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", {