Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update_charge on postgres failed #57

Merged
merged 3 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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,),
Expand Down
6 changes: 4 additions & 2 deletions models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import json
from datetime import datetime
from typing import Optional

from fastapi.param_functions import Query
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion static/js/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
},
Expand Down
5 changes: 3 additions & 2 deletions static/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
1 change: 0 additions & 1 deletion views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
{
Expand Down